SugarCRM: How to save Parent information on Quick Create

This are the steps on how to save the Parent data when a record is saved in Quick Create form. I used the Accounts module as the parent and I will create a new Contact record using the Quick Create.

What I want is upon clicking the Save button, the Billing address of the parent Account will also be saved on the Primary address of the new Contact record.

1.  Go to modules/Accounts/metadata/subpaneldefs.php and find the settings for the Contacts under the subpanel_setup. Look at the example below:

'contacts' => array(
			'order' => 30,
			'module' => 'Contacts',
			'sort_order' => 'asc',
			'sort_by' => 'last_name, first_name',
			'subpanel_name' => 'ForAccounts',
			'get_subpanel_data' => 'contacts',
			'add_subpanel_data' => 'contact_id',
			'title_key' => 'LBL_CONTACTS_SUBPANEL_TITLE',
			'top_buttons' => array(
				array('widget_class' => 'SubPanelTopCreateAccountNameButton'),
				array('widget_class' => 'SubPanelTopSelectButton', 'mode'=>'MultiSelect')
			),

In the example the SubPanelTopCreateAccountNameButton is used when the Create button is clicked.

* If the module for quick create is custom, go here.

2. Go to the SubPanelTopCreateAccountNameButton file. You can find it in include/generic/SugarWidgets/SugarWidgetSubPanelTopCreateAccountNameButton.php

3.  In the display function, input the following codes below:

// This will hold the additionalData
$additionalFormFields = array();

// Check if billing address street has value
if(isset($defines['focus']->billing_address_street))
{
       // Assign the billing address street to the array
       // The key you will use will be the id and name of the hidden field
       $additionalFormFields['primary_address_street'] = $defines['focus']->billing_address_street;
}

// Do the codes above for the other fields

// Create the Save button
$button = $this->_get_form($defines, $additionalFormFields); // This creates the hidden fields with the additional data that we added
$button .= "<input id="{$this->getWidgetId()}" title="$title" type="submit" name="{$this->getWidgetId()}" value="  $value  " />\n";
$button .= "</form>";
return $button;

* go to the Create button then inspect the code and you should see the hidden fields with the additional fields that we added

4. Add the logic hook that will catch this process. Edit custom/modules/Contacts/logic_hook.php and add the following code below if it doesn’t exist yet:

$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, '<description>', '<file where to call the function>','<class name>', '<function name>');

5. Write the following code on the function you call for the logic_hook:

// Check if the function was called by the Accounts module
if ($_REQUEST['return_module'] == "Accounts")
{
     // Get the value by using $_REQUEST then assign it to the appropriate field
     $bean->primary_address_street = $_REQUEST['primary_address_street'];
}

6. Run Quick Repair and Rebuild.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s