Trying to make a validateLine script but the line ...
# suitescript
n
Trying to make a validateLine script but the line is not adding for some reason
Copy code
function validateLine(context) {

    var tranRec = context.currentRecord;
    var etailchan = tranRec.getValue({
      fieldId: 'custbody_celigo_etail_channel'
    });
    log.debug('etailchan', etailchan);
    var cus = tranRec.getValue({
      fieldId: 'entity'
    });
    log.debug('cus', cus);

    if (etailchan == 1 && cus == 53034) {

      var itemname = tranRec.getCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'custcolcat_sale_etail_item_name'
      });
      log.debug('itemname', itemname);

      if (itemname != null || itemname != '') {
        var itemid = getItem(itemname);
        log.debug('itemid', itemid);

        context.currentRecord.setCurrentSublistValue({
          sublistId: 'item',
          fieldId: 'item',
          value: itemid
        });
        return true
      }
    }
  }
thanks for taking a look
b
a validate line function should always return true or false in all cases
you only do it for one specific branch
n
so should I repeat return true elsewhere, or just change the spot?
b
how much do you understand of code branching?
n
beginner, I'm assuming every if statement is a "branch" in this case
b
one for when the if is true, one when its false
you need to return true or false at the end of every one of the branches
if you dont, netsuite's default is to assume false, which is extremely unfavorable for your code
n
I see.. so what would be the optimal way of attacking this one? I want to return true no matter what basically
should I put an else return true?
b
technically you can skip returning false since the default is false, but thats an easy way to confuse the next reader of code
you will find multiple opinions on how you should structure your code
pick whatever you feel makes your code most readable, but you really should make an effort to always return true or false
n
ok
I don't need to commitLine for validateLine functions do I?
b
no
I will give a general warning about changing the item: lots of things are sourced from it
be prepared to use
ignoreFieldChange
or
forceSyncSourcing
if your sourcing is not working the way you expect
n
yeah so I have to make a lookup for a shopify sync (basically mapping a shopify sku to an item in NS) so I have a line column field mapped to the shopify sku, then i have a lookup search in the code and it returns the correct item and I want to set it
but that darn return true; just doesn't seem to be working
b
are you sure its actually getting to the line that returns true
n
I assume its not since its not working lol
but it gets to the itemid, then sets the item correctly
b
whats the point of logging if you arent going to use it
those logs are expensive in client script
n
^
I can see it gets to itemid correctly
and it sets the item ok
it seems to work if i set ignoreFieldChange: true
b
probably want to make sure there are no other scripts interfering with your script
if you are working on a transaction with more than 1 sublist, then you probably want to add code to return true earlier if the sublist is not the item sublist
👍 1