Is there such a way to take a sys_id of an item/record and find exactly what it is on the instance?

GEM1
Tera Expert

I have come across code that references sys_ids numerous times.  These usually are on my demo instances through work but it would be nice to know how to instantly be directed to the record that has that sys_id.  Is this possible?

For example, we retained a copy of this business rule for future reference from a somwhere (I never did it) and to know what exactly it wants to remove would be just ducky!

var removeGrp = new GlideRecord('sys_user_grmember');
removeGrp.addQuery('user',user);
removeGrp.addQuery('group',toRemoveGrp[n]);
if (current.cat_item == '53fa138074f64e6314c7f926ca310c777'){ //if CAT
removeGrp.addQuery('group.type', '631434874f64e2402c7f926ca310c7c0');

 

1 REPLY 1

Jan Prochazka
Tera Guru

Hi Genna,

 

If your question is: What your business rule is trying to remove?

then the answer, based on the scrip logic, is: It is trying to remove the given user from all the groups specified by the array toRemoveGrp. 

 

In addition, when the business rule was triggered by the object related to the catalog item of the specific sys id (53fa138074f64e6314c7f926ca310c777), then the user is removed only from groups of the specific type (sys id of that group type is 631434874f64e2402c7f926ca310c7c0).

 

Note: I don't see the rest of the script so I don't know it whether it's really removing the Group Member records meeting the given criteria, but by variable names (removeGrp, toRemoveGrp) is seems, the scrip continues with using the removeGrp GlideRecord for deleting them.

 

Please don't take it offensively, but as an architect I have to add two comments:

1) The script itself (and the whole solution) would deserve some refactoring as it is mixing several things together what makes it hard to understand and maintain.

2) Hardcoding the sys ids is a quite bad coding practice - the simplest way is to replace them by at least constants explaining what the sys id are referring at or by using system properties what would make it configurable.

 

And now, a bit about searching objects in ServiceNow per their sys id:

If you want a brute-force solution, check the following, pretty old post on the Developer Forum: How do we find out the record, given the sys_id?  It answers the question and provides a universal script how to do that.

 

But, there can be better, a bit more sophisticated solutions requiring a less force.

If you know what kind of object it is, everything is simpler 

 

In your code, there are coupe of places using sys_id:

1) current.cat_item == '53fa138074f64e6314c7f926ca310c777'

 

Do you know what king of object is the current at that specific place?

 

You will know it directly by the table your business rule is defined for, but I can make a guess what the table is as i can see it has a field cat_item. So it must be one of the following 10 tables (considering just out-of-the-box tables including this field):

 

JanProchazka_0-1682710748845.png

You can get this result via: https://<your instance>.service-now.com/sys_dictionary_list.do sysparm_query=elementSTARTSWITHcat_item&sysparm_view=

 

Let's assume it's sc_req_item (the table Requested Item). Then you can get your wanted object by simply searching Requested Item with the given sys_id via using a standard filter on the respective list view, like this:

JanProchazka_1-1682711532655.png

 

If your table is different, then the method will be the same.

 

a little trick: You can even use a shortcut, once knowing how the ServiceNow URL works:

https://your_instance_name.service-now.com/sc_req_item.do?sys_id=133e6fc6db1523005e749646db9619f3

 

just copy the URL and replace the sc_req_item with another table name, e.g., sc_task, the sys id will stay.

https://your_instance_name.service-now.com/another_table_name.do?sys_id=your_sys_id

 

2) removeGrp.addQuery('group.type', '631434874f64e2402c7f926ca310c7c0')

Here is is obvious, the sys id belongs to dot walked value: sys_user_grmember.group.type. Translated into a human understandable language its a sys id of the Group Type of the given User Group used in the User is Member of Group record

 

JanProchazka_2-1682712251585.png

 

The question is whether the object wanted to locate was that Group Type or, more probably the Group Member record, because that was you question: "what exactly it your business rule trying to remove", and this takes us back to the beginning of my answer.

 

 

I hope this helps.

 

Regards,

Jan