I have task to populate company on the bases of caller on the Incident Form.

Atharvabx
Tera Contributor

I have Created a Onchange client script and calling Script Include to Achieve the Same, my full requirement is to populate company on the bases of caller if company is not available for the caller then fetch the value from CI 's company field but for now i am not getting the company on the bases of the caller.

Client scipt:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var callerDetails = g_form.getValue("caller_id");
    var ga = new GlideAjax("autopopulate_CallerInfo");
    ga.addParam('sysparm_name', getCallerInfo);
    ga.addParam('sysparm_value', callerDetails);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('company', answer);
    }
}
 
Script Include:-
Client callable-YES
var autopopulate_CallerInfo = Class.create();
autopopulate_CallerInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCallerInfo: function() {
        var sysIdOfCaller = this.getParameter('sysparm_value');
        var x = new GlideRecord('sys_user');
        x.addQuery('sys_id', sysIdOfCaller);
        x.query();
        if (x.next()) {
            var comp=x.getDisplayValue('company');
            return comp;
        }
       
    },
    type: 'autopopulate_CallerInfo'
});
2 ACCEPTED SOLUTIONS

Let's go back to the basics: you are triggering the onChange Client Script on change of which field? If you add alerts like this to the script, do you see one or both when changing the value of that field?

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    alert('TEST: client script running');
    if (isLoading || newValue === '') {
        return;
    }
    var callerDetails = g_form.getValue("caller_id");
    var ga = new GlideAjax("autopopulate_CallerInfo");
    ga.addParam('sysparm_name', 'getCallerInfo');
    ga.addParam('sysparm_value', callerDetails);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert('TEST: value received from server script = ' + answer); 
        g_form.setValue('company', answer);
    }
}

 

View solution in original post

Hi @Atharvabx ,

 

Try below code i just checked in my PDI its working,

CS:

swathisarang98_1-1709039837088.png

 

 

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

    var userSysid = g_form.getValue('caller_id');
    var userName = new GlideAjax('getUserCompany');
    userName.addParam('sysparm_name', 'getUser');
    userName.addParam('sysparm_sysid', userSysid);
    userName.getXML(callback);



    //Type appropriate comment here, and begin script below

}

function callback(response) {
    var ans = response.responseXML.documentElement.getAttribute('answer');
    g_form.setValue('company', ans);
}

 

SI:

 

var getUserCompany = Class.create();
getUserCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getUser: function(){
		var sys_id =this.getParameter('sysparm_sysid'); 
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',sys_id);
		gr.query();
		if(gr.next()){
			var company = gr.company;
		}
		gs.info('company is ' + company);
		return company;

	},



    type: 'getUserCompany'
});

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

View solution in original post

16 REPLIES 16

swathisarang98
Giga Sage
Giga Sage

Hi @Atharvabx ,

 

Try something like below in the script include,

var comp=x.company.getDisplayValue();

 

and while passing function name in client script keep it inside 'function name'  ga.addParam('sysparm_name', 'getCallerInfo');

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

Tried the same but no luck still the company is not populating even after changing the CODE

Then you should try the correct solution that I suggested.  I actually test my code when it can be easily replicated, so I know it works with a simple change. 

Subhashis Ratna
Tera Guru

Hi @Atharvabx 

I have updated the Logic , Could you please try and let me know !!

Script Include : 

var autopopulate_CallerInfo = Class.create();
autopopulate_CallerInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCallerInfo: function() {
        var sysIdOfCaller = this.getParameter('sysparm_value');
        
        try {
            var companyID = gs.getUser().getUserByID(sysIdOfCaller).getCompanyID();

            if (companyID) {
                return companyID;
            } else {
                // If company ID is not available, fetch it from the CI
                var ciRecord = new GlideRecord('cmdb_ci');
                ciRecord.addQuery('caller_id', sysIdOfCaller);
                ciRecord.query();
                if (ciRecord.next()) {
                    return ciRecord.getValue('company');
                }
            }
        } catch (ex) {
            // Handle any exceptions that might occur
            gs.log('Error fetching company ID: ' + ex);
        }

        // Return null if neither the caller nor the CI has a company
        return null;
    },
    type: 'autopopulate_CallerInfo'
});



Client Script : 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var callerDetails = g_form.getValue("caller_id");
    var gaPop = new GlideAjax("autopopulate_CallerInfo");
    gaPop.addParam('sysparm_name', 'getCallerInfo'); // You need to use single / double quotes to use the function 
    gaPop.addParam('sysparm_value', callerDetails);
    gaPop.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('company', answer);
    }
}

 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.

Thank you!
Subhashisr7

Hi @Atharvabx 

Have you tried this logic ?

Thanks
Subhashis r7