Call multiple functions in client script from script include

Ashwini53
Tera Contributor

I have two fields in a form. My requirement is, I've to get values selected in Business services (cmdb_ci_service) and environment(cmdb_ci_environment)  field as shown below picture. Both the fields belong to different tables.  

find_real_file.png

And show/populate them in another field named 'name of the account' as shown below in string format . For eg. "Business services_Environment" in this format.

find_real_file.png

For this I tried writing on change client script as below:

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

        var ba = g_form.getValue('business_services');
        var ga = new GlideAjax('GetName');
        ga.addParam('sysparm_name', 'getBSName');
        ga.addParam('sysparm_bs', ba);
        var a = ga.getXML(set_form_values);

    function set_form_values(xml) {
        var answer = xml.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('name_of_the_account', answer);
    }

 

Script include:

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

    getBSName: function() {
        var bs = this.getParameter('sysparm_bs');
        var bsGR = new GlideRecord('cmdb_ci_service');
        bsGR.addQuery('sys_id', bs);
        bsGR.query();
        if (bsGR.next()) {

 

            var return_string = "SVC_ " + bsGR.name;

            return return_string;
        }
    },

getEnvName: function() {
        var env = this.getParameter('sysparm_env');
        var envGR = new GlideRecord('cmdb_ci_environment');
        envGR.addQuery('sys_id', env);
        envGR.query();
        if (envGR.next()) {
            return envGR.name;
        }
    },    type: 'GetName'
});

Using above scripts I'm only getting name of business service in "name of the account field". Need help in getting both business service and environment name.

6 REPLIES 6

Adrian Ubeda
Mega Sage
Mega Sage

Hi Ashwini,

As I see you have one field dependant based on two referenced field. So, you'll need to use two client script onChange to autopopulate that field based on the others behaviours. Or how your logic in the form should be? Are you going to have always both fields filled at the same time?

If it was helpful, please give positive feedback.
Thanks,

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

I need to populate the name/value selected in business service and environment field (both are reference type and belongs to different tables) in the "name of the account" field. It should be in "business service_environment" format. Shown in below picture. 

 

find_real_file.png

Voona Rohila
Kilo Patron
Kilo Patron

Hi Ashwini

you can directly use below to get the displayvalues of refernce field if name is display value of the 2 tables.

you can paralelly add logic by creating another client script on sevice field to populate values accordingly.

//onchange on environment field.
function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
       if(g_form.getValue('business_services') == '')
          g_form.addInfoMessage("please enter business service");
	if(window == null) //for portal
      {
		var bs= g_form.getDisplayValue('business_services');
                var env =  g_form.getDisplayValue('environment');

		g_form.setValue(bs +"_"+env);
	}
	else{ //for non-portal
		var bs= g_form.getDisplayBox('business_services').value;	
                var env =  g_form.getDisplayBox('environment').value;
        
		g_form.setValue(bs +"_"+env);
	}
	//Type appropriate comment here, and begin script below
}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

I tried this, but getDisplayValue/ getDisplayBox is not working on client script. You can see below. I need to populate the name/value selected in business service and environment field (both are reference type and belongs to different tables) in the "name of the account" field. It should be in "business service_environment" format.

find_real_file.png

find_real_file.png

 

find_real_file.png