How to set field value in another record in NetSuite using SuiteScript

81 Views Asked by At

I deployed a SuiteScript code to netsuite and applied to invoice that upon being created, will set field values in the sales order it was created from. But the code doesn't have any effect on the sales order record neither does any error pop up.

function afterSubmit(context) {
        var curRec = context.newRecord;
        //internal id of the invoice
        var ciId = curRec.id;
        //internal id of the sales order I want to set value in
        var piId = curRec.getValue(
            {
                fieldId: 'createdfrom'
            }
        );

        var loadPI = record.load(
            {
                type: record.Type.SALES_ORDER, 
                id: piId,
                isDynamic: true,
            }
          );
          // the field in the sales order that I want to change
          loadPI.setValue(
            {
                fieldId: 'custbody55',
                value: true
            }
          );
}
return{afterSubmit: afterSubmit};
2

There are 2 best solutions below

0
d.k On BEST ANSWER

You probably have to save the record.

Call: loadPI.save();, namely (your code updated):

function afterSubmit(context) {
  var curRec = context.newRecord;
  //internal id of the invoice
  var ciId = curRec.id;
  //internal id of the sales order I want to set value in
  var piId = curRec.getValue(
    {
      fieldId: 'createdfrom'
    }
  );

  var loadPI = record.load(
    {
      type: record.Type.SALES_ORDER,
      id: piId,
      isDynamic: true,
    }
  );
  // the field in the sales order that I want to change
  loadPI.setValue(
    {
      fieldId: 'custbody55',
      value: true
    }
  );
  loadPI.save();
}
return{afterSubmit: afterSubmit};
0
Krypton On

As d.k says, you would need to save the loaded record for the changes to be persisted into the database. However, there is a more efficient way than loading, modifying and saving the record if you're only updating a body level field on the sales order. That is to use record.submitFields().

function afterSubmit(context) {
    var curRec = context.newRecord;
    //internal id of the invoice
    var ciId = curRec.id;
    //internal id of the sales order I want to set value in
    var piId = curRec.getValue(
        {
            fieldId: 'createdfrom'
        }
    );

    record.submitFields({
        type: record.Type.SALES_ORDER,
        id: piId,
        values: {
            custbody55: true
        }
    });
}
return{afterSubmit: afterSubmit};

In addition to being more performant and efficient, this uses half the governance points versus load, modify, save.