function for getting sys_id

kendall_lin
Giga Contributor

Hello,

Novice ServiceNow "developer" that would like to know what is the recommended practice for creating a global function that would get the sys_id of a specified record. We use scripts in our record producers to set values for various reference fields (business service, assignment group, etc.). Currently we are hardcoding the sys_id's and they vary by instance (DEV, UAT, PROD).

The most recent use case is when a user submits a Demand through a record producer. We would like to set the business service and assignment group for that Demand record. I know the name of the business service and assignment group, but do not know of a clean and simple method to get the sys_id based on that alone. I envision some type of function that would take some parameters (i.e. table, record name / label) and return the sys_id which I could then use to set current.business_service = 'returned sys_id'.

Any help would be appreciated.

Thanks,
Kendall Lin

15 REPLIES 15

Hi Chuck,



Thanks for that detailed explanation, makes sense.




-Brian


Rama Chandra D
Kilo Guru

Hi Kendall,



When you say, I know the name of the business service and assignment group , do you mean you use fixed business service and assignment group all the time? Or do you have a sort of   look up that maps the business service and assignment group based on a demand record? If you're looking to automate this process of setting up business service and the other field, I think it's better to know how these values are chosen in first place.



Darshak


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Kendall,



I would suggest the below.



1. Create a Property in sys_properties table for the Business Service name (so that it can be changed anytime without doing any code change/deployment)


2. Wherever you need the sys_id, get the Business Service name from Property and use GlideRecord to get the sys_id based on the Business Service Name.



var business_services_name = gs.getProperty('prop_name');




var service = new GlideRecord('cmdb_ci_service');


service.addQuery('name', business_services_name);


service.query();




var output;




if(service.next())


output = service.sys_id;



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


Be careful about using properties too "dynamically". Every time you change a system property, the system cache gets flushed. This can cause performance issues that all users on all nodes will recognize.



I ran in to one case where someone was changing the property every time a business rule ran and it brought the system to its knees.



Remember, they are "system properties", not "application properties" to be set and reset frequently.


antin_s
ServiceNow Employee
ServiceNow Employee

It applies to any property field we have in the system and there are 1.2K properties OOB. Ultimately it depends on how often if you want to update the property. If you want to update it every day, it may not be the best option.