Sunday, February 17, 2019

Sample code for copying the Parent Record along with the Child Record using nlapiCopyRecord API

nlapiCopyRecord returns the nlobjRecord object based on the record type the user is copying. Thus, he/she utilizes nlapiCopyRecord to copy a single record type, it is expected that it won't copy the child records as well.

To properly copy the child records, one must add another line for nlapiCopyRecord to instantiate another nlobjRecord object that references the child record's record type. After that, use setFieldValue() method to set the field that connects them.

Here is the sample snippet that copies a Project record along with its child record - Project Task. Since the field that connects the Project Task to its parent record is the 'company' field, setFieldValue() for this field was used.

var recProj = nlapiCopyRecord('job', 1244, null)
recProj.setFieldValue('companyname','Sample Project Copy');
var recProjId = nlapiSubmitRecord(recProj);
var recProjTask = nlapiCopyRecord('projecttask',1286);
var recProjTaskId = nlapiSubmitRecord(recProjTask);

No comments:

Post a Comment