Hi there! Want
to see some
related articles?

Changing the entity owner based on custom logic

The Gravity Forms Dynamics CRM Add-On includes great support for sending leads, contacts, cases, and more from WordPress to Dynamics CRM and Dynamics 365.

Support is built-in for selecting a designated lead, contact, or case owner. However, sometimes you may want to customize the logic to assign an owner based on data in the form.

Fortunately, this is easily achieved with a short snippet of code that you can customize to meet your needs by hooking into the gform_dynamicscrm_lead, gform_dynamicscrm_contact, and gform_dynamicscrm_case hooks exposed by the plugin.

In the following example, we'll use an scenario from a real customer who wanted to assign leads based on the value in a "Location" field on their form. This customer had an international customer base and wanted to assign new leads to an appropriate user in the appropriate region. This example has been simplified for sake of this article, but the concepts are the same.

Visitors who select "Canada" from the location field should be assigned to the "[email protected]" Dynamics user as their lead owner. Visitors who select "USA" from the location field should be assigned to the "[email protected]" Dynamics user. Finally, visitors who select "International" from the location field should be assigned to the "[email protected]" Dynamics user.

The code snippet below is all that's needed to achieve this.

function gfdcrm_custom_lead_owner_logic( $lead, $feed, $entry, $form, $form_id ) {

	// Ensure we have the system user list from Dynamics
	$gfdcrm = gf_dynamics_crm();
	$dynamics_users = $gfdcrm->get_users_for_feed_setting();

	// Mapping of locations to user email addresses
	// IMPORTANT: change these values to match your settings
	$lead_location_to_owner_mapping = array(
		'Canada' => '[email protected]',
		'USA' => '[email protected]',
		'International' => '[email protected]',
	);

	// First, retrieve the selected location from the passed form entry (field ID: 16)
	// IMPORTANT: change to the correct field ID on your form)
	$selected_location = $entry['16'];

	// Get the lead owner that should be assigned
	$assigned_lead_owner_email_address = $lead_location_to_owner_mapping[$selected_location];

	// Now, find that lead owner in the System User list and filter based on DomainName
	$assigned_lead_owner = $current_field = current( array_filter( $dynamics_users, function( $system_user ) use( $assigned_lead_owner_email_address ) {
		return preg_match( '('. $assigned_lead_owner_email_address .')', $system_user['label'] );
	} ) );

	$lead['OwnerId']['Id'] = $assigned_lead_owner['value'];

	$gfdcrm->log_debug( __METHOD__ . ':' . print_r( $lead, true ) );

	return $lead;

}
// Custom lead logic for Form ID(6)
// IMPORTANT: change the _6 suffix to match the ID of your form
add_filter( 'gform_dynamicscrm_lead_6', 'gfdcrm_custom_lead_owner_logic', 10, 5 );

There are three lines marked with IMPORTANT that indicate the areas you need to modify to meet your requirements.

  1. First, inside the gfdcrm_custom_lead_owner_logic function, we define a $lead_location_to_owner_mapping variable which is a simple array mapping locations to the corresponding email addresses of the Dynamics users. Change these values to match your settings.
    	$lead_location_to_owner_mapping = array(
    	'Canada' => '[email protected]',
    	'USA' => '[email protected]',
    	'International' => '[email protected]',
      );
    
  2. Next, we retrieve the value selected for the location field:
    	$selected_location = $entry['16'];
    On our form, field ID 16 corresponds to the Location field. Change this ID to match the ID of your desired form field.
  3. Finally, on the last line in the add_filter call, specify the desired filter for your scenario. In this case, we are using the gform_dynamicscrm_lead filter which fires before a lead is sent to the Dynamics API. Specifically, we are using a more specific version of that filter ( gform_dynamicscrm_lead_{form_id}) which allows us to target submissions for a specific form id (in this case, form ID 6).
    	add_filter( 'gform_dynamicscrm_lead_6', 'gfdcrm_custom_lead_owner_logic', 10, 5 );
    We could simply leave this as gform_dynamicscrm_lead which would make it run for all forms. However, by adding the _{form_id} suffix, we can target the specific form we want and leave any others alone that don't require this functionality.

Using the power of Gravity Forms Dynamics CRM and a little custom code you can achieve fine-grained control over entity ownership for your Dynamics data coming from WordPress.