Let the platform do the work

Best Practices

Overview

Best practices when integrating and migrating Sugar.

Latency When Posting Data To Sugar

When integrating with Sugar, it is best to avoid long-running web requests. While performance-draining requests are not always obvious, once an issue has been identified, it is best to move the processing to the backend.

By default, we expect a request to an endpoint such as POST /Accounts to be straightforward, however, adding Workflows, Logic Hooks, and Sugar Logic may stress the otherwise simple process and cause issues with webheads and other resources being used in the process. If you are experiencing these symptoms, the following sections may help you.

When a calculated field in Sugar uses the related function in the Sugar Logic, this will cause the calculated field to be executed when the related module is updated. This can cause a cascading effect through the system to update related calculated fields. When this happens you may receive a 502 Gateway Error. You can disable the related calculation field updates temporarily or permanently by adding the following line to the config_override.php file:

  $sugar_config['disable_related_calc_fields'] = true;

Note : This is a global setting that will affect all modules. If you have a calculated field in Accounts that sums up all Opportunities for the account, setting this value to true will no longer update the opportunity account sum in Accounts until the account record itself is modified. However, if this setting is left disabled, the sum would update any time a related opportunity or the account is modified.

More information on this setting can be found in the core configuration settings.

Disabling Logic Hooks

When data is being migrated into Sugar, logic hooks may be adding unnecessary time to your API requests. It is highly recommended for you to disable any unnecessary logic hooks from your system during an initial import. Logic hook definitions may be located in the following files and/or directories:

  • ./custom/modules/logic_hooks.php
  • ./custom/modules/<module>/logic_hooks.php
  • ./custom/Extension/application/Ext/LogicHooks/
  • ./custom/Extension/modules/<module>/Ext/LogicHooks/

Disabling Workflows

When data is being migrated into Sugar, workflows may be adding unnecessary time to your API requests. It is highly recommended for you to disable any unnecessary workflows from your system during an initial import in Admin > Workflows.

Queuing Data in the Job Queue

One solution for long running requests is to send the data to the Job Queue to be processed by the cron. To accomplish this, make a file customization to add to the target processing function. For this article's use case, create  ./custom/Extension/modules/Schedulers/Ext/ScheduledTasks/CustomCreateAccountJob.php and send any stored data to it for processing. Enter the following code in the php file:

  <?php

function CustomCreateAccountJob($job)
{
	if (!empty($job->data)) {
		$account = BeanFactory::newBean('Accounts');
		$fields = json_decode($job->data, true);

		foreach($fields as $field => $value) {
		    $account->$field = $value;
		}

		$account->save();

		if (!empty($account->id)) {
		     return true;
		}
	 }

	 return false;
}

Next, modify the request to pass the new account data to the SchedulerJobs module in the data field and specify the new function in the target field, as follows: 

  curl -v -H "oauth-token: 4afd4aea-df99-7cec-4d94-560a97cda9f8" -H "Content-Type: application/json" -X POST -d '{"assigned_user_id": "1", "name":"Queue Create Account", "status":"queued", "data":"{\"name\": \"Example Account\"}", "target":"function::CustomCreateAccountJob"}' http://<site_url>rest/v10/SchedulersJobs

This solution will queue data for processing and free up system resources to send more requests. For more information, please refer to the Scheduler Jobs documentation.

Topics