GlideRecord getUniqueValue

Colleen
Tera Expert

Is getUniqueValue a valid GlideRecord method?  It is not listed in the GlideRecord documentation, but can be selected from the intellisense menu in a ServiceNow script editor.

If gr is a GlideRecord object, Is it better to use gr.sys_id or gr.getValue('sys_id') to retrieve the sys_id as a string?  When I have referenced the sys_id property in a workflow script it seems to return an object rather than a string.

9 REPLIES 9

Jaspal Singh
Mega Patron
Mega Patron

Hi Collen,

 

Yes, it can be termed as one of methods of GlideRecord.

For question

'If gr is a GlideRecord object, Is it better to use gr.sys_id or gr.getValue('sys_id') '

better use gr.getUniqueValue() as it will always return output in string. Refer link for a check.

Alok Das
Tera Guru

Hi Colleen,

Yes you can use "getUniqueValue" an OOB function to retrieve a sys_id of a record.

gr.getValue('sys_id') will also return the sys_id.

But if you are using "gr.sys_id" you should use toString() function like this "gr.sys_id.toString()"

Any of the above 3 ways can be used to retrieve the sys_id.

Kindly mark my answer as Correct and Helpful based on the Impact

Regards,

Alok

Willem
Giga Sage
Giga Sage

Hi Colleen,

ServiceNow and many developers use different supported functions for this:

- gr.getUniqueValue();

- gr.sys_id.toString()

- gr.sys_id + '';

- gr.getValue('sys_id');

 

All return the sys_id as a string and will work. They are all used throughout the system.

The gr.sys_id is just a 'pointer' to the sys_id field, which will not always be interpreted as a string.

 

Hope this helps! 🙂

 

 

 

Megha Padale
Giga Guru

Hi Colleen,

Both getValue() and getUniqueValue() will return a value from the GlideRecord upon which they're called.

getUniqueValue() accepts no arguments, and simply returns the primary key (the sys_id) of the record.

getValue() on the other hand, can return the value from any table column (including sys_id). It accepts one argument - a field name, and will return the value therein.

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 my answer helped you in any way, mark answer as helpful and correct.

thanks and regards,

Megha

Willem
Giga Sage
Giga Sage

Hi Colleen,

Hope you are doing well.

Is your question resolved? Or do we need to follow-up on this?

Please mark the answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,

Willem