Search code examples
netsuitesuitescriptsuitescript2.0

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


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};

Solution

  • 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};