Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to Get value from another table which is not the refernce table for the variable

Shalika
Tera Expert

I have a catalog item which has a reference field "Virtual Machine" referencing to virtual machine table. The another variable is "Cloud Provider". Virtual machine table has a field "Resource Group"

Shalika_0-1668529853357.png

I have another table "Cloud resource container" which has fields - name and cloud provider. The name in "Cloud resource container" table matches with the "Resource Group" in virtual machine table.

Shalika_1-1668530200689.png

 

I want the "Cloud Provider" variable in catalog item to be populated with the field cloud provider in "Cloud Resource container" table. How to do this?

 

1 ACCEPTED SOLUTION

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Shalika ,

Create onchange catalog client script, Fill mandatory details details and select proper catalog 

Note : Replace proper table name and field variable names.

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

    //Type appropriate comment here, and begin script below
    var gaPhone = new GlideAjax('getAjax');
    gaPhone.addParam('sysparm_name', 'get_provider');
    gaPhone.addParam('sysparm_vm', newValue);
    gaPhone.getXML(_handleResponse);

    function _handleResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        g_form.setValue('cloud_provider', answer);  // Replace your Cloud Provider
    }

}

GunjanKiratkar_0-1668533261766.png

 

Script Include :-

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

    get_provider: function() {
        var vm = this.getParameter('sysparm_vm');
        var gr = new GlideRecord("VirtualMachineTableName");
        gr.addQuery("sys_id", vm);
        gr.query();
        if (gr.next()) {
            var grCloud = new GlideRecord("CloudResourceContainerTableName");
            grCloud.addQuery("name", gr.resource_group);
            grCloud.query();
            if (grCloud.next()) {
                return grCloud.cloud_provider;
            }

        }

    },
    type: 'getAjax'
});

 

GunjanKiratkar_1-1668533329765.png

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

View solution in original post

1 REPLY 1

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Shalika ,

Create onchange catalog client script, Fill mandatory details details and select proper catalog 

Note : Replace proper table name and field variable names.

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

    //Type appropriate comment here, and begin script below
    var gaPhone = new GlideAjax('getAjax');
    gaPhone.addParam('sysparm_name', 'get_provider');
    gaPhone.addParam('sysparm_vm', newValue);
    gaPhone.getXML(_handleResponse);

    function _handleResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        g_form.setValue('cloud_provider', answer);  // Replace your Cloud Provider
    }

}

GunjanKiratkar_0-1668533261766.png

 

Script Include :-

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

    get_provider: function() {
        var vm = this.getParameter('sysparm_vm');
        var gr = new GlideRecord("VirtualMachineTableName");
        gr.addQuery("sys_id", vm);
        gr.query();
        if (gr.next()) {
            var grCloud = new GlideRecord("CloudResourceContainerTableName");
            grCloud.addQuery("name", gr.resource_group);
            grCloud.query();
            if (grCloud.next()) {
                return grCloud.cloud_provider;
            }

        }

    },
    type: 'getAjax'
});

 

GunjanKiratkar_1-1668533329765.png

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy