hi im trying to have a script run on all item type...
# suitescript
s
hi im trying to have a script run on all item types kit , inventory , lot inventory etc this is my current code
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'],
    /**
     * @param {record} record
     */
    function (record) {
        function afterSubmit(scriptContext) {
            //Step 1
            rec = scriptContext.oldRecord;

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

            //Step 3
            if (amazonsku != extId) {
                //Step 4
                record.submitFields({
                    
                    id: rec.id,
                    values: {
                        'externalid': amazonsku
                    }
                });
            }
            //Step 5
            var newext = rec.getValue({ fieldId: 'externalid' });
            log.debug('rec id', rec.id)
        }
        return {
            afterSubmit: afterSubmit
        };
    });
s
Dont use record.submitFields, get this information in beforeSubmit and just set it on the record.
1000 1
s
i got this basic code from suite answers to use after submit
Answer Id: 80814
b
You dont really have a choice in the matter
s
meaning
b
You cant edit the external id in beforeSubmit
s
Really? I had never tried it, but didn’t think it would be prohibited.
s
in record.submitFields do i have to set the record type
s
yes, type is required to use that function
s
i have multible types inventory , lot inventory assembly
s
try using
rec.type
?
💯 1
s
yes but i need to set that dynamically
b
although im not sure why you would use the old record vs the new record, use Record.type
s
good points im going to rewrite this script !
s
You would be getting the type property from the current record (old or new)
s
scottvonduhn is right - rec.type in your code =
Copy code
scriptContext.oldRecord.type
this will give you the record type that battk is talking about - which will make your code dynamic then you will need to do
Copy code
record.submitFields({
                    type:rec.type,
                    id: rec.id,
                    values: {
                        'externalid': amazonsku
                    }
                });
s
so shoul iuse old record or new record ?
s
almost always it's new record, in this case it doesn't matter*, but do note that when the fields are not changed sometimes it might not be available in context.newRecord there are a few workarounds, but that's a different topic *if amazonsku won't be updated
if your amazonsku can be updated, you should use newRecord, or you'll create a bug
s
thanks guys script works now
will this script work whether it is a edit or create ?
b
Depends what you wrote for your version
s
Well create might not have an
oldRecord
so if you are referencing that, could definitely be a problem
s
but this is a after submit script so shouldn't it have a record ?
b
nope
old record represents the record before you submit the new record
there is no old record when you create a new one
the first lines of your user event script usually add logic to filter
scriptContext.type
if there is only one type you want to work with, you can do it by setting the
Event Type
on the script deployment
i await the day they make Event Type a multi select