Given a record of unknown type that I’ve retrieved...
# suitescript
j
Given a record of unknown type that I’ve retrieved using
record.load()
or
currentRecord.get()
, is there way to tell that it’s a type of
transaction
without checking all possible transaction types e.g.
salesorder
,
estimate
etc?
e
You could probably do something clever-ish like looking for the presence of certain fields/sublists or something, but specifically checking the possible transaction types you care about seems like a pretty quick utility function you could write once and use repeatedly.
Copy code
const isTransaction = (type) => {
  const transactionTypes = ['salesorder', ...];
  return transactionTypes.includes(type);
}
t
If you can afford to run a SuiteQL query, you could do something like this:
Copy code
SELECT BUILTIN.DF(Type) FROM Transaction WHERE ID = 4939031
b
Use recordtype instead of BUILTIN.DF(Type) if you need the scriptable record type like salesorder and estimate.
I wrote a couple functions for you that you might be able to use. The first one, isTransactionType, simply checks if the record has the transactionnumber field. You can change the field to a different one if needed.
Copy code
/**
 * @type {record.Record}
 * @return {string}
 */
const isTransactionType = rec => !!rec.getField({fieldId: 'transactionnumber'})?.id;
The second one, getBaseRecordType, gets a portion of the record's URL. It's definitely on the creative side and won't work for all types of records, but it at least works for items, transactions, cases, and custom record types.
Copy code
/**
 * @type {record.Record}
 * @return {string}
 */
const getBaseRecordType = rec =>
  url.resolveRecord({
    recordType: rec.type,
    recordId: rec.id,
  }).split('/')[3].replace(/s$/, '');
m
@burkybang Why does it look like you're running that code locally and how are you doing that? Are you mocking those modules?
b
I'm running the code in my browser console on a NetSuite scriptable page.
👍 1
I use my employee record to run tests like this most often since it's so easy to get to from any page, but all records, saved searches (edit mode), and Suitelets allow loading SuiteScript modules client side.
e
My go-to scriptable page is @tdietrich’s SuiteQL tool
b
Nice, it seems like a really useful too, but I don't normally have access to it since I'm a consultant in many different NS accounts, so I just do everything in my console including testing SuiteQL. I'd have to install it in every NS account I use.
j
I think I’ll just doing an
includes()
. Thought there might be a handy-dandy way.
for context, I’m writing a client script that will run on All Records and check if there are tasks associated and show a little coloured box at the top so that the person doesn’t have to go down to Communications -> Activities to see if there are any associated tasks.
to do my lookup I need to know if this is a case, entity, transaction so I know my WHERE field
I actually got a helpful response from NS Support (though given that their solution includes typos and the word “lol” I suspect they copy-pasted it from StackOverflow or Reddit or something).
if(Object.values(transaction.Type).includes(type_to_check))
👀 1
can do this using N/transaction
b
That's a great idea!