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

Nilesh Pol
Tera Guru

Hi @Joshuu 

No need to write a script!

Have tried Auto populate tab?

NileshPol_0-1742991435605.png