Hi, new to writing scripts. I am trying to check t...
# suitescript
r
Hi, new to writing scripts. I am trying to check the createwo box on sales orders under a certain condition but I get an error when trying to save the record. Any help is appreciated.
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define([],
    function() {
        function beforeSubmit(context){
            log.debug({
                title: 'Set WO Checkbox',
                details:'script started'
            });

            if(context.type = context.UserEventType.CREATE){
                
                
                var salesOrder = context.newRecord;

                var itemSublistSize = salesOrder.getLineCount({
                    sublistId: 'item'
                });
                var CustomerType = salesOrder.getValue({
                    fieldId:'custbody_sc_cust_category'
                });

               
                if (CustomerType != 'Customer' || 'International Customer'){
                    for (x = 0; x <= itemSublistSize -1; x++) {
                        salesOrder.setSublistValue({
                            sublistId:'item',
                            fieldId: 'createwo',
                            line:x,
                            value:true
                        });
                

                    }
                    log.debut({
                        title:'Create Work Orders',
                        details:'Script Done'
                    });
                }
            }
        }
        return{
            beforeSubmit: beforeSubmit
        };
    });
a
=
is an assignment.. "make this equal to" for comparisons you need
==
"is this equal to"
When you're going to OR two things
||
you need to fully write out both of them... this should read
if (CustomerType != 'Customer' || CustomerType != 'International Customer'){
... but even then its gonna be wrong doing ORs and NOTs gets messy so generally avoid that....
the simplest way to solve this at your level is probably just to use a new variable
Copy code
var CustomerType = salesOrder.getValue({fieldId:'custbody_sc_cust_category'});

var validCustomerType = true;

if (CustomerType == 'Customer' || CustomerType == 'International Customer'){ validCustomerType = false; })

if (validCustomerType){...
...
}
I think this is just a typo, but it will blow up your script
generally you'll see this written as
x < itemSublistSize;
... remove the equals and remove the -1, just simpler
your version isn't wrong tho, and its not causing any errors
r
thanks for the help. trying the suggestions now.
👍 1
a
generally you want so share the error as well as the code 🙂 , that will let ppl focus on the problem right away.
r
Still get an error saving. Its a generic netsuite error. No details. "An unexpected error has occurred. Please click here to notify support and provide your contact information." I looked in execution logs and there was nothing there.
a
gimme a minute...
okay so pretty sure your fieldId on your setSublistValue isn't right.. fields generally start with "cust" in this case "custline_" so maybe the field id is "custline_createwo" ? I don't think that's what is causing your error tho, its something more fundamental... you're not even getting the first "set WO Checkbox" log?
r
Ill try that. Yeah no logs. I tried it in the debugger using existing script option and it doesn't do anything. Just stays on waiting for input even after trying to save a sales order. But I could be using the debugger wrong.
b
you have a generic syntax mistake
Copy code
if (CustomerType != 'Customer' || 'International Customer')
is equivalent to true
that wont cause the error you are seeing
otherwise
createwo is a standard field for sales orders that only works for assembly items
👍 1
a
my bad
r
I did change that here is what i am using now
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */
define([],
    function() {
        function beforeSubmit(context){
            log.debug({
                title: 'Set WO Checkbox',
                details:'script started'
            });

            if(context.type == context.UserEventType.CREATE){
                                
                var salesOrder = context.newRecord;

                var itemSublistSize = salesOrder.getLineCount({
                    sublistId: 'item'
                });
                var CustomerType = salesOrder.getValue({
                    fieldId:'custbody_sc_cust_category'
                });
                var validCustomerType = true;
                if(CustomerType == 'Customer' || CustomerType == 'International Customer'){
                    validCustomerType = false;
                }
               
                if (validCustomerType){
                    for (x = 0; x < itemSublistSize; x++) {
                        salesOrder.setSublistValue({
                            sublistId:'item',
                            fieldId: 'createwo',
                            line:x,
                            value:true
                        });
                

                    }
                    
                    }
                }
            }
        return{
            beforeSubmit: beforeSubmit
        }
    });
b
probably want to start with no script and make sure that you can actually create sales orders in that account
then move up to a script that only logs
and when that works, move up to a script that sets one line's createwo
a
yeah that's good advice, it feels dumb doing that but you're able to rule so many thing out as you do that... if you're not even getting that first log it might not even be this script causing the issue?
r
I did try creating a sales order with it off and it did go through. But i will try breaking this down. Thanks for the help.
c
Does
beforeSubmit
implicitly return
true
or do you have to actually write it? Or does it not matter?
r
Incase anyone was curious. The problem ended up being I did not put .js at the end of the filename when I uploaded it to the file cabinet.
a
facepalm
I was curious so thanks for that