how do you query gliderecord using a reference field's value

regwalker53
Kilo Explorer

// Using a single test OHF with little data and a closed RITM

var OHF = new GlideRecord('u_ob_openhirefulfillment');

var lm = "OH_test_rig_3";

OHF.get("cbe0a2be2b7c664091d580a119da1511"); // hard coding for testing only

gs.log("First name: " + OHF.u_first_name, lm); // verify we got the test record

// get RITM

var ritm =   OHF.u_fksc_req_item_request.getRefRecord();

gs.log("get returned   RITM: " + ritm.getDisplayValue());

gs.log('OHF request field: ' + OHF.u_fksc_req_item_request, "openHire");

gs.log('ritm value: ' + ritm.sys_id, "openHire");

try {

var myOHF = new GlideRecord('u_ob_openhirefulfillment');

var RITMSID =   ritm.sys_id;

myOHF.addQuery('u_fksc_req_item_request', 'RITMSID');

myOHF.query();

if (myOHF.next) {

  gs.log("last error msg; " + myOHF.getLastErrorMessage());

  gs.log('OHF in email scripting is ' + myOHF.getDisplayValue(), "openHire");

  gs.log("First name: " + myOHF.u_first_name, lm); // verify we got the test record

} else {gs.log('query returned nothing for ' + ritm.getDisplayName()); }

} catch (err) {gs.log('query returned nothing for ' + ritm.getDisplayName()); }

The logs show that the ritm is successfully read and gs.log output shows the value of the RITMSID field is the sys_id stored on the u_ob_openhirefulfillment's u_fksc_req_item_request field.

The problem is that no errors are thrown and the myOHF.u_first_name displays a blank, even though the field is valued based on the first log message.

1 ACCEPTED SOLUTION

There are few syntax errors. Try this



var OHF = new GlideRecord('u_ob_openhirefulfillment');


var lm = "OH_test_rig_3";


OHF.get("cbe0a2be2b7c664091d580a119da1511"); // hard coding for testing only


gs.log("First name: " + OHF.u_first_name); // verify we got the test record






// get RITM


var ritm =   OHF.u_fksc_req_item_request.getRefRecord();


gs.log("get returned   RITM: " + ritm.number);






gs.log('OHF request field: ' + OHF.u_fksc_req_item_request, "openHire");


gs.log('ritm value: ' + ritm.sys_id, "openHire");


try {


  var myOHF = new GlideRecord('u_ob_openhirefulfillment');


  var RITMSID =   ritm.sys_id;


  myOHF.addQuery('u_fksc_req_item_request', RITMSID);


  myOHF.query();


  if (myOHF.next()) {


  gs.log("last error msg; " + myOHF.getLastErrorMessage());


  gs.log('OHF in email scripting is ' + myOHF.getDisplayValue(), "openHire");


  gs.log("First name: " + myOHF.u_first_name); // verify we got the test record


  } else {gs.log('query returned nothing for ' + ritm.getDisplayName()); }




} catch (err) {gs.log('query returned nothing for ' + ritm.getDisplayName()); }


View solution in original post

6 REPLIES 6

Abhinay,



Thank you for finding my syntax error, I couldn't see. The use case is a context where I have a RITM object but need a OHF object. The OHF has a foreign key to the RITM, but the reverse isn't true. It only happens in one place so far, where I have a RITM w/o an OHF in hand. It should have been a slam dunk to get it, but my coding error may it harder than necessary.



Reg Walker


Application Developer


Glad you got this working.