Friday, April 12, 2019

Script Error Invalid custbody_xxx reference key xxx when setting the Field Value of a List/Record custom field

Error is thrown when the value that the user is trying to set cannot be found among the IDs collection of that record or the field to set has different type than the value to set to.

For example: Sales Order is generated from Case record using script. A custom field Linked Case Number of type List/Record from Case has been defined on Sales Order to store the Case from which the Sales Order has been generated.
User would like to set the appropriate Case Number in the list on the Sales Order through script but gets Invalid custbody_xxx reference key xxx error.

Script code sample:
// retrieve  current Case Numbervar casenum = nlapiGetFieldValue('casenumber');// create the new Sales Order var salesord  = nlapiCreateRecord('salesorder');// The memo field can be updated with the Case Number salesord.setFieldValue('memo', casenum); //But Invalid custbody_xxx reference key xxx is thrown when setting the custom field salesord.setFieldValue('custbody_linked_case', casenum)

When setting the value for a custom field of type List/Record we need to keep in mind that nlapiSetFieldValue() expects the ID (Value) of the record that needs to be set rather than the Text displayed on the screen upon selection.
Case Number is not the Internal ID of the Case expected by the function nlapiSetFieldValue('caseID', recordID).

The respective ID can be retrieved by changing the line

salesord.setFieldValue('custbody_linked_case', casenum) ;
With a code similar to:
//define filters to search the Case details by case number var filters = new Array();filters[0] = new nlobjSearchFilter( 'casenumber', null, 'is', casenum);//search for case by Numbervar result =  nlapiSearchRecord('supportcase',null, filters);if(result != null){//retrieve the Case Internal IDvar currentCaseID  = result[0].getId();//set the value of the Case in the List/Record fieldsalesord.setFieldValue('custbody_linked_case', currentCaseID);}

No comments:

Post a Comment