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.

scriptinclude and onchange client script

Mahesho11
Tera Contributor

In cmdb_ci_business_app table if i select Application name (group reference)then the owner (user reference) of that group should auto populated. 

Can u please explain code in detail. 

Thanks in advance.

3 REPLIES 3

Tai Vu
Kilo Patron
Kilo Patron

Hi @Mahesho11 

You can give the getReference a try.

getReference(String fieldName, Function callBack)

Sample OnChange Script of the Group field (make sure you're using the correct field name)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue === '') {
        g_form.clearValue('owner'); //your owner field name
        return;
    }
    g_form.getReference('group', populateOwner); //your gorup field name

}

function populateOwner(group) { // reference is passed into callback as first arguments
    g_form.setValue('owner', group.getValue('manager')); //your owner field name
}

 

 

Cheers,

Tai Vu

Mahesho11
Tera Contributor

Please provide me script include only. I need to show.

Hi @Mahesho11 

There you go.

#OnChange Client Script (table cmdb_ci_business_app and the group reference field)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue === '') {
        g_form.clearValue('owner'); //your owner field name
        return;
    }

	var ga = new GlideAjax('BusinessAppUtilAjax');
	ga.addParam('sysparm_name', 'getGroupManager');
	ga.addParam('sysparm_group_id', newValue);
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
			g_form.setValue('owner', answer); //your owner field name
		}
	});

}

 

#Script Include (make sure the Client callable checkbox is checked)

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

	getGroupManager: function(){
		var groupID = this.getParameter('sysparm_group_id');
		var managerID = '';
		var grGroup = new GlideRecord('sys_user_group');
		if(grGroup.get(groupID)){
			managerID = grGroup.getValue('manager');
		}
		return managerID;
	},

    type: 'BusinessAppUtilAjax'
});

  

Cheers,

Tai Vu