Monday, February 18, 2019

Re-assign a Task to a Specific Employee Based on a Custom Criteria via SuiteScript

To re-assign a task to a specific employee based on a custom criteria, we can create a custom record that would contain the criteria and then use a user event script triggered on Before Submit to check the criteria via a custom record search.

Steps

1. Create a custom recordtype that would contain the rules.(Customization> Lists, Records, & Fields> Record Types> New)
  • Employee (Type = List/Record, List/Record = Employee)
  • Name of Region (Type = List/Record, List/Record = Subsidiary)
  • Region Countries (Type = Multiple Select, List/Record = Country)
  • Region States (Type = Multiple Select, List/Record = State)

2. Create a user event script triggered on Before Submit and deployed on tasks records. (Customization> Scripting> Scripts> New

Note:  Below is the sample script that checks the contact's address and assigns the task based on the rules set on custom record created above.

// Employee  - custrecord158
// Name of Subsidiary - custrecord159
// Region Countries - custrecord160
// Region States - custrecord161

function beforeSubmit(type) {
 // Get the id on the contact and load the contact record
 var contactId = nlapiGetFieldValue('contact');
 var contactRec = nlapiLoadRecord('contact', contactId);

 // Check the address of the contact and get the country, state and subsidiary
 var cCountry = contactRec.getLineItemValue('addressbook', 'country', 1);
 var cState = contactRec.getLineItemValue('addressbook', 'state', 1);
 var cSubsidiary = contactRec.getFieldValue('subsidiary');

 // Create filters to check which Sales rules would match the Contact's address and subsidiary
 // Note: custrecord159, custrecord160 &  custrecord161 are internal ids of the fields (Name of Region, Region Countries & Region States) in the custom record created for the Rules.
 var filterExpression = [
   [ 'custrecord160', 'anyof', cCountry ],
   'and',
   [ [ 'custrecord161', 'anyof', cState ], 'or',
     [ 'custrecord161', 'anyof', '- None -' ] ],
   'and',
   [ [ 'custrecord159', 'anyof', cSubsidiary ], 'or',
     [ 'custrecord159', 'anyof', '- None -' ] ] ];

 // Create the columns or rows to be returned for the search.
 // Note: custrecord158 is the internal id for Employee field in the custom record created for the Rules.
 var arrSearchColumns = new Array();
 arrSearchColumns[0] = new nlobjSearchColumn('custrecord158');

 // Execute the search
 var arrSearchResults = nlapiSearchRecord('customrecord970', null ,filterExpression, arrSearchColumns);

 // Set the assigned to field to the Employee based on the rules that match.
 nlapiSetFieldValue('assigned', arrSearchResults[0].getValue('custrecord158'));
}

No comments:

Post a Comment