Hi, I want to get country field value from location in catalog client script

soumya17
Tera Contributor

Hi All,

My requirement is: In my catalog item, under variable set there are two fields 

1- Office Location - reference to location table

2-Service to the user - select box with ChoiceA and ChoiceB

Now if the office location.country == FR, then service to the user should display ChoiceA 

Else reverse.

Can someone guide me where did i do wrong.

find_real_file.pngfind_real_file.png

1 ACCEPTED SOLUTION

Use below client script and script inlcude :

find_real_file.png

Script inlcude :

find_real_file.png

Script for you to copy:

Script Inlcude :

var getCountry = Class.create();
getCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {

            getCountrybyLocation: function() {
                var location = this.getParameter("location");
                var grLocation = new GlideRecord("cmn_location");
                if (grLocation.get(location)) {
                    return grLocation.country;
                }
			},
                type: 'getCountry'
            });

Client Script :

//Type appropriate comment here, and begin script below
    var location = "25ab9c4d0a0a0bb300f7dabdc0ca7c1c";
    var locationAjax = new GlideAjax("global.getCountry");
    locationAjax.addParam("sysparm_name", "getCountrybyLocation");
    locationAjax.addParam("location", location);
    locationAjax.getXMLAnswer(callback);

    function callback(response) {
        alert(response);
    }

I have used static variable in client script for storing location, you just need your 'offloc' variable there. Tested this on PDI and it is working fine.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

6 REPLIES 6

Abhijit4
Mega Sage

You cannot access values by dot walking in client script so offloc.country is not working in your case.

You will have to write GlideAjax to fetch that country value by passing location to script include.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Thankyou.  Can you help me with sample code please?

Use below client script and script inlcude :

find_real_file.png

Script inlcude :

find_real_file.png

Script for you to copy:

Script Inlcude :

var getCountry = Class.create();
getCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {

            getCountrybyLocation: function() {
                var location = this.getParameter("location");
                var grLocation = new GlideRecord("cmn_location");
                if (grLocation.get(location)) {
                    return grLocation.country;
                }
			},
                type: 'getCountry'
            });

Client Script :

//Type appropriate comment here, and begin script below
    var location = "25ab9c4d0a0a0bb300f7dabdc0ca7c1c";
    var locationAjax = new GlideAjax("global.getCountry");
    locationAjax.addParam("sysparm_name", "getCountrybyLocation");
    locationAjax.addParam("location", location);
    locationAjax.getXMLAnswer(callback);

    function callback(response) {
        alert(response);
    }

I have used static variable in client script for storing location, you just need your 'offloc' variable there. Tested this on PDI and it is working fine.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Valmik Patil1
Kilo Sage

Hello,

Try using getReference using callback method as below

var location = g_form.getReference('office_location', doAlert); // doAlert is our callback function

function doAlert(location) { //reference is passed into callback as first arguments

alert(' country is: ' + location.country);


}

check for alert if value is right for country and then add your if logic

Thanks,

Valmik