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

Najmuddin Mohd
Mega Sage

Hi @Joshuu ,

Did you enable 'Client Callable' checkbox in the Script Include.
Are you able to log the values when using gs.log() ?


Regards,
Najmuddin.

Hi @Najmuddin Mohd ,

 

Yes, it is enabled.

Shree_G
Kilo Sage

Hello,

 

If you only want to populate the variables for Catalog Item, then you can use "Auto Populate" section in the Variables form without scripting. Below video for reference :

 

https://www.youtube.com/watch?v=IMLdnF7ufOg


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.

Technical Consultant Manan Bhatt walks us through how to Auto-populate Variables on Catalog Item in the ServiceNow Utah Release. You can auto-populate the catalog item variables using an onchange client script. Learn more: ...

Hi @Shree_G ,

 

Thanks for your response. Since the Hotel Code is a Lookup select box. Unable to see that in the dependent question under auto populate tab.