I'm copying the UPC to the External ID of Inventor...
# suitescript
l
I'm copying the UPC to the External ID of Inventory which is not shown on the form. The script below gets the value but it doesn't set it. No error encountered. Thanks!
m
Need to set external id on after submit
l
I tried that already and still no luck.
m
If it's an aftersubmit you would need to save the record again. It would be a better idea to do "inline edit" (record.submitFields) instead
s
i have simalliar script hold on let me check
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'],
    /**
     * @param {record} record
     */
    function (record) {
        function afterSubmit(scriptContext) {
            //Step 1
            rec = scriptContext.newRecord;

            //Step 2
            var extId = rec.getValue({ fieldId: 'externalid' });
            var amazonsku = rec.getValue({ fieldId: 'custitem3'  });
           var amazonfba = rec.getValue({ fieldId: 'custitem_fa_amz_fba01' });

            //Step 3 TODO make sure only amazon fba 
            if (amazonsku != extId && amazonfba === true) {
                //Step 4
              var amzsku= amazonsku.trim()
                record.submitFields({
                    type: rec.type,
                    id: rec.id,
                    values: {
                        'externalid': amzsku
                    }
                });
            }
            //Step 5
            
            log.debug({title: "test",
                      details:amazonfba})
        }
        return {
            afterSubmit: afterSubmit
        };
    });
you cant use setvalue on after submit , --it is after the server submitted so you need to use
Copy code
record.submitFields()
l
Thanks everyone! I compared my afterSubmit version to yours @Sim Greenbaum after comparing the two I found out that I just had the wrong if condition. 🤦‍♂️ Thanks a lot! Is it possible to ignore the setting of External ID if it is a duplicate?
m
NetSuite won't allow you to set a duplicate
You could either check for this yourself or wrap in a try/catch and ignore the error
l
Yes, there's an error. If I use try and catch, will I be able to save the record but not set the duplicate External ID? Thanks!
m
If you are in an aftersubmit the record has already been saved, but a try/catch will stop the error from displaying to user
l
Makes sense! It worked, ofc. Thanks again!
b
setting the external id is the kind of thing i would recommend a map reduce script or scheduled script
m
why is that?
b
you dont need to slow down the item by saving it again in the after submit
✔️ 1
its unlikely that something will need that external id realtime
✔️ 1
m
makes sense
s
Yes and no this is one time thing for an item a map reduce would check whole catlog
m
The map reduce could just look search for items which external id doesn't match the field in getInputData stage and only update those
s
Still searching whole database and this is one record
b
the idea is to make the map/reduce script search for items whose external id does not match your field. that would make the map (or reduce) function only run on records that need updating. The advantage being that the external id update would be performed at a more opportune time. The after submit solution will make whatever is waiting for the save to complete wait for the external id update
s
I guess there are benefits to both ways , and downside
b
get started on the performance constraints early. your account has less performance to spare the larger and more customized it gets
arguably it has no performance to spare