Resolve 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 a different type than the value to set to.

Solution

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.

// retrieve  current Case Number
var casenum = currentRecord.getValue({‌
    fieldId: 'casenumber'
});
// create the new Sales Order
var salesord = record.create({‌
    type: record.Type.SALES_ORDER
});
// The memo field can be updated with the Case Number
salesord.setValue({‌
    fieldId: 'memo',
    value: casenum
});
//But Invalid custbody_xxx reference key xxx is thrown when setting the custom field
salesord.setValue({‌
    fieldId: 'custbody_linked_case',
    value: 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).

//define filters to search the Case details by case number
var filters = new Array();
filters[0] = search.createFilter({‌
    name: 'casenumber',
    operator: search.Operator.IS,
	values:	casenum
});
//search for case by Number
var result =  nlapiSearchRecord('supportcase',null, filters);
var mySearch = search.create({‌
	type: search.Type.SUPPORT_CASE,
	filters: filters
});
if(result != null)
{‌
//retrieve the Case Internal ID
var currentCaseID  = result[0].getId();
//set the value of the Case in the List/Record field
salesord.setValue({‌
    fieldId: 'custbody_linked_case',
    value: currentCaseID
});
}

Leave a comment

Your email address will not be published. Required fields are marked *