Autopopulate a relate field in Quick Create

I am trying to autopopulate a relate field when creating an opportunity off of a contact. I have done extensive searching of these forums and the sugarcrm forums. I have found several ways of doing this but am stuck. I can get the contact name into my relate field, “contact_c” but every time I try to save the Opportunity, it gives and error - [color=#ff0000]No match for field: Contact[/color]

First, I created a view.edit.php at \custom\modules\Opportunities\views\view.edit.php and added the following into the display function.

$contact_name = $_REQUEST[‘contact_name’]
$_REQUEST[‘contact_c’] = $contact_name;

I’ve also gone about it by creating a custom vardef - \custom\Extension\modules\Opportunities\Ext\Vardefs\vardef.ext.php
$dictionary[‘contacts’][‘fields’][‘contact_name’][‘populate_list’] = array(‘name’, ‘id’, ‘contact_name’);
$dictionary[‘opportunities’][‘fields’][‘contact_name’][‘field_list’] = array(‘contact_name’, ‘contact_id_c’, ‘contact_c’);

Finally, just for other testing purposes, I’ve put it right into opportunity.php using

$this->contact_c = $_REQUEST[‘contact_name’];

Any of these work for loading the contact name into my relate field. Clearly an event happens though upon putting focus onto that field and typing something because I can go into that field, delete the last letter on the name, put it back on, and the contact is found.

Any advice?

Thanks.

Check this post

https://community.sugarcrm.com/sugarcrm/topics/subpanel-auto-fill-relations

Best Regards

Thank you for the help. I tried the method described in the link by adding this function to Opportunity.php

function fill_in_additional_detail_fields() { 
	parent::fill_in_additional_detail_fields();
	if ($_REQUEST['target_action']=="QuickCreate"){
		//here we check if it is quick create
		$contact_id = $_REQUEST['contact_id'];
		
		if ($contact_id!=""){
			//if we are here it means that we create a record from opportunity
			$contact = new Contact();
			$contact->retrieve($contact_id);
			//you need to use inspect element in browser to find your names of account fields
			$this->contact_c = $contact->contact_name;
			$this->contact_id_c = $contact->contact_id;
		}
	} 
} 	

That did not work. Upon clicking Create on the Opportunity, I get a blank area underneath the Opportunities title (see attached image). I tried doing an R&R but that hangs at Rebuilding relationships. Comment out the function I added and things return to normal.

While over at the sugar forms I saw this topic and gave this a shot too.
https://community.sugarcrm.com/sugarcrm/topics/auto_fill_related_field_from_parent_module_to_child_module

New file create at custom/modules/opportunity/views/view.quickcreate.php

class OpportunityViewQuickcreate extends ViewQuickcreate
function preDisplay(){
parent::preDisplay();
if (isset($_REQUEST[‘parent_id’]) && $_REQUEST[‘parent_id’] != ‘’ && isset($_REQUEST[‘parent_type’]) && $_REQUEST[‘parent_type’] == ‘Contacts’){
$cont = new Contact();
$cont->retrieve($_REQUEST[‘parent_id’]);
//say the fields are company_id and company_name in Companies
//and company_id_c and company_name_c in Contracts
//assuming company is a relate field you’ll need to map both
$_REQUEST[‘contact_id_c’] = $cont->contact_id;
$_REQUEST[‘contact_c’] = $cont->contact_name;
}
}
}

Thanks again.

EDIT: typo

Building off of the link that you did send me though I did get it working but it isn’t upgrade safe. I added one more line to Opportunity.php

$this->contact_id_c = $_REQUEST['contact_id'];

This made it work.

Sorry for not converting the earlier post to code. I just realized it wasn’t showing properly.

Here is the first bit of code I tried. This prevented anything from happening.

function fill_in_additional_detail_fields() { 
	parent::fill_in_additional_detail_fields();
	if (@$_REQUEST['target_action']=="QuickCreate"){
		//here we check if it is quick create
		$cont_id = @$_REQUEST['contact_id'];

		if ($contact_id!=""){
		//if we are here it means that we create a record from opportunity
			$cont= new Opportunity();
			$cont->retrieve($cont_id);
			//you need to use inspect element in browser to find your names of account fields
			$this->contact_c = $cont->contact_name;
			$this->contact_id_c = $cont->contact_id;
			}
		}
}

Here is the code for the view.quickcreate.php file that I also tried. This did nothing.


class OpportunityViewQuickcreate extends ViewQuickcreate
  function preDisplay(){
    parent::preDisplay();
	echo "view.quickcreate.php";
    if (isset($_REQUEST['parent_id']) && $_REQUEST['parent_id'] != '' && isset($_REQUEST['parent_type']) &&  $_REQUEST['parent_type'] == 'Contacts'){
         $cont = new Contact();
         $cont->retrieve($_REQUEST['parent_id']);
         //say the fields are company_id and company_name in Companies
         //and company_id_c and company_name_c in Contracts
         //assuming company is a relate field you'll need to map both
         $_REQUEST['contact_id_c'] = $cont->contact_id;
         $_REQUEST['contact_c'] = $cont->contact_name;
    }
  }
}

Hi beasly,
I am facing exactly the same problem as you did.
In the quotes module I have a few custom fields that are getting populated from the other modules.
For example I have a “relate” field for accounts that I want to get pre populated.
When I’m creating the quotes from opportunity module, I wrote some custom code to get the account relate field pre-populated
but upon saving, it says no match found and exactly like you did, I tried to edit the content and then it was able to find it.
I went through the code that you’ve posted, But I couldn’t understand it.
Can I shoot my questions?

Thanks!

Any chance of you posting what solution you found so others can learn from this please??

It appears that the preDisplay function is skipped for Quickcreate form from subpanels, but cant find a reason why. See my post on the Sugarcrm forum also:
https://community.sugarcrm.com/message/80247

Found a way round it, skip using the preDisplay function, and just put the “pre fill field” code in the display function instead, but before calling parent::display()

Still doesnt make sense why preDisplay is not firing for subpanel forms.

Late reply but with a solution for everyone in that case, just follow whats on that thread :

I have tried this to auto-populate flex relate from parent however it only inserts plain text. Any suggestions?

<?php

require_once('include/EditView/SubpanelQuickCreate.php');

class CasesSubpanelQuickcreate extends SubpanelQuickCreate {

    public function CasesSubpanelQuickcreate() {

        $_REQUEST['parent_name'] = 'parent_id';

        parent::SubpanelQuickCreate("Cases");

    }

}

SuiteCRM 7.11.10

Hi
are you still looking for the same?, then I would like to share my knowledge as per your question I understand that you are trying to create a custom field contact module in the opportunity module, Right?

No, I wanted my flex relate field to be auto populated by parent.
I have achieved this by doing this:

<?php
require_once('include/EditView/SubpanelQuickCreate.php');

class CasesSubpanelQuickcreate extends SubpanelQuickCreate {

public function CasesSubpanelQuickcreate() {
    $_REQUEST['parent_type']=$_REQUEST['return_module'];
    $_REQUEST['parent_id']=$_REQUEST['return_id'];
    $_REQUEST['parent_name']=$_REQUEST['return_name'];
    parent::SubpanelQuickCreate("Cases");
}
}

File is located at custom/modules/Cases/views/view.subpanelquickcreate.php

hey @Urvi I’ve got a couple of questions… does this still work (7.13)? Also, I’m unclear about the file path. In my case I want to populate some fields in the Tasks subpanel of the Quotes module. Do I need to put this in…/AOS_Quotes/… or …/Tasks/… ?

What I’m trying to do is populate the account name in the Task subpanel of the Quotes module. So when I create a task from the quote, the Quote relationship is already populated by default. I just want to be able to populate the account that’s related to the quote. Its stored in the quote as billing_account