GlideAJAX Script Includes question

Community Alums
Not applicable

Hi,

 

I have a requirement to autopopulate the cost center code, I am unable to populate it. Kindly help.

 

GlideAJAX

 

1.png

 

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

	var off = g_form.getValue('officer_name');
	var ga = new GlideAjax('officerNameClass');
	ga.addParam('sysparm_name', 'officerNameFunction');
	ga.addParam('sysparm_id', off);

	ga.getXML(callBackFunction);
	function callBackFunction(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
		var op = JSON.parse(answer);
		//g_form.addInfoMessage(op.costCenter);
		g_form.setValue('user_id_officer', op.userid);
		g_form.setValue('office_phone_officer', op.officePhone);
		g_form.setValue('cost_center_officer', op.costCenter);
	}


}

 

Script Include

 

2.png

 

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

    officerNameFunction: function() {
        var officeKey = this.getParameter('sysparm_id');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', officeKey);
        gr.query();
        if (gr.next()) {
            var jsonObj = {};
            jsonObj.userid = gr.getValue('user_name');
            jsonObj.officePhone = gr.getValue('phone');
            jsonObj.costCenter = gr.getValue(cost_center.code);
            return JSON.stringify(jsonObj);

        }

    },

    type: 'officerNameClass'
});

 

3.png

 

4.png

 

Regards

Suman P.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

cost_center.code is undefined causing the script to not run.  Try one of these 2 formats:

jsonObj.costCenter = gr.getValue('cost_center.code');

jsonObj.costCenter = gr.cost_center.code.toString();

View solution in original post

4 REPLIES 4

anshul_goyal
Kilo Sage

Hello @Community Alums ,

You can make a small change in your script include while fetching the cost center code add quotes inside the getValue like 'cost_center.code':

 

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

    officerNameFunction: function() {
        var officeKey = this.getParameter('sysparm_id');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', officeKey);
        gr.query();
        if (gr.next()) {
            var jsonObj = {};
            jsonObj.userid = gr.getValue('user_name');
            jsonObj.officePhone = gr.getValue('phone');
            jsonObj.costCenter = gr.getValue('cost_center.code');
            return JSON.stringify(jsonObj);
        }
    },

    type: 'officerNameClass'
});

 


Please mark my solution as Helpful and Accepted, if it works for you in any way!

Thanks

Brad Bowman
Kilo Patron
Kilo Patron

cost_center.code is undefined causing the script to not run.  Try one of these 2 formats:

jsonObj.costCenter = gr.getValue('cost_center.code');

jsonObj.costCenter = gr.cost_center.code.toString();

Anand Kumar P
Giga Patron
Giga Patron

Hi @Community Alums ,

jsonObj.costCenter = gr.getValue('cost_center.code');

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Community Alums
Not applicable

This worked for me.

 

GlideAJAX

 

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

	var off = g_form.getValue('officer_name');
	var ga = new GlideAjax('officerNameClass');
	ga.addParam('sysparm_name', 'officerNameFunction');
	ga.addParam('sysparm_id', off);

	ga.getXML(callBackFunction);
	function callBackFunction(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
		var op = JSON.parse(answer);
		//g_form.addInfoMessage(op.costCenter);
		g_form.setValue('user_id_officer', op.userid);
		g_form.setValue('office_phone_officer', op.officePhone);
		g_form.setValue('cost_center_officer', op.costCenter);
	}


}

 

Script Include

 

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

    officerNameFunction: function() {
        var officeKey = this.getParameter('sysparm_id');
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', officeKey);
        gr.query();
        if (gr.next()) {
            var jsonObj = {};
            jsonObj.userid = gr.getValue('user_name');
            jsonObj.officePhone = gr.getValue('phone');
            jsonObj.costCenter = gr.cost_center.code.toString();
            return JSON.stringify(jsonObj);

        }

    },

    type: 'officerNameClass'
});