How do I Auto Populate a Custom Relate Field when Creating a Record from a Subpanel?

Hi all,

I'm attempting to populate a custom relate field (primary_contact_c) when creating an opportunity from a contact using the subpanel; the relate field in the opportunity should be populated with the contact that it is being created from.

I've found a few articles on here and on other sites which ask similar things but I've not made much progress with this.

Any help would be very much appreciated.

Many thanks in advance.

  • You need to create an extended vardefs for Contacts in order to update accordingly the attribute 'populate_list' in the field 'opportunities'. You have to add a new pair from -> to in the populate_list with respect to the field to be copied from parent Contact to child Opportunity.

    Remember to run Quick Repair and Rebuild after saving the extended vardefs.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • When we create a relationship between 2 modules a related field is automatically generated. The beauty of the "Relate field" which is generated by relationship is that we didn't need to populate it by own, It will populate automatically.

    https://support.sugarcrm.com/Knowledge_Base/Studio_and_Module_Builder/Introduction_to_Relationships_and_Relate_Fields/

  • Thanks, Andre and Maryam for your responses.

    I'm not sure if this is considered to be basic stuff but I was eventually able to work this out by adding the below code to the vardef found at custom\extension\modules\Opportunities\Ext\Vardefs\sugarfield_primary_contact_c.php

    $dictionary['Opportunity']['fields']['primary_contact_c']['name']='primary_contact_c';

    $dictionary['Opportunity']['fields']['primary_contact_c']['rname']='name';

    $dictionary['Opportunity']['fields']['primary_contact_c']['id_name']='contact_id_c';

    $dictionary['Opportunity']['fields']['primary_contact_c']['vname']='LBL_PRIMARY_CONTACT';

    $dictionary['Opportunity']['fields']['primary_contact_c']['type']='relate';

    $dictionary['Opportunity']['fields']['primary_contact_c']['table']='contacts';

    $dictionary['Opportunity']['fields']['primary_contact_c']['join_name']='contacts';

    $dictionary['Opportunity']['fields']['primary_contact_c']['isnull']=true;

    $dictionary['Opportunity']['fields']['primary_contact_c']['module']='Contacts';

    $dictionary['Opportunity']['fields']['primary_contact_c']['dbType']='varchar';

    $dictionary['Opportunity']['fields']['primary_contact_c']['link']='contacts';

    $dictionary['Opportunity']['fields']['primary_contact_c']['len']='255';

    $dictionary['Opportunity']['fields']['primary_contact_c']['source']='non-db';

    $dictionary['Opportunity']['fields']['primary_contact_c']['unified_search']=true;

    $dictionary['Opportunity']['fields']['primary_contact_c']['required']=true;

    $dictionary['Opportunity']['fields']['primary_contact_c']['importable']='required';

    $dictionary['Opportunity']['fields']['primary_contact_c']['required']=true;

    $dictionary['Opportunity']['fields']['primary_contact_c']['related_field']= array('contact_id_c');

    $dictionary['Opportunity']['fields']['primary_contact_c']['exportable']=true;

    $dictionary['Opportunity']['fields']['primary_contact_c']['export_link_type']='one';

    This pre-populates the relate field in opportunities with the contact name when creating an opportunity from that contact but the link tries to take you to the the opportunity rather than the contact.

    With that in mind I also tried to amend an existing vardef in the same folder which was sugarfield_contact_id_c and added in the below code:

    $dictionary['Opportunity']['fields']['contact_id_c']['name']='contact_id_c';
    $dictionary['Opportunity']['fields']['contact_id_c']['vname']='LBL_CONTACT_ID';
    $dictionary['Opportunity']['fields']['contact_id_c']['id_name']='contact_id';
    $dictionary['Opportunity']['fields']['contact_id_c']['type']='relate';
    $dictionary['Opportunity']['fields']['contact_id_c']['link']='contacts';
    $dictionary['Opportunity']['fields']['contact_id_c']['rname']='id';
    $dictionary['Opportunity']['fields']['contact_id_c']['source']='non-db';
    $dictionary['Opportunity']['fields']['contact_id_c']['audited']=true;
    $dictionary['Opportunity']['fields']['contact_id_c']['dbType']='id';
    $dictionary['Opportunity']['fields']['contact_id_c']['module']='Contacts';
    $dictionary['Opportunity']['fields']['contact_id_c']['massupdate']=false;

    I'm sure there are improvements that can be made on all of the above but this seems to work and I hope it helps anyone who has the same issues.

  • If instead of standard relate field type you instead use the Flex Relate field type, and create the Opportunity from the Contact subpanel, it will automatically populate the Contact for you. Relaxed

    As an alternative without using any relate fields, the Opportunity created this way already stores the related Contact as you can see if you run a report on Opportunities and add the Contact name to the report layout.
    Therefore, there must be a way to 'expose' this via code and not resort to 'duplicating this info'? 

    .

    CRM Business Consultant