is there a way to get all subrecords that are on a...
# suitescript
u
is there a way to get all subrecords that are on any given record?
c
I can't think of a way using standard SuiteScript API. You might be able to construct some SuiteQL that will get a list though.
Although it might require you knowing every possible sublist type unless you're a SQL ninja that knows something about relational data that I don't.
I guess you could write a statement to traverse every foreign key but each related table isn't necessarily a subrecord as the DB doesn't have a way to differentiate.
function getAllSubrecords(record) {
const allSubrecords = {};
const fieldIds = record.getFields();
fieldIds.forEach(fieldId => {
if (record.getSublist({fieldId: fieldId})) {
const subrecord = record.getSublist({fieldId: fieldId});
allSubrecords[fieldId] = subrecord;
}
});
return allSubrecords;
}
Maybe something like this. I'm just playing around at this point though.
u
I tried
record_data = record.create
… to then
fieldIds = record_data.getFields()
.. to
Copy code
fieldIds.map(function(fieldId) {
     let field = record_data.getField({ fieldId: fieldId });
      try{
         field.id = field.id.replace('_text', '');
         if(field && record_data.hasSubrecord({fieldId: field.id})){
              arrSubRecords.push(field); 
         }
      } catch(res){
        log.debug('Non Sub Record', `${field} was not a sub rec`);
}
But always just ends up in the catch
holy crap were pretty much on the same page there haha
c
lol
I feel like our approach is probably correct.
u
thanks ill keep trying it out!
s
curious about why you'd want this?
u
building out a testing tool (any scriptable record) with a custom user interface currently supports body fields and sublists - just need to figure out how to get subrecords
s
interesting. testing tool like some sort of UAT (ui testing)? FWIW, I think your
hasSubrecord()
approach is better than the
getSublist()
flavor
u
yea exactly. good for consultants or admins to make sure their processes continue to work after releases or script / wf updates etc.
s
Nice. I have been looking at using playwright for this purpose - hadn't started using it for that [E2E testing], but we do use playwright now for automated API testing (i.e. RESTlets/API Suitelets)
👀 1