GlideRecord query on parent table?

perkinsarm
Mega Guru

Is it possible to write a GlideRecord query that will find records whose Parent reference is from a specific table? If not, could you search for records that have a parent then iterate over the results and match on the parent's table?

1 ACCEPTED SOLUTION

One more thought, if you want to force update your existing records without updating the Updated and Updated By fields as well as not force any notifications, etc you can use the below script instead:


var projRec = new GlideRecord("pm_project"); //


projRec.addQuery("parent.sys_class_name", "u_my_change");


projRec.query();


while(projRec.next()) {


      projRec.u_change_parent = projRec.parent;


      projRec.parent = "";


      projRec.autoSysFields(false);   // Do not update sys_updated_on, sys_updated_by, and sys_mod_count


      projRec.setWorkflow(false);       // Do not run any other business rules


      projRec.update();


}



Please make sure you test this script in a test instance prior to production.   You can even add a new line before liNe 3 such as projRec.addQuery("sys_id", "SYS_ID-OF-A-TEST-RECORD"); to test with one record.



You can put this code into a Fix Script and execute it from there.


View solution in original post

8 REPLIES 8

Bradley,


Something like the following should work:


var projRec = new GlideRecord("pm_project");


projRec.addQuery("parent.sys_class_name", "u_my_change"); //Query for parent with class name of your custom task table


projRec.query();


while(projRec.next()) {


      projRec.u_change_parent = projRec.parent; // copy parent to custom field


      projRec.parent = ""; //Blank out parent field


      projRec.update();


}


Hello Bradley,



Please add projRec.setWorkflow(false); before projRec.update(); to the script shared by Michael.


Use the setWorkflow(boolean e) method to minimize triggering an excessive amount of business rules while doing bulk update.



Reference link to update bulk records.


Background Scripts — ServiceNow Elite


http://wiki.servicenow.com/index.php?title=Fix_Scripts


http://wiki.servicenow.com/index.php?title=GlideRecord


One more thought, if you want to force update your existing records without updating the Updated and Updated By fields as well as not force any notifications, etc you can use the below script instead:


var projRec = new GlideRecord("pm_project"); //


projRec.addQuery("parent.sys_class_name", "u_my_change");


projRec.query();


while(projRec.next()) {


      projRec.u_change_parent = projRec.parent;


      projRec.parent = "";


      projRec.autoSysFields(false);   // Do not update sys_updated_on, sys_updated_by, and sys_mod_count


      projRec.setWorkflow(false);       // Do not run any other business rules


      projRec.update();


}



Please make sure you test this script in a test instance prior to production.   You can even add a new line before liNe 3 such as projRec.addQuery("sys_id", "SYS_ID-OF-A-TEST-RECORD"); to test with one record.



You can put this code into a Fix Script and execute it from there.


Thank you. This looks like what I need. I'll run some tests and update with my results.