What is the fastest + lowest governance method to ...
# suitescript
c
What is the fastest + lowest governance method to check whether a record exists? For example, given ID 12345, check if that ID exists as a customer without throwing an error. I am thinking suiteql query
j
@Cory Weiner
Copy code
doesCustomerExist = (customerId) => {
    if (!customerId) { return false; }
    try {
        let recordObj = record.load({ type: record.Type.CUSTOMER, id: customerId });
        return true;
    } catch (er) {
        log.debug({ title: '', details: er.message });
        return false;
    }
}
This is something that comes on the top of my head. I ain't sure that this is the best method. This throws an error, however the error is caught on the catch() and returns false if a record doesn't exist. It should cost you 5 units.
e
I'd probably use search.lookupFields, less governance and doesn't attempt to load the whole record which would be quicker
wow 1
z
search.lookupFields! absolutely winner. doesn't load whole record, use primary key (id) for searching... record.load require EDIT/FULL permission for the user who execute script... if not possible to use ID for searching, suitleql (N/query) is my next favorite
wow 1
j
@Zoran R-DATAGRAM Could you please elaborate on the statement, 'record.load require EDIT/FULL permission for the user who execute script'?
z
Copy code
require(['N/record'],function(rec) {

 rec.load({
  "type" : 'invoice',
  "id" : 5723
 });

} 
)
response, executed in Script Debugger with ROLE with Invoice->View Permission
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "INSUFFICIENT_PERMISSION",
  "message": "Permission Violation: You need the 'Transactions -> Invoice' permission to access this page. Please contact your account administrator.",
  "stack": [
    "createError(N/error.js)",
    "<anonymous>(adhoc$-1$debugger.user:3)",
    "<anonymous>(adhoc$-1$debugger.user:1)"
  ],
  "cause": {
    "type": "internal error",
    "code": "INSUFFICIENT_PERMISSION",
    "details": "Permission Violation: You need the 'Transactions -> Invoice' permission to access this page. Please contact your account administrator.",
    "userEvent": null,
    "stackTrace": [
      "createError(N/error.js)",
      "<anonymous>(adhoc$-1$debugger.user:3)",
      "<anonymous>(adhoc$-1$debugger.user:1)"
    ],
    "notifyOff": false
  },
  "id": "",
  "notifyOff": false,
  "userFacing": false
}
record.load() in script is equivalent with EDIT in user interface. It executes BeforeLoad entry if exists for loaded record! I have more complex issue with employee record type. In that case, it is not possible to load record for an Employee with ADMINISTRATOR ROLE!! even you provide Employee->Edit Permission. The error you will receive is something like ‘…only administrator can edit employee record…’ To summarise : NEVER use record.load in your script if you don’t update record. The only exception might be if you need some specific data which exists only if load record (sublist lines, subrecord data….) and that can not be retrieved by search.create or N/query sql search.create({type : ‘invoice’}) -> for this, only VIEW permission is required
🙌 1