Script Include return always the same value

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 06:57 AM
Hi All,
I have an issue with Script Include - it always return the same value of this variable "iso_short", no matter which value is this variable "name" on this table "core_country "
Script include :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 08:12 AM
you didn't answer the question?
is that a reference field?
if yes then it should work
If not then please query the correct field on core_country table which holds the country name
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2024 10:18 AM
Hello @Hicham Zeriate
After reviewing the code, I believe the issue may stem from a few potential areas:
- User's Country Field: Ensure user.u_country contains the expected values. You can add logging to verify it:
gs.info("User's country: " + user.u_country);
- GlideRecord Query: Check if user.u_country holds the correct sys_id that corresponds to a record in the core_country table.
- Caching Behavior: Confirm that the fetched data isn't stale.
Below are the complete scripts with try-catch error handling added to help identify the issue:
Script Include:
getSite_CountryCode: function() {
try {
var userId = this.getParameter('user') + "";
var user = new GlideRecord("sys_user");
if (user.get(userId)) {
var result = this.newItem("result");
// Retrieve the Country Code
if (user.u_country) {
var country = new GlideRecord("core_country");
if (country.get(user.u_country)) {
result.setAttribute("country", country.iso_short + "");
gs.info("Country code retrieved: " + country.iso_short);
} else {
gs.info("Country record not found for u_country: " + user.u_country);
}
} else {
gs.info("u_country is not set for user: " + userId);
}
} else {
gs.info("User not found for ID: " + userId);
}
} catch (e) {
gs.error("Error in getSite_CountryCode: " + e.message);
}
},
Client Script
var ga = new GlideAjax("FixPortalCatalogUtilsAJAX");
ga.addParam("sysparm_name", "getSite_CountryCode");
ga.addParam("user", g_form.getValue('requested_for')); // Ensure this field has the correct sys_id of the user
ga.getXML(getResponse);
function getResponse(response) {
try {
var result = response.responseXML.getElementsByTagName("result")[0]; // Get the first result element
if (result) {
var country = result.getAttribute("country"); // Retrieve the country code
// Fetch the site name from the form (assuming it's from a field called 'site_name')
var siteName = g_form.getValue('site_name'); // Replace 'site_name' with your actual field name
var emailDomain = "@company.com"; // Replace with your actual domain if needed
var description = "description_text"; // Replace with an appropriate value if available
if (country && siteName) {
// Concatenate the email address
var emailAddress = country.replace(/ /g, '_') + '-' + siteName.replace(/ /g, '') + '-' + description + emailDomain.replace(/ /g, '_');
// Set the value to the 'DLEmailAddress' field on the form
g_form.setValue('DLEmailAddress', emailAddress);
} else {
console.error("Country or site name is missing.");
}
} else {
console.error("No result returned from Script Include.");
}
} catch (e) {
console.error("Error processing the response: " + e.message);
}
}
These changes should help diagnose the issue by providing more detailed logging, allowing us to verify the values being processed.
"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!
Thank You
Juhi Poddar