Dotwalking sys_class_name with GlideRecord's addQuery?

JeremiP
Kilo Sage

I’m trying to query all approval records which were opened for records on the sc_req_item table.
If they aren’t processed, they should be closed after 3 weeks of inactivity.

I have issues with getting only records with sc_req_item as a parent to show up.

I thought you could perhaps verify what I’m doing here, because I’ve tried the below in other ways, but I’m missing something: I tried to use the sysapproval field before, and didn’t use the ‘=’ operator before.

var dbgSrc = "JP"; //for gs.log statements’ source

var gr = new GlideRecord('sysapproval_approver');
      gr.addQuery('state', 'requested');
      gr.addQuery('document_id.sys_class_name', '=', 'sc_req_item'); //the record that the approval is for must be under provided table name
      gr.addNotNullQuery('sysapproval.number');
      gr.addActiveQuery();
      gr.setLimit(100);
gr.query();



If I then query for the results this way:

var dbgTempArr = [];
while(gr.next()) {
      var sid = gr.getUniqueValue();
      if (gr.document_id.sys_class_name != "sc_req_item")
            dbgTempArr.push("gr.next() SID: "+sid+
" for: "+gr.document_id.number + " @ " + gr.document_id.sys_id +
" table: " + gr.document_id.sys_class_name);
}
gs.log(dbgTempArr, dbgSrc);



I get not precisely what I’ve been asking for:
 find_real_file.png
find_real_file.png
 
Sometimes it even queries results which shouldn’t be there – a record for example doesn’t have the document_id filled out – instead it only had sysapproval (must’ve been a bug that they already fixed).
I think it shouldn’t be there since I’m using .addNotNullQuery for sysapproval’s number field one line below sys_class_name – if sysapproval is null, surely its fields should also be null?
 find_real_file.png

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

You could just query on the "source_table" field instead:

gr = new GlideRecord('sysapproval_approver');
gr.addQuery('state', 'requested');
gr.addQuery('source_table', 'sc_req_item');  //document_id depends on source_table
gr.addNotNullQuery('sysapproval.number');
gr.addActiveQuery();
gr.setLimit(100);
gr.query();

 

View solution in original post

2 REPLIES 2

Ahmmed Ali
Giga Sage

Hello,

 

Try 

gr.addEncodedQuery('sysapproval.sys_class_name=sc_req_item');

 

Thanks,

Ali

 

Jim Coyne
Kilo Patron

You could just query on the "source_table" field instead:

gr = new GlideRecord('sysapproval_approver');
gr.addQuery('state', 'requested');
gr.addQuery('source_table', 'sc_req_item');  //document_id depends on source_table
gr.addNotNullQuery('sysapproval.number');
gr.addActiveQuery();
gr.setLimit(100);
gr.query();