Multiple sys_IDs on system property, how to call in client script to verify condition

Varun Sai
Tera Contributor

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

getPropertyCountry: function() {
        return gs.getProperty('glide.exclude.countries');  // Has 5 countries sys_id's - India, Africa, Japan, China, Sri Lanka
    },
 

Client script

function onSubmit() {

        var ga = new GlideAjax('County_check');
        ga.addParam('sysparm_name', 'getPropertyCountry');
        ga.getXML(myCallBack);

        function myCallBack(response) {
            var prop = response.responseXML.documentElement.getAttribute('answer');
 
            var user_country = g_form.getValue('u_country');  

            if (user_country !=  prop){     //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;
            }
 
But I am getting an error and it's not working as the client script is not pulling the country sys_ID from the system property I think.
What am I missing here, appreciate any help.
1 ACCEPTED SOLUTION

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

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

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).

 

Screenshot_2024-01-31-07-48-04-783-edit_com.android.chrome.jpg

(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 👍

 

Thanks,
Anvesh

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.

Sagar Pagar
Tera Patron

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

The world works with ServiceNow

Tai Vu
Kilo Patron
Kilo Patron

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

Timi_1-1706673024828.png

 

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