Auto populate location field Values on the Catalog form

Joshuu
Kilo Sage

Hi All,

 

On a catalog form, I have below 3 fields. Here, Hotel code is lookup select box as it is a string field in the location table. And Brand and country are reference fields. 

 

Requirement is to auto populate Brand and Country when user selects hotel code. I have written Script include and on change Client Script, but it is not working. I am not getting any values.

 

priyarao_0-1742989182925.png

 

Scripts:

Script include:

var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var holidex = this.getParameter('sysparm_holidex');
		gs.log('Hotel Code received: ' + holidex);
        var locationGR = new GlideRecord('cmn_location');
        locationGR.addQuery('u_holidex', holidex);
        locationGR.query();

        if (locationGR.next()) {
            var details = {
                brand: locationGR.u_brand.getValue(),
                country: locationGR.u_country_ref.getValue()
            };
			gs.log('Details found: ' + JSON.stringify(details));
            return JSON.stringify(details);
        }
		 gs.log('No details found for hotel code: ' + holidex);
        return '';
    }
});

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

	 console.log('Hotel Code selected: ' + newValue);

    // Define the GlideAjax call
    var ga = new GlideAjax('GetLocationDetails');
    ga.addParam('sysparm_name', 'getDetails');
    ga.addParam('sysparm_holidex', newValue);
    ga.getXMLAnswer(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		console.log('Response from Script Include: ' + answer);
        var details = JSON.parse(answer);

        // Set the values of brand and country
        g_form.setValue('brand', details.brand);
        g_form.setValue('country', details.country);
    });
}

Please assist.

1 ACCEPTED SOLUTION

@Joshuu 

so basically it's referring to cmn_location table

Ensure your lookup value - Sys Id

Lookup label Field - Holidex

In below screenshot instead of name, enter your field -> u_holidex

AnkurBawiskar_0-1742992349944.png

 

1) is your script include client callable

try this

var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var holidex = this.getParameter('sysparm_holidex');
		gs.info('Hotel Code received: ' + holidex); // this line if returns sysId then use sysId in query
        var locationGR = new GlideRecord('cmn_location');
        locationGR.addQuery('sys_id', holidex);
        locationGR.query();

        if (locationGR.next()) {
            var details = {
                brand: locationGR.u_brand.getValue(),
                country: locationGR.u_country_ref.getValue()
            };
			gs.info('Details found: ' + JSON.stringify(details));
            return JSON.stringify(details);
        }
		 gs.log('No details found for hotel code: ' + holidex);
        return '';
    }
});

client script: check what came in console for JSON

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

	 console.log('Hotel Code selected: ' + newValue);

    // Define the GlideAjax call
    var ga = new GlideAjax('GetLocationDetails');
    ga.addParam('sysparm_name', 'getDetails');
    ga.addParam('sysparm_holidex', newValue);
    ga.getXMLAnswer(function(response) {
        console.log(response);
        var details = JSON.parse(response);
        // Set the values of brand and country
        g_form.setValue('brand', details.brand);
        g_form.setValue('country', details.country);
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Hello @Joshuu 

To simplify, Create 1st variable i.e. "Hotel Code" as Reference field instead of lookup. This would also give you flexibility over time.


If this solution helped resolve your issue, please consider marking it as helpful or correct.
This will assist others in finding the solution faster and close the thread.

Hi @Shree_G ,

 

'Hotel Code' variable/question is a lookup select box type on the catalog form. and reference to the Holidex field under cmn_location table. And Holidex is a string field in location table. Like we would want to display the hotel code instead of location name here on the catalog form. But since that is not a display field, I have created a lookup select box question. 

 

Could you please assist how to proceed further.

Ankur Bawiskar
Tera Patron
Tera Patron

@Joshuu 

you can make the 1st variable as Reference and use Auto populate feature and no scripting required

Auto-populate a variable based on a reference type variable (Utah) 

the 1st variable is referring to which table? share the variable configuration screenshot

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Hotel Code variable/question is a lookup select box type on the catalog form. and reference to the Holidex field under cmn_location table. And Holidex is a string field in location table. Like we would want to display the hotel code instead of location name here on the catalog form. But since that is not a display field, I have created a lookup select box question. 

 

Could you please assist how to proceed further.

@Joshuu 

so basically it's referring to cmn_location table

Ensure your lookup value - Sys Id

Lookup label Field - Holidex

In below screenshot instead of name, enter your field -> u_holidex

AnkurBawiskar_0-1742992349944.png

 

1) is your script include client callable

try this

var GetLocationDetails = Class.create();
GetLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var holidex = this.getParameter('sysparm_holidex');
		gs.info('Hotel Code received: ' + holidex); // this line if returns sysId then use sysId in query
        var locationGR = new GlideRecord('cmn_location');
        locationGR.addQuery('sys_id', holidex);
        locationGR.query();

        if (locationGR.next()) {
            var details = {
                brand: locationGR.u_brand.getValue(),
                country: locationGR.u_country_ref.getValue()
            };
			gs.info('Details found: ' + JSON.stringify(details));
            return JSON.stringify(details);
        }
		 gs.log('No details found for hotel code: ' + holidex);
        return '';
    }
});

client script: check what came in console for JSON

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

	 console.log('Hotel Code selected: ' + newValue);

    // Define the GlideAjax call
    var ga = new GlideAjax('GetLocationDetails');
    ga.addParam('sysparm_name', 'getDetails');
    ga.addParam('sysparm_holidex', newValue);
    ga.getXMLAnswer(function(response) {
        console.log(response);
        var details = JSON.parse(response);
        // Set the values of brand and country
        g_form.setValue('brand', details.brand);
        g_form.setValue('country', details.country);
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader