I'm sure this is easy, but I don't know SS very we...
# suitescript
r
I'm sure this is easy, but I don't know SS very well. I repurposed another working script to try to do the same thing (Set Ext ID equal to a custom field value on the record) on a different record type and it's not working. When I save the record in the UI, I get "Record has been changed" error and the value does not get set to Ext ID. The Ext ID has been set, but it's a bunch of gibberish vs the data I'm trying to set it to. On the SS Execution Log, it says just "Set Ext ID of standard record.", which I assume is coming from the line in the code, but it shows as Type=Audit and not error.
Copy code
define(["N/record", "N/log"],

function(record, log) {
    function setItemExternalID(context){
      var itemRecord = context.newRecord;

      try{
        var itemField = itemRecord.getValue({ fieldId: 'cust_field_reference_i_triple_checked_is_correct'});
        
        if(itemField){
            record.submitFields({
                type: record.Type.NON_INVENTORY_ITEM,
                id: itemRecord.id,
                values: {
                    externalid: itemRecord
                }
            });
            log.audit({title: 'Set External ID of ' + itemRecord});
        }
      }
      catch(e){
        log.error(e);
        log.error(e.message);
        log.error(e.stack);
      }
    }

    function beforeSubmit(context) {
      setItemExternalID(context);
    }
    return {
        beforeSubmit: beforeSubmit
    };
});
The Ext ID is set to this:
Copy code
{removeSubrecord=com.netledger.app.common.scripting.gen.api.rhino.v2x.proxy@2588c77f, hasSubrecord=com.netledger.app.common.scripting.gen.api.rhino.v2x.proxy@780b269, save=com.netledger.app.common.scripting.gen.api.rhino.v2x.proxy@5f146dde, type=noninvent
Which is definitely not the value of cust_field_reference_i_triple_checked_is_correct
p
you are setting the
externalid
with the value of
itemRecord
and not
itemField
r
facepalm
Thanks @Patrick A.. I swear I looked at this at least 10 times and didn't catch that. So it is setting the value properly now, but I still get a "Record has been changed" error when saving, even though it is setting the Ext ID value properly now. Something to do with the function and return clauses at the end maybe? In the script I swiped this code from it was set to the below and I just changed all the afters to befores
Copy code
function afterSubmit(context) {
      setAccountExternalID(context);
    }
    return {
        afterSubmit: afterSubmit
    };
b
before submit occurs before the record has been saved
your code modifies the record before the original is saved, giving you your record modification error since the original is now out of sync
💯 1
☝️ 2
n
You should consider keeping the script running before submit and using itemRecord.setValue(). This is significantly more performant than record.submitFields(). (Do not add itemRecord.save() though as this is automatically run by default after before submit scripts run.)