The CreatorCon Call for Content is officially open! Get started here.

can we set the value using Disp_name only for reference field and not the sys_id client script?

Prathamesh Chav
Tera Contributor

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

2 ACCEPTED SOLUTIONS

sunil maddheshi
Tera Guru

@Prathamesh Chav 

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!

 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Prathamesh Chav 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

sunil maddheshi
Tera Guru

@Prathamesh Chav 

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!

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Prathamesh Chav 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader