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 09:03 AM
Please use below sample code to get sys_id of inserted record.
- var snuser = new GlideRecord('sys_user');
- snuser.initialize();
- snuser.first_name = current.variables.first_name;
- snuser.last_name = current.variables.last_name;
- snuser.location = current.variables.location;
- snuser.u_start_date = current.variables.start_date;
- snuser.title = current.variables.job_title;
- snuser.u_end_date = current.variables.end_date;
- snuser.company = current.variables.company;
- snuser.u_division = snuser.company.parent;
- var sysId = snuser.insert(); // sysId will contain the record created sys_id
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 09:07 AM
Take a look at the getUniqueValue() method of the GlideRecord class. Any GlideRecord, including "current" can call this once the record is created to retrieve the sys_id (aka GUID.)
Example:
var mySysID = current.getUniqueValue();
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_ScopedGlideRecordGetUniqueValue

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2017 07:45 PM
Hi Chuck,
What would be the benefit of using this method over something as simple as reading the field value as Berny suggested below?
(e.g.) var mySysID = current.sys_id;
Thanks,
-Brian
Edit:.. I'm only referring to server side script where current is available.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2017 07:55 AM
Hi Brian,
The thing to remember when dealing with fields is that they are objects. sys_id isn't just a 32 character hex value, it is an object of type GlideElement (like all fields) with methods and properties.
If you are after just the 32 character hex value, I recommend using the getValue() method, otherwise you're making a copy of the object and that may lead to confusion and errors later.
var mySysID = current.getValue('sys_id');