I have a script with a VALIDATE LINE function whic...
# suitescript
b
I have a script with a VALIDATE LINE function which is used to validate the data against a set of rules on all transaction lines entered. I have found, however, it also tries to run when someone adds an attachment under the Communication/Files section and when it does this it throws an error message - so I assume that it thinks this is a transaction line. Is there a simple condition I can add so it won't run the code on these lines? It there something unique that identifies them? TIA
e
You might share your code for more specific help, but in general, the
validate*
functions should return
true
in all cases that you don't care about. If you only care about validating transaction lines, you can inspect the
context.sublistId
property that is passed in and only run your validation logic when the
sublistId
is `'item'`; all other paths should
return true
.
b
Thanks, I'll have a look at that.. the code is possibly a bit long to share.. I am running validate on sublistid of type item and expense.
e
Then I'd expect your code at a minimum to have behavior like:
Copy code
function validateLine(context) {
  if (context.sublistId === 'item') {
    return isValidItemLine(context)
  }
  
  if (context.sublistId === 'expense') {
    return isValidExpenseLine(context)
  }

  // all other paths return true
  return true;
}
1
b
Thanks, there is lots of different elements of the item and expense lines to validate, so I've just return true if its a mediaitem and it appears to be working correctly.
Copy code
function validateLine(scriptContext) {
        // COST CENTRE FIELD CHECK
        let objRec = scriptContext.currentRecord;
        let contextcheck = scriptContext.sublistId;
        if (scriptContext.sublistId === 'mediaitem') {
          return true;
        }
.... // about 100 lines of code to validate if the data in the item/expense lines are correct.
}
1
s
There are multiple sublists in a transaction, like notes, systeminfo etc... My "go-to" would be instead of
Copy code
if (scriptContext.sublistId === 'mediaitem') {
          return true;
}
I would use
Copy code
if (scriptContext.sublistId !== 'expense' && scriptContext.sublistId !== 'item') {
          return true;
}
Or encapsulate your validation implicitly:
Copy code
if (scriptContext.sublistId === "item") {
   // do item validation
}
else if (scriptContext.sublistId === "expense") {
   // do expense validation
}
else return true;
e
^ This was my recommendation as well, though the `else`s are unnecessary when each branch `return`s
b
Thanks, I've changed it to the below as I was still getting some errors with cash entries and vendor bills. Journals now seem to be ok. The same validation is done on both expense and item lines.
Copy code
if (scriptContext.sublistId !== 'expense' && scriptContext.sublistId !== 'item') {
        return true;
      }