```function setCancelledSOextID() { var record...
# suitescript
a
Copy code
function setCancelledSOextID()
{
    var record = nlapiLoadRecord(nlapiGetRecordType()), nlapiGetRecordId());
    how do i search for status of cancelled?
    nlapiSubmitField ("SalesOrder", record, "externalid", record + "_cancelled");
}
Hey all, I'm pretty new at this and I'm trying to write a trigger on Sales Order that sets external ID to append _cancelled to the end of the external ID. wondering if anyone can shine some light here. I think i'm way off base here The logic I want is basically when someone sets status to Cancelled, change external Id to internal id + "_cancelled".
b
first step is to lookup the status column
in your case, you probably want to lookup the external id at the same time
then you can submit the new fields
the code you have currently makes the general mistake of assuming that nlapiLoadRecord returns a string
it doesnt, it returns an object
a
Copy code
function setCancelledSOextID()
{
    var recordStatus = nlapiGetFieldText('Status');
    var externalidvalue = nlapiGetFieldText('externalid');
    var record = nlapiLoadRecord(nlapiGetRecordType()), nlapiGetRecordId());
    var newexternalid = record + "_Cancelled";
    if (recordStatus = "Cancelled" && externalid != newexternalid){
        nlapiSubmitField ("SalesOrder", record, "externalid", newexternalid);
    }
}
How does that look
oh crap, it doesn't return a string
one sec
b
i wouldnt expect it to work
take smaller steps
first step is getting the order status and external id
a
first step is done right?
Copy code
var recordStatus = nlapiGetFieldText('Status');
    var externalidvalue = nlapiGetFieldText('externalid');
b
its something you should be able to confirm for yourself
a
ok i finally got external id and status to populate as a string
function loadCurrent() { recordId = nlapiGetRecordId(); recordType = nlapiGetRecordType(); varrecord = nlapiLoadRecord(recordType,recordId); varexternalid = varrecord.getFieldValues('externalid'); varstatus = varrecord.getFieldValue('status'); return varexternalid + varstatus; } loadCurrent()
Copy code
This works 

function loadCurrent() {
    recordId = nlapiGetRecordId();
    recordType = nlapiGetRecordType();
    varrecord = nlapiLoadRecord(recordType,recordId);
    varinternalid = varrecord.getFieldValues('id'); 
    varexternalid = varrecord.getFieldValues('externalid'); 
    varstatus = varrecord.getFieldValue('status');
    newexternalid = varinternalid + '_Cancelled';
    if(varstatus = 'Cancelled' && varexternalid != newexternalid){
        varrecord.setFieldValue('externalid',newexternalid);
        nlapiSubmitRecord(varrecord);
        return 'Changed';
    }
    else{
        return 'No Change';
    }
}
loadCurrent()
nevermind, not working
the if statement is not working.
b
i cant really tell, your code looks like its skipping the space in
varinternalid
usually a properly setup code editor would tell you that skipping the var keyword is a terrible idea
a
ok i'll try it again with everything as var recordId, var recordtype etc..
the if statement is not working at all
b
you still probably want to get a proper ide with eslint setup
no-cond-assign would catch the mistake you are making
a
@battk are you talking about like vscode? that's what I'm typing it in and copy and pasting into console
message has been deleted
do you know if single quote vs double quote matters?
huh I returned varstatus it came back as true
oh
varstatus = 'cancelled' turned to true
need it to compare, which is varstatus == 'cancelled'
new script that looks to be working
Copy code
function loadCurrent() {
    var recordId = nlapiGetRecordId();
    var recordType = nlapiGetRecordType();
    var varrecord = nlapiLoadRecord(recordType,recordId);
    var varinternalid = varrecord.getFieldValues('id'); 
    var varexternalid = varrecord.getFieldValues('externalid'); 
    var varstatus = varrecord.getFieldValue('status');
    var newexternalid = varinternalid + '_Cancelled';
    alert(varexternalid);
    if(varexternalid != null){
        if(varstatus == "Cancelled" && varexternalid != newexternalid){
            varrecord.setFieldValue('externalid',newexternalid);
            nlapiSubmitRecord(varrecord);
            return 'Changed - '+ varstatus + ' external id current ' + varexternalid + ' new ' + newexternalid;
        }
        else{
            return 'No Change';
        }
    }
}
loadCurrent()
so now I think I have to put a trigger to that inside of Netsuite
b
The ESLint plugin is what you want for vscode
SuiteScript 2.x User Event Script Type would be the usual choice
a
So I tried to deploy it and something weird comes up
the script works when I try to load via console f12
sample of one that worked
tried to edit a record and save it. getting this: [Ljava.lang.String;@fa5648_Cancelled
whatever the internal id is, became [Ljava.lang.String;@fa5648
b
serverside suitescript is actually run on a java platform
and that string is not actually a string primitive
its an object, specifically a java String object pretending to be a javascript string
a
so would JSON.stringify every variable be needed?
or a dot notation .toString at the end of every variable
b
JSON.stringify would introduce a new problem, it would try to escape the quotes
a
I'm running into a bit of a roadblock here, I'm searching for conversion of string object to string primitive in javascript right
found an answer that someone is saying I can use object.valueOf();?
how would I even debug this, if it works on console but deploying it has a different outcome
b
the reason your code worked in the browser console is a combination of javascript being very loose on types
and java not at all
Copy code
var varinternalid = varrecord.getFieldValues('id');
would make
varinternalid
equal to something like ['123']
an array of strings
a
why would it do that, there's only 1 result
on each record
so if i [0] it, it would come out of the array? that's weird right?
b
javascript would convert the array to a string when doing
Copy code
['123'] + '_Cancelled'
javascript arrays when converted to a string are comma delimited values
Copy code
getFieldValues
is for use on multi-select fields, which have many values, so it can be represented as an array of strings
a
oh
I'd just need it to be getFieldValue rather than getFieldValues
I'm assuming
so it doesn't try to make it an array, having multiple
it worked!
I'm still a bit confused how i can listen to the debugger if it logs me out of my session
I'll read into it a bit later, thanks for your help
I guess, last question. Now that it's working. What would you do differently on this code.
whether it be for readability, performance, etc..
Copy code
function loadCurrent() {
    var recordId = nlapiGetRecordId();
    var recordType = nlapiGetRecordType();
    var varrecord = nlapiLoadRecord(recordType,recordId);
    var varinternalid = varrecord.getFieldValue('id'); 
    var varexternalid = varrecord.getFieldValue('externalid'); 
    var varstatus = varrecord.getFieldValue('status');
    var newexternalid = varinternalid + '_Cancelled';
    if(recordType == 'salesorder' && varexternalid != null){
        if(varstatus == 'Cancelled' && varexternalid != newexternalid){
            varrecord.setFieldValue('externalid',newexternalid);
            nlapiSubmitRecord(varrecord);
            return 'External ID Changed';
        }
        else{
            return 'No External ID Change';
        }
    }
}
b
use nlapiLookupFields to get the external id and status
use nlapiSubmitFields to make your changes
both for performance/governance points reasons
use camel casing or snake case for variable names
not no case like what you have going
i would not name variables with a var prefix
you did not need to nest if statements, you only needed one level
honestly,
Copy code
if(recordType == 'salesorder' && varexternalid != null){
is unnecessary
you messed up your deployment if this is deployed to a different record type
a
Copy code
varexternalid != newexternalid
This would not cover null case would it? say newexternalid is not null
because it always has a _Cancelled
b
Copy code
if(varstatus === 'Cancelled' && varexternalid !== newexternalid){
a
ok javascript === compares types too, so null doesn't have a type and will be false
b
sorta depends on what you really wanted
if a record didnt have an external id, it would set it to the newexternalid
a
I made the nested if statement because if it doesn't have an external id, i don't want to run the remainder of the script at all. just skip
b
your else statement does nothing
the return value for a user event script entry point is ignored
a
I thought that when the script ran, it would at least display the return value in the console. it didn't
so I don't need a return at all then
b
Copy code
if(recordType == 'salesorder' && varexternalid != null && varstatus == 'Cancelled' && varexternalid != newexternalid){
            varrecord.setFieldValue('externalid',newexternalid);
            nlapiSubmitRecord(varrecord);
    }
would do the same thing without the nesting
a
Copy code
if(status == 'Cancelled' && externalId != newExternalId && externalId != null){
            nlapiSubmitField(recordType,recordId,'externalid',newExternalId);
        }
would be the more optimized version
Copy code
function loadCurrent() {
    var recordId = nlapiGetRecordId();
    var recordType = nlapiGetRecordType();
    var record = nlapiLoadRecord(recordType,recordId);
    var internalId = record.getFieldValue('id');
    var externalId = record.getFieldValue('externalid');
    var status = record.getFieldValue('status');
    var newExternalId = internalId + '_Cancelled';
        if(status == 'Cancelled' && externalId != newExternalId && externalId != null){
            nlapiSubmitField(recordType,recordId,'externalid',newExternalId);
        }
}
well, after i read a bit more into the getFieldValues
b
Copy code
var internalId = record.getFieldValue('id');
is the same as
Copy code
var recordId = nlapiGetRecordId();
it usually doesnt actually make a difference, but the orderstatus field will return the same value regardless of language, the status field is affected by language
a
is there a reason to use nlapiLookupFields rather than record.getFieldValues?
the result is the same right?
or is it so we would refer to pair later like var recordFields = nlapiLookupField(recordType, recordId, ["status", "externalid"]); var externalId = recordFields.externalId; var status = recordFields.status;
or even just using it in the if statement without declaring the external id and status variable
b
doesnt really matter for user event script
but nlapiLookupFieeld will not trigger other user event scripts
nlapiLoadRecord will trigger
when you actually get to suitescript 2 (which is where you should have started), then it actually uses less points than loading too
a
I'll check that out. I have a lot of reading to do. Thanks for your help!