https://netsuiteprofessionals.com logo
#suitescript
Title
# suitescript
a

Archie Kawmongkolsi

04/08/2022, 7:39 PM
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

battk

04/08/2022, 7:42 PM
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

Archie Kawmongkolsi

04/08/2022, 7:48 PM
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

battk

04/08/2022, 7:50 PM
i wouldnt expect it to work
take smaller steps
first step is getting the order status and external id
a

Archie Kawmongkolsi

04/08/2022, 7:51 PM
first step is done right?
Copy code
var recordStatus = nlapiGetFieldText('Status');
    var externalidvalue = nlapiGetFieldText('externalid');
b

battk

04/08/2022, 7:51 PM
its something you should be able to confirm for yourself
a

Archie Kawmongkolsi

04/08/2022, 8:36 PM
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

battk

04/08/2022, 9:02 PM
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

Archie Kawmongkolsi

04/08/2022, 9:03 PM
ok i'll try it again with everything as var recordId, var recordtype etc..
the if statement is not working at all
b

battk

04/08/2022, 9:16 PM
you still probably want to get a proper ide with eslint setup
no-cond-assign would catch the mistake you are making
a

Archie Kawmongkolsi

04/08/2022, 9:20 PM
@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

battk

04/08/2022, 9:40 PM
The ESLint plugin is what you want for vscode
SuiteScript 2.x User Event Script Type would be the usual choice
a

Archie Kawmongkolsi

04/08/2022, 9:45 PM
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

battk

04/08/2022, 9:52 PM
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

Archie Kawmongkolsi

04/08/2022, 9:54 PM
so would JSON.stringify every variable be needed?
or a dot notation .toString at the end of every variable
b

battk

04/08/2022, 9:56 PM
JSON.stringify would introduce a new problem, it would try to escape the quotes
a

Archie Kawmongkolsi

04/08/2022, 10:00 PM
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

battk

04/08/2022, 10:08 PM
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

Archie Kawmongkolsi

04/08/2022, 10:12 PM
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

battk

04/08/2022, 10:12 PM
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

Archie Kawmongkolsi

04/08/2022, 10:15 PM
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

battk

04/08/2022, 10:21 PM
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

Archie Kawmongkolsi

04/08/2022, 10:28 PM
Copy code
varexternalid != newexternalid
This would not cover null case would it? say newexternalid is not null
because it always has a _Cancelled
b

battk

04/08/2022, 10:29 PM
Copy code
if(varstatus === 'Cancelled' && varexternalid !== newexternalid){
a

Archie Kawmongkolsi

04/08/2022, 10:30 PM
ok javascript === compares types too, so null doesn't have a type and will be false
b

battk

04/08/2022, 10:31 PM
sorta depends on what you really wanted
if a record didnt have an external id, it would set it to the newexternalid
a

Archie Kawmongkolsi

04/08/2022, 10:32 PM
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

battk

04/08/2022, 10:34 PM
your else statement does nothing
the return value for a user event script entry point is ignored
a

Archie Kawmongkolsi

04/08/2022, 10:35 PM
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

battk

04/08/2022, 10:35 PM
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

Archie Kawmongkolsi

04/08/2022, 10:36 PM
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

battk

04/08/2022, 10:37 PM
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

Archie Kawmongkolsi

04/08/2022, 10:41 PM
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

battk

04/08/2022, 10:50 PM
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

Archie Kawmongkolsi

04/08/2022, 10:57 PM
I'll check that out. I have a lot of reading to do. Thanks for your help!
3 Views