restrict country selection
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 09:58 PM
Hi,
It is for a catalogue item, I'm trying to restrict users to select any other country rather than their home country.
This country data is coming from a custom table called " u_razor_access", which eventually is coming from the core_country table.
screenshot 1: it is a custom table "u_razor_access"
the variable that holds the value is" what country is this for?" backend name "country"
screenshot 2: i tried to capture the logged in user country by using a lookup select box and a client script (screenshot 3).
Now, eventually i need to compare if the logged in user is selecting the correct country from the lookup select box.
that is comparing the values of "country & requested_for_country_1". if they don't match then it should clear the value and alert a message.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 10:52 PM
Could you not simply check the users country and find the matching country in the u_razor_access table and return that in the client script with your GA?
You could keep the field read only and simply force the country on the field via script if user doesn't have an option of selecting something else anyway.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 11:17 PM
can you please help me do this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 11:07 PM
On client script you get the sys_id of the currently logged in user with "g_user.userID".
Or if you're taking the value from a field then g_form.getValue("field_name");
Then in your client script you'll call the GlideAjax script include with the userID and then do the comparison there. If a match is found, you'll return the Razor country and populate it to the target field and make the field read only. If no match is found then you can for example clear the field and notify user that they're missing a country which is required.
Example script include:
var RazorAccessService = Class.create();
RazorAccessService.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareCompany: function() {
var userID = this.getParameter('sysparm_user_id');
var userRec = new GlideRecord('sys_user');
userRec.get(userID);
if (userRec.isValidRecord()) {
var razorRec = new GlideRecord('u_razord_access');
razorRec.addQuery('u_country', userRec.country.toString());
razorRec.query();
if (razorRec.next()) {
return razorRec.u_country.toString();
}
}
return "";
},
type: 'RazorAccessService'
});
This way if your script include returns emptry string, you know that they either don't have a country or there's no matching razor country. You can modify this to add logic to handle both cases, but currently if no match is found we just return "".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2024 11:05 PM
Hello,
Try this CCS,
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Fetch the user's home country from their user record
var userCountry = '';
var userSysId = g_user.userID;
// Query the user record to get the user's home country
var userGR = new GlideRecord('sys_user');
if (userGR.get(userSysId)) {
userCountry = userGR.getValue('home_country'); // Replace 'home_country' with the correct field name
}
// Fetch the selected country value
var selectedCountry = g_form.getValue('country'); // Replace 'country' with the correct field name
// Compare the selected country with the user's home country
if (selectedCountry !== userCountry) {
// Clear the selected value
g_form.clearValue('country');
// Display an alert message
g_form.addErrorMessage('You can only select your home country.');
}
}
By implementing this client script, you ensure that users can only select their home country in the catalog item.