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

sachin_namjoshi
Kilo Patron
Kilo Patron

Please use below sample code to get sys_id of inserted record.



  1. var snuser = new GlideRecord('sys_user');  
  2. snuser.initialize();  
  3. snuser.first_name = current.variables.first_name;  
  4. snuser.last_name = current.variables.last_name;  
  5. snuser.location = current.variables.location;  
  6. snuser.u_start_date = current.variables.start_date;  
  7. snuser.title = current.variables.job_title;  
  8. snuser.u_end_date = current.variables.end_date;  
  9. snuser.company = current.variables.company;  
  10. snuser.u_division = snuser.company.parent;  
  11. var sysId = snuser.insert(); // sysId will contain the record created sys_id


Regards,


Sachin


Chuck Tomasi
Tera Patron

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


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.


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');