Fill Request country field based on user country code

kuba4
Kilo Expert

Hi There,

This is what I need to do: I have a field "Requester country" which I need to fill when Request is created, based on country of Requester.

Requester has an attribute of country, but it is in long ISO code (3 letters) instead of country name. I have country names mapped to ISO codes in "Country" table and "Requester country" is a reference field to that table. How to make that instead of ISO code from user record, field will be updated with full country name? 

Current code used to fill Request fields (based on call) is something like this:

 

var grRequest = new GlideRecord ("sc_request");
grRequest.initialize();
// copy fields from call to req
grRequest.requested_for = current.opened_by;
grRequest.opened_by = current.opened_by;
grRequest.u_region_high_level = current.u_call_region_high_level;
grRequest.u_requester_email = current.u_call_requester_email;
grRequest.u_requester_name = current.u_call_requester_name;

(...)

//I was trying to use line below but I know it can't work since it is updating ISO code and not full name.

grRequest.u_requester_country = current.u_call_requester_name.country;

(...)

 

Could you please help me with that?

 

Regards,
Kuba

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is this field a reference type "u_call_requester_name"?

to which table it refers?

you will have to do something like this

var rec = new GlideRecord('country'); // give proper country table name here

rec.addQuery('iso_field', current.u_call_requester_name.country); // give valid iso field here

rec.query();

if(rec.next()){

grRequest.u_requester_country = rec.sys_id;

}

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is this field a reference type "u_call_requester_name"?

to which table it refers?

you will have to do something like this

var rec = new GlideRecord('country'); // give proper country table name here

rec.addQuery('iso_field', current.u_call_requester_name.country); // give valid iso field here

rec.query();

if(rec.next()){

grRequest.u_requester_country = rec.sys_id;

}

Regards
Ankur

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

This solution is working 🙂 thanks Ankur for your help!

Willem
Giga Sage
Giga Sage

Are you using default core_country table?

If so use this:

var grCountry = new GlideRecord('core_country');
    if (grCountry.get('iso3166_3', current.u_call_requester_name.country)) {// u_call_requester_name.country is the attribute with the ISO right?
        grRequest.u_requester_country = grCountry.getDisplayValue(); //or grCountry.name
    }

Hi Willem - unfortunately this solution did not work for me, but thanks for your effort! 🙂