- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 03:44 PM
I have created system property to include about 5 countries sys_ids(India, Africa, Japan, China, Sri Lanka), would like to call this system property sys_id's in the client script, 'Country' variable is a reference field to country table.
If one of these countries is not selected then the below condition in client script it should clear the variable values.
Script Include -
Client script -
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 09:27 PM
Hi @Varun Sai
Oops. The actionName variable is missing from the script above.
function onSubmit() {
//Add these lines to your script
if (g_scratchpad.isFormValid){
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax('County_check');
ga.addParam('sysparm_name', 'getPropertyCountry');
ga.getXMLAnswer(function(answer){
var countries = answer;
var user_country = g_form.getValue('u_country');
//If the selected User Countries is not in 5 countries
if(countries.indexOf(user_country) == -1){
g_form.clearValue('u_lead');
g_form.clearValue('c_approver');
return false;
}
//Add this lines to your script
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
});
return false;
}
This is my script include. (Make sure the Client callable checkbox is checked)
var County_check = Class.create();
County_check.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getPropertyCountry: function() {
return gs.getProperty('glide.exclude.countries'); // Has 5 countries sys_id's - India, Africa, Japan, China, Sri Lanka
},
type: 'County_check'
});
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 06:18 PM
Hi @Varun Sai
getXML is async method, in onSubmit client script getXML will not work, you can use getXMLWait or a Display Business Rule.
I prefer using display business rule, see the following code for the display BR (select your appropriate table name).
(function executeRule(current, previous /*null when async*/ ) {
g_scratchpad.exclude_countries = gs.getProperty('glide.exclude.countries');
})(current, previous);
Then in the client script,
function onSubmit() {
var prop = g_scratchpad.exclude_countries;
var user_country = g_form.getValue('u_country');
if (prop.indexOf(user_country) == -1){ //only if the selected country is not any one of India, Africa, Japan, China, Sri Lanka then below conditions needs to be applied
g_form.clearValue('u_lead');
g_form.clearValue('c_approver');
return false;
}
}
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 09:12 PM - edited 01-30-2024 09:14 PM
Hi Anvesh,
I tried your solution and I am getting the JavaScript error on the browser.
I put in alerts and didn't see the country come up on the browser.
Not sure why.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 06:32 PM
Hi @Varun Sai,
Take a look at my one of article related to System property and its usages. It can provide valuable insights and assistance.
System Properties & it's Usage
If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2024 07:57 PM - edited 01-30-2024 09:27 PM
Hi @Varun Sai
You're calling Ajax within an OnSubmit Client Script. So the form will get submitted before the response returning.
Let's check this KB. KB0783579
Let's give my adjustment a try.
function onSubmit() {
//Add these lines to your script
if (g_scratchpad.isFormValid){
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax('County_check');
ga.addParam('sysparm_name', 'getPropertyCountry');
ga.getXMLAnswer(function(answer){
var countries = answer;
var user_country = g_form.getValue('u_country');
//If the selected User Countries is not in 5 countries
if(countries.indexOf(user_country) == -1){
g_form.clearValue('u_lead');
g_form.clearValue('c_approver');
return false;
}
//Add this lines to your script
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
});
return false;
}
Cheers,
Tai Vu