Rest Api Issue

saikumarkak
Tera Contributor
try {
    var r = new sn_ws.RESTMessageV2('Dev196285 Get Method', 'Default GET');


    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();

    var responseObj = JSON.parse(responseBody);
    var incidents = responseObj.result;
    for (var i = 0; i < incidents.length; i++) {
        gs.info('Retrived Incident : ' + incidents[i].number);
        var inc = incidents[i];
        var check = new GlideRecord('incident');
        check.addQuery('correlation_display', inc.number);
        check.addQuery('correlation_id', inc.sys_id);
        check.query();
        if (check.next()) {
            check.caller_id = inc.caller_id.value;
            check.category = inc.category;
            check.subcategory = inc.subcategory;
            check.short_description = inc.short_description;
            check.description = inc.description;
            check.impact = inc.impact;
            check.urgency = inc.urgency;
            check.assigned_to = inc.assigned_to.value;
            check.assignment_group = inc.assignment_group.value;
            check.update();
            gs.info('Existing record is updated successfully');
        } else {
            var grInc = new GlideRecord('incident');
            grInc.initialize();
            grInc.caller_id = inc.caller_id.value;
            grInc.category = inc.category;
            grInc.subcategory = inc.subcategory;
            grInc.short_description = inc.short_description;
            grInc.description = inc.description;
            grInc.impact = inc.impact;
            grInc.urgency = inc.urgency;
            grInc.assigned_to = inc.assigned_to.value;
            grInc.assignment_group = inc.assignment_group.value;
            grInc.correlation_display = inc.number;
            grInc.correlation_id = inc.sys_id;
            grInc.insert();
            gs.info('Successfull record is retrieved from the api');

        }
    }
} catch (ex) {
    var message = ex.message;
}

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:  
"caller_id": {
                "value": "681ccaf9c0a8016400b98a06818d57c7"
            },
How can i populate the caller id into another instance form one instance.
If this question is not clear please mention.
2 REPLIES 2

Naveen20
ServiceNow Employee

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.

Rishi_11
Mega Sage

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.