Tuesday, January 8, 2019

Steps to Attach Same Custom Child Records from One Record to Another through Scripting

If we have a custom record that is set as a child for 2 different record types and we want to automatically attach the same set of custom record from one to another. For example we have a lead record that has a custom record attached to it and we want to have that same set of custom records attached to a contact record.

How to attach same custom child records from one record to another through scripting


For details on creating custom child records and place it on a record as a sublist, refer to the following path in Help Center.
Custom Child Record Sublists
SuiteCloud (Customization, Scripting, and Web Services) : SuiteScript : SuiteScript Reference : Scriptable Sublists : Custom Child Record Sublists


Sample script below is set to attach the same set of custom records from a lead record to a contact record. This is especially useful if we want to retain the relationship of records when a lead record is converted to a contact. Script can be a client side or user event script that can be triggered on either "Save", "Before Record Submit" or "After Record Submit".

// Create a search either through a saved search that would be referenced on the script or set the filters and columns within the script.
// Search is for a custom record that has the 'Lead/Prospect/Customer' which would be equal to the value of the company field on the contact record.

// Declaration of the search filter object to be used on the search API
var arrSearchFilters = new Array();
arrSearchFilters[0] = new nlobjSearchFilter('custrecord_company', null, 'anyof', nlapiGetFieldValue('company'));
    
// Declaration of the search column object to be used on the search API
var arrSearchColumns = new Array();
arrSearchColumns[0] = new nlobjSearchColumn('internalid');
    
// Search for the custom record already attached to a lead record
var arrSearchResults = nlapiSearchRecord('customrecord724', null, arrSearchFilters, arrSearchColumns);
        
// Use a for loop to go through each of the search results and use the Internal Id of the custom record on the nlapiAttachRecord
for (var i in arrSearchResults) {
    
    var searchResults = arrSearchResults[i].getId(); // Gets the internal id of the campaign response and set it to the variable searchResults

    // Attach the record to the contact record
    nlapiAttachRecord('customrecord724', searchResults, 'contact', nlapiGetRecordId(), {'field': 'custrecord_contact'});       

}

No comments:

Post a Comment