Need to pull the variables value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 04:51 AM
- zipcode is entered on the catalog form, check the Location table in SN and validate the country,state and city. If its incorrect populate the Country,State, City but leave the field editable.
- If the zipcode is not existing in Location table then API call to be triggered to MySite. From the response payload, under best suggestion block validate the country, state, city, if incorrect populate the values from the response and leave the fields editable.
1 --> is working correctly
2--> need help to pull the country code from the core_country table
all other variables country & county, town are present in the catalog form, so directly pulled it from the form & validated
request payload {
"country": "India",
"countryCode": "IN",
"postZipCode": "560066",
"county": "Karnataka",
"town": "Bangalore"
}
able to get the success response in the from the rest message, but not getting to pull the dynamic value of country code
"country":"India",
"countryCode":"IN",
"postZipCode":"560066",
"county":"Karnataka",
"town":"BENGALURU"
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 08:43 AM
Hi Is_12,
You need to update your script to correctly extract the country code from the core_country table and adjust your API response parsing to pull values from "bestSuggested" instead of "suggestions[0]".
Fix 1: Fetch Country Code from core_country Table
In ServiceNow, the core_country table (sys_country) holds country details, including the country code. You need to query this table using the country name to get the countryCode.
Modify your function to retrieve countryCode like this:
function getCountryCode(countryName) {
var countryRec = new GlideRecord('core_country');
countryRec.addQuery('name', countryName);
countryRec.query();
if (countryRec.next()) {
return countryRec.getValue('code'); // Fetch the country code
}
return null; // Return null if not found
}
Fix 2: Parse API Response from bestSuggested
Your API response parsing should be updated to extract values from "bestSuggested" instead of "suggestions[0]".
Modify this section:
var responseBody = JSON.parse(response.getBody()); // Convert response to JSON
var httpStatus = response.getStatusCode();
if (httpStatus == '200' || httpStatus == '201') {
// Extract from 'bestSuggested' instead of 'suggestions[0]'
return JSON.stringify({
"country": responseBody.bestSuggested.country,
"countryCode": responseBody.bestSuggested.countryCode, // Now correctly pulling the country code
"state": responseBody.bestSuggested.county,
"city": responseBody.bestSuggested.town
});
}
Final Updated Function
Here's your fully updated getLocationDetails function:
getLocationDetails: function() {
var zipcode = this.getParameter('sysparm_zipcode');
var city = this.getParameter('sysparm_city');
var state = this.getParameter('sysparm_state');
var country = this.getParameter('sysparm_country');
var location = new GlideRecord('cmn_location');
location.addQuery('zip', zipcode);
location.query();
if (location.next()) {
return JSON.stringify({
"country": location.getValue('country'),
"state": location.getValue('state'),
"city": location.getValue('city'),
"countryCode": getCountryCode(location.getValue('country')) // Fetch country code
});
} else {
var reqObj = {
"country": country,
"postZipCode": zipcode,
"county": state,
"town": city
};
var r = new sn_ws.RESTMessageV2('BT- GetLocation(mySite)', 'Default Post');
r.setRequestHeader("APIGW-Tracking-Header", gs.generateGUID());
r.setRequestBody(JSON.stringify(reqObj));
var response = r.execute();
var responseBody = JSON.parse(response.getBody()); // Parse JSON
var httpStatus = response.getStatusCode();
if (httpStatus == '200' || httpStatus == '201') {
return JSON.stringify({
"country": responseBody.bestSuggested.country,
"countryCode": responseBody.bestSuggested.countryCode,
"state": responseBody.bestSuggested.county,
"city": responseBody.bestSuggested.town
});
}
}
return null;
}
How this fixed?
* Fetches countryCode dynamically from the core_country table.
* Extracts data from "bestSuggested" instead of "suggestions[0]".
* Returns correct JSON format with updated fields.
This should solve your issue!
Kindly mark it as "Accepted Solution"/"helpful", as it resolves your query. Please press like button for the resolution provided.
With Regards,
Krishna Kumar M - Talk with AIT3ch
LinkedIn: https://www.linkedin.com/in/mkrishnak4/
YouTube: https://www.youtube.com/@KrishAIT3CH
Topmate: https://topmate.io/mkrishnak4 [ Connect for 1-1 Session]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 10:26 AM
Hello @KKM
in the core_country table i need to fetch the ISO Short (iso3166_2) field currently replaced with return countryRec.getValue('code'); // Fetch the country code with iso3166_2 value and here also done the same thing "countryCode": getCountryCode(location.getValue('country')) // Fetch country code with iso3166_2 (I believe only this are two place that I need to change an backend value of the country code) and this iso3166_2 should be mapped to the countrycode of the request payload meaning :
else {
var reqObj = {
"country": country,
"postZipCode": zipcode,
"county": state,
"town": city
};
Here in the above script only there are 4fields response body, need to include an country code also : "country":"India",
"countryCode":"IN",
"postZipCode":"560066",
"county":"Karnataka",
"town":"BENGALURU"
Thanks,
I**bleep**a
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 11:34 AM
currently made an changes highlighed in bold
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-14-2025 10:56 AM
Normally I should pass the country code var from the form(client script) ideally that var is present in the form, So need to get it form the country table, So now how to pass this ist short(country code) in the request payload & also in the repsonse.
here is an client script which runs onchange of zipcode variable :
Thanks,
I**bleep**a
Looking forward for the fast replay