Rest Api Issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
This is the script wrote in the scheduled script. for every 10 minutes here reference fields are not populating showing [== object object]. and this the api response i am getting:
If this question is not clear please mention.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Even though you're using inc.caller_id.value, this can fail when the reference field is null/empty in the source record — null.value throws an error silently, and sometimes the API returns the field differently depending on whether sysparm_display_value or sysparm_exclude_reference_link is set. Add a safety check:
grInc.caller_id = (inc.caller_id && inc.caller_id.value) ? inc.caller_id.value : '';
The bigger problem — sys_ids don't match across instances
This is the core issue. The value you're pulling (681ccaf9c0a8016400b98a06818d57c7) is the sys_id of a user on Dev196285. That same sys_id almost certainly doesn't exist on your target instance, so ServiceNow can't resolve the reference.
You need to look up the user by a common field like user_name or email. Here's the corrected approach:
// Helper function to resolve a reference across instances
function resolveUser(sourceUserSysId, sourceIncident) {
// If the API response includes display values, use sysparm_display_value=all
// Otherwise, you need a second API call or include user_name in your query
if (!sourceUserSysId) return '';
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', sourceUserSysId); // see note below
gr.query();
if (gr.next()) {
return gr.getUniqueValue();
}
return '';
}
The cleanest fix: Modify your REST message to include sysparm_display_value=all as a query parameter. This changes the API response to:
"caller_id": {
"display_value": "Abel Tuter",
"link": "https://dev196285.service-now.com/...",
"value": "681ccaf9c0a8016400b98a06818d57c7"
}
Then look up by display name or — even better — add sysparm_fields to also return nested user fields. The most reliable approach is to query with user_name since that's typically consistent across instances.
Revised script snippet:
function getLocalUserSysId(remoteCallerObj) {
if (!remoteCallerObj || !remoteCallerObj.display_value) return '';
var user = new GlideRecord('sys_user');
user.addQuery('name', remoteCallerObj.display_value);
user.query();
if (user.next()) {
return user.getUniqueValue();
}
gs.warn('User not found locally: ' + remoteCallerObj.display_value);
return '';
}
// Then in your loop:
grInc.caller_id = getLocalUserSysId(inc.caller_id);
grInc.assigned_to = getLocalUserSysId(inc.assigned_to);
Apply the same pattern to assignment_group — look it up by name on the local instance rather than trusting the remote sys_id. This is the standard approach for any cross-instance data synchronization in ServiceNow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago - last edited an hour ago
Hi @saikumarkak ,
Try using g_form.SetValue(), to set values in the fields. Also as Naveen mentioned these sysids might not be present on the target instance so do a look up and make sure that the sys_id is present and then set the value. This should fix the issue.
Please mark this response as helpful/solved if it helped with your query.
Regards,
Rishi.
