How do I query all the records in a Related List by clicking on UI action ?

Abhishek Pidwa
Kilo Guru

Hi, 

I am new to ServiceNow and have just been tasked with something I don't know how I would accomplish. 

Background of the Problem: I need to accomplish versioning on the catalog items on a related list. So we have a Service Catalog Registry as the Parent and Catalog Items as the Related Lists to the parent. My job is whenever a user clicks on NEW VERSION UI Action button, it should automatically query the related list (for Catalog Items) and search for the latest active Catalog Item. In this case it is with the Name = 'Test 3'. It should copy all the values for that table and its related variables and create a new catalog item (with copied values). And make the Test 3 as inactive.

find_real_file.png

I could accomplish this behavior by enabling the user to click on the active catalog item and then when the user click on NEW VERSION , it automatically copies the contents and make this as inactive by using this script: 

 

find_real_file.png

But this puts an extra step on the customer to select a Related record and then hit NEW VERSION and the solution is not user-friendly. 

 

Does anyone have a pointer to how I can accomplish it ? Basically these questions :

 

1) Which API should I use to query the related list ? 

2) How would add a condition in the query to fetch me the most current active record ? In this case active=true

3) How can i make the current record as INACTIVE ? 

4) How can I redirect the URL to the new catalog item ? 

5) A code snippet for the same if someone has already done such type of problem ? This would be really helpful. Thanks a ton for your help 🙂

Let me know if you have any questions/concerns ?

Thanks

 

 

 

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

You could do a GlideRecord query in you UI Action to query for the active item and then copy it and set it to active = false.

View solution in original post

6 REPLIES 6

Brian Lancaster
Tera Sage

You could do a GlideRecord query in you UI Action to query for the active item and then copy it and set it to active = false.

Thanks Brian for the reply. Can you tell me what query should i add to filter only on the related list of the parent ?  A code snippet based on the current situation would be very helpful. 

I would need more info on how your tables are configured and related.

Hey Brian, 

 

So here is the snippet of code which I have written :

var helper = new GlideappScriptHelper();

var catItemRecord = new GlideRecord('sc_cat_item');
catItemRecord.addQuery('active', true);
// How can I make it dynamic ? Currently it has been hardcoded but I would need a way to get this value automatically ????
catItemRecord.addEncodedQuery('u_service_catalog_registry=1030b634db7067406622f97e0f961935^');
catItemRecord.orderByDesc('sys_created_on');
catItemRecord.setLimit(1);
catItemRecord.query();

if (catItemRecord.hasNext()){
var cR = helper.copyItem(catItemRecord);
gs.addInfoMessage(gs.getMessage('Catalog Item copied successfully'));
action.setRedirectURL(cR);
catItemRecord.active = false;
catItemRecord.update();
} else{

//code to create a NEW catalog Item record ????
}

 

 

Is this helpful information ?