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.

Populate the domain values when a CI changes on incident form

Phanideepthi
Tera Contributor

Hi All,

We have two fields Business domain and Business function on the incident form as shown in the image. Now we are working on getting the values populated on the form when a CI is selected. We have written before insert business rule with the following script.

(function executeRule(current, previous /*null when async*/) {

var ubus = new GlideRecord('cmdb_ci_service_discovered');
    ubus.addQuery('sys_id',current.cmdb_ci);
    ubus.query();
    if (ubus.next()) {
        current.u_business_domain = ubus.u_business_domain;
        current.u_business_function = ubus.u_business_function;
        // gs.print(ubus.u_business_domain);
    }
})(current, previous);
 
Now the issue we are facing is the values are populating once the form is saved instead we need to show when a CI is selected. Any help on this is much helpfull.
Phanideepthi_0-1763139194189.png
 
Thanks in advance.
 
1 ACCEPTED SOLUTION

Hi @Phanideepthi ,

 

Your client script looks good but small issue in script include

getValues: function() {
        gs.log('function');
        var ciSysId = this.getParameter("sysparm_ci");
        var result = {};
        var gr = new GlideRecord('cmdb_ci');
        if (gr.get(ciSysId)) {
            result.domain = gr.u_business_domain.getDisplayValue() + ""; // getDisplayValue you are using small D in display
            result.function1 = gr.u_business_function.getDisplayValue() + "";
        }
       gs.log("Check Return = " + JSON.stringify(result));
        return JSON.stringify(result);
    },

    type: 'GetBusinessDetails'
});

Also add alert in Client script

function onChange(control, oldValue, newValue) {

    if (!newValue) {
        g_form.clearValue('u_business_domain'); // Your field backend value
        g_form.clearValue('u_business_function'); //Your field backend value
        return;
    }

    var ga = new GlideAjax('GetBusinessDetails');
    ga.addParam('sysparm_name', 'getValues');
    ga.addParam('sysparm_ci', newValue); // new Value is your CI field value sysId

    ga.getXML(function(response) {
       
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var obj = JSON.parse(answer);
        alert(obj.function1);
        alert(obj.domain); // if you alert obj it will pass object object you can do JSON.stringify(obj) or dot walk
        g_form.setValue('u_business_domain', obj.domain);
        g_form.setValue('u_business_function', obj.function1);
    });
}

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

 

 

View solution in original post

10 REPLIES 10

Hi @Phanideepthi ,

 

Your client script looks good but small issue in script include

getValues: function() {
        gs.log('function');
        var ciSysId = this.getParameter("sysparm_ci");
        var result = {};
        var gr = new GlideRecord('cmdb_ci');
        if (gr.get(ciSysId)) {
            result.domain = gr.u_business_domain.getDisplayValue() + ""; // getDisplayValue you are using small D in display
            result.function1 = gr.u_business_function.getDisplayValue() + "";
        }
       gs.log("Check Return = " + JSON.stringify(result));
        return JSON.stringify(result);
    },

    type: 'GetBusinessDetails'
});

Also add alert in Client script

function onChange(control, oldValue, newValue) {

    if (!newValue) {
        g_form.clearValue('u_business_domain'); // Your field backend value
        g_form.clearValue('u_business_function'); //Your field backend value
        return;
    }

    var ga = new GlideAjax('GetBusinessDetails');
    ga.addParam('sysparm_name', 'getValues');
    ga.addParam('sysparm_ci', newValue); // new Value is your CI field value sysId

    ga.getXML(function(response) {
       
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var obj = JSON.parse(answer);
        alert(obj.function1);
        alert(obj.domain); // if you alert obj it will pass object object you can do JSON.stringify(obj) or dot walk
        g_form.setValue('u_business_domain', obj.domain);
        g_form.setValue('u_business_function', obj.function1);
    });
}

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

 

 

Getting undefined from script include itself

 

Check Return = {"domain":"undefined","function1":"undefined"}

Hi @Sarthak Kashyap ,

 

My bad missed to update the table to applications.

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

   getValues: function() {
        gs.log('function');
        var ciSysId = this.getParameter("sysparm_ci");
        var result = {};
        var gr = new GlideRecord('cmdb_ci_service_discovered');
        if (gr.get(ciSysId)) {
            result.domain = gr.u_business_domain.getDisplayValue() + ""; // getDisplayValue you are using small D in display
            result.function1 = gr.u_business_function.getDisplayValue() + "";
        }
       gs.log("Check Return = " + JSON.stringify(result));
        return JSON.stringify(result);
    },

    type: 'GetBusinessDetails'
});
 
Client script
 
function onChange(control, oldValue, newValue) {

    if (!newValue) {
        g_form.clearValue('u_business_domain'); // Your field backend value
        g_form.clearValue('u_business_function'); //Your field backend value
        return;
    }

    var ga = new GlideAjax('GetBusinessDetails');
    ga.addParam('sysparm_name', 'getValues');
    ga.addParam('sysparm_ci', newValue); // new Value is your CI field value sysId

    ga.getXML(function(response) {
       
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var obj = JSON.parse(answer);
        // alert(obj.function1);
        // alert(obj.domain); // if you alert obj it will pass object object you can do JSON.stringify(obj) or dot walk
        g_form.setValue('u_business_domain', obj.domain);
        g_form.setValue('u_business_function', obj.function1);
    });
}
 
This worked and getting the values. Thanks for your help. Not sure why before the same script didnt worked before.
 
Thanks alot need one more help.
How can we get the display value of the list collector through flow designer. I am getting the sysid values.

For testing added getdisplayvalue but even without that not able to get the desired output.

Hi @Phanideepthi ,

 

Yes I just verified that function field is reference so displayValue will not work there 

getValues: function() {
        gs.log('function');
        var ciSysId = this.getParameter("sysparm_ci");
        gs.log("ciSysId = " + ciSysId;) // Check this log
        var result = {};
        var gr = new GlideRecord('cmdb_ci');
        if (gr.get(ciSysId)) {
            gs.log("Inside IF = " + ciSysId;) // Check this log
            result.domain = gr.u_business_domain + "";
            result.function1 = gr.u_business_function + "";
        }
       gs.log("Check Return = " + JSON.stringify(result)); // Check this log
        return JSON.stringify(result);
    },

    type: 'GetBusinessDetails'
});

 

Client script

function onChange(control, oldValue, newValue) {

    if (!newValue) {
        g_form.clearValue('u_business_domain'); // Your field backend value
        g_form.clearValue('u_business_function'); //Your field backend value
        return;
    }

    var ga = new GlideAjax('GetBusinessDetails');
    ga.addParam('sysparm_name', 'getValues');
    ga.addParam('sysparm_ci', newValue); // new Value is your CI field value sysId

    ga.getXML(function(response) {
       
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var obj = JSON.parse(answer);
        alert(obj.function1);
        alert(obj.domain); // if you alert obj it will pass object object you can do JSON.stringify(obj) or dot walk
        g_form.setValue('u_business_domain', obj.domain);
        g_form.setValue('u_business_function', obj.function1);
    });
}

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak