Friday, January 25, 2019

Create a Link/s to Sales Orders Related to an Opportunity Stored on a Custom Field within a Custom Record

To create a link to sales orders related to an opportunity stored on a custom field within a Custom Record, we need to create a custom field of type, Multiple Select that is sourced to List / Record of Transaction records. We can then create a user event script triggered on Before Record Submit that sets the value of the custom field to the sales order related to the opportunity contained on another custom field.

Steps to create Custom Field.

1. Customize the custom record type. (Customization> Lists, Records, & Fields > Record Types)
2. Add a new field and set the following details.

 a. Label - "Opp Sales Order"
 b. Type - Multiple Select
 c. List/Record - Transaction
 d. Store Value - Checked

Steps to create User Event Script.

1. Create a User Event script. (Customization > Scripting > Scripts > New)
2. Load the sample script below and set the function to the "Before Submit Function".

function beforeSubmit(type) {

 nlapiLogExecution('DEBUG', 'Opp Related Sales Order', 'Start');

 // Check if 'custrecord34' contains a value. 'custrecord34' is the field that contains the opportunity.
 if (nlapiGetFieldValue('custrecord34') != null || nlapiGetFieldValue('custrecord34') != '') {

  // Search for sales order that was created from the opportunity
  var arrSearchFilters = new Array();
  arrSearchFilters[0] = new nlobjSearchFilter('createdfrom', null,'anyof', nlapiGetFieldValue('custrecord34'));
  arrSearchFilters[1] = new nlobjSearchFilter('mainline', null, 'is', 'T');

  var arrSearchColumns = new Array();
  arrSearchColumns[0] = new nlobjSearchColumn('internalid');

  var arrSearchResults = nlapiSearchRecord('salesorder', null, arrSearchFilters, arrSearchColumns);

  var values = new Array(); // Initialize an array that would contain the values of the internal ids of the sales orders

  for ( var i in arrSearchResults) {
   var searchResults = arrSearchResults[i];
   nlapiLogExecution('DEBUG', 'test', searchResults.getValue('internalid'));
   values[i] = searchResults.getValue('internalid');
  }
  nlapiSetFieldValue('custrecord35', values); // custrecord35 is the internal id of the multi select custom field

 }

}

No comments:

Post a Comment