- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 09:01 PM
Hi Team,
I am fetching values from rest message response body and I want to set value for one of the reference field, can't I set it using name only, do I need sys_id to set the values for reference field, cause only names I am getting from response?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 09:10 PM
Yes, in ServiceNow, when setting a value for a reference field, you need to store the sys_id, not just the name. A reference field stores a sys_id internally, even though it may display a name. If you only have the name from the REST API response, you'll need to perform a lookup to find the corresponding sys_id
Setting a Reference Field Using a Name example:
var userName = "John Doe"; // Name received from REST API
var userSysId = "";
var userGR = new GlideRecord('sys_user');
userGR.addQuery('name', userName);
userGR.query();
if (userGR.next()) {
userSysId = userGR.getUniqueValue(); // Get the sys_id
}
// Now, set the reference field
var incidentGR = new GlideRecord('incident');
if (incidentGR.get('<some_incident_sys_id>')) {
incidentGR.caller_id = userSysId; // caller_id is a reference field to sys_user
incidentGR.update();
}
- f multiple records have the same name, your script may fetch the first match. Consider adding more filters like email or company if available.
- Use orderByDesc('sys_created_on') if you need the latest record.
Please mark correct/helpful if this helps you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 09:11 PM
you can set the display value using this syntax
gr.setDisplayValue('caller_id', 'Abel Tuter');
OR
If you are getting sysid then use this
gr.setValue('caller_id','userSysId');
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 09:10 PM
Yes, in ServiceNow, when setting a value for a reference field, you need to store the sys_id, not just the name. A reference field stores a sys_id internally, even though it may display a name. If you only have the name from the REST API response, you'll need to perform a lookup to find the corresponding sys_id
Setting a Reference Field Using a Name example:
var userName = "John Doe"; // Name received from REST API
var userSysId = "";
var userGR = new GlideRecord('sys_user');
userGR.addQuery('name', userName);
userGR.query();
if (userGR.next()) {
userSysId = userGR.getUniqueValue(); // Get the sys_id
}
// Now, set the reference field
var incidentGR = new GlideRecord('incident');
if (incidentGR.get('<some_incident_sys_id>')) {
incidentGR.caller_id = userSysId; // caller_id is a reference field to sys_user
incidentGR.update();
}
- f multiple records have the same name, your script may fetch the first match. Consider adding more filters like email or company if available.
- Use orderByDesc('sys_created_on') if you need the latest record.
Please mark correct/helpful if this helps you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 09:11 PM
you can set the display value using this syntax
gr.setDisplayValue('caller_id', 'Abel Tuter');
OR
If you are getting sysid then use this
gr.setValue('caller_id','userSysId');
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader