function for getting sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 08:59 AM
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
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:13 PM
Hi Kendall,
You can use this function:
// Return the sys_id value for a given table and its display value
function GetIDValue(table, displayValue) {
var rec = new GlideRecord(table);
var dn = gs.getDisplayColumn(table);
if (rec.get(dn, displayValue))
return rec.sys_id;
else
return null;
}
Thanks,
Arindam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2019 12:39 AM
Case Sensitive: One note is that while GetIDValue('table', "displayValue") works great to retrieve the sys_id, it appears that getidvalue('table', "displayValue") will not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:22 PM
Hi Kendall,
Retrieving the sys_id of the object is always possible out of the box. There's client side functions and server side functions to accomplish this.
At the server side:
- the object within the context of the record where your code is running is referred as current. You can then do something as simple as current.sys_id
- through GlideRecord objects you can also query other tables/object. For such cases you can do something as simple asglideRecordInstance.sys_id (replace glideRecordInstance with the name of the instance of your GlideRecord)
In regards your use case:
- the assignments that you're mentioning can be accomplished through Business Rules. Most probably an onBefore Insert / Update Business Rule that will perform the respective assignments based on the conditions you set.
- Assignment Rules are also an option. Defining Assignment Rules - ServiceNow Wiki
Thanks,
Berny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:40 PM
Hi Kendall,
I believe getUniqueValue() is a best way to get unique id (sys_id) of the record from server side and client side scripting both. If you have any reference field and you want to get the unique id of that particular field value then you can use the getReference() fucntion to glide the record and then fetch the sys_id of the record.
Server side:
current.getUniqueValue(); OR use the object which you use to Glide the record like,
var gr = new GlideRecord('cmdb_ci);
// Your rest of the code
gr.sys_id
Client Side:
g_form.getUniqueValue(); to get the unique id of the record OR
function onChange(control, oldValue, newValue, isLoading) {
var bus = g_form.getReference('business_services', doAlert);
}
function doAlert(bus) {
var UID = bus.sys_id; // to get the sys_id of the reference field record.
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2019 04:29 AM
My solution for this is to:
create a UTIL sys include class that contains methods to retrieve the sysid of a record given the name (or other identifying field)
for example, to get the sysid of a particular group,
create a method that takes the name of the group as the input and returns the sysid of the group from sys_group.