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.

Autopopulate email,location,manager of caller onchange client script

Community Alums
Not applicable

Hi,

I am trying to populate email,location , manager name of caller on change client script,

Unfortunately its not working.

 

Client script - 

 

function onChange(_control, _oldValue, newValue, isLoading, _isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var callerdetails = g_form.getValue('caller_id');


    var ga = new GlideAjax('getEmailID');
    ga.addParam('sysparm_name', 'emaildetails');
    ga.addParam('sysparm_value', callerdetails);
    ga.getXML(getResult);

    function getResult(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
   

        var demo = answer.split(',');
        g_form.setValue('email',demo[0]);
        g_form.setValue('manager',demo[1]);
        g_form.setValue('location',demo[2]);
    }

}
 
 
Script Include:
 
var getEmailID = Class.create();
getEmailID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    emaildetails: function() {
        var test = this.getParameter('sysparm_value');
        gs.log('testshubham', test);
        var xe = new GlideRecord('sys_user');
        xe.addQuery('sys_id', test);
        xe.query();
        if (xe.next()) {
            gs.log('email is ' + xe.email + ',' + xe.manager + ',' + xe.location);
            return xe.email + ',' + xe.manager + ',' + xe.location;
        }
                return '';

    },


    type: 'getEmailID'
});
6 REPLIES 6

If onChange applied on any reference field and you are trying to set other field from same table then use this auto populate options.

 

Your query on other post  , check my reply.

 

 

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

I agree with the approach shared by Mark

check this link and you can achieve this with no-code

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader