Shahriar
Shahriar

Reputation: 15

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

Upvotes: 1

Views: 696

Answers (2)

Krypton
Krypton

Reputation: 5231

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.

Upvotes: 2

d.k
d.k

Reputation: 4460

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

Upvotes: 2

Related Questions