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

Hey,

getReference method fetches whole row instead of just one country value therefore it is not considered as best approach in this case. You can use it only when you need multiple values from location.

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

Thanks!! It worked