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.

On Incident form Auto Populate users email and user id when user changes using script include

Vutikonda Vinay
Tera Contributor

Can anyone know help me to do this coding in Javascript.

trying to do the script with GlideAjax.

I'm tried with the help of  getReference method, but we know its not a good practice.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var caller = g_form.getReference('caller_id');
    g_form.setValue('u_email', caller.email);
    g_form.setValue('u_user_id', caller.user_name);
}

Thanks in advance. 

3 REPLIES 3

Harish KM
Kilo Patron
Kilo Patron

Hi use the below, Make sure field names are correct

Script Include

getUser: function()
{
var user = this.getParameter("sysparm_id");
var userInform ={};
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", user);
gr.query();
if (gr.next()){
userInform.name = gr.getValue("name");
userInform.email = gr.getValue("email");
}

return JSON.stringify(results);

},

 

Onchange client script on caller id

Script:

    var requestor = g_form.getValue("caller_id");
        
       var ga = new GlideAjax("getUserDetails"); // script include name
       ga.addParam("sysparm_name", "getUser"); // function name
       ga.addParam("sysparm_id", requestor); // value from client script pass to  script include
       ga.getXMLAnswer(response);
                }
}

function response(getResponse) {
var answer = JSON.parse(getResponse);
g_form.setValue("fieldname", answer.name);
g_form.setValue("fieldname", answer.email);
}

Regards
Harish

chetan17421
Tera Guru

@Vutikonda Vinay : Refer this link,

auto Populating the values in a form using glideajax and script include

Let me know if it helps you.

Abhijit4
Mega Sage

Hi,

Do you know instead of creating those two fields on incident record, you can bring caller email and caller user id on incident form directly from caller record. You don't need to write any script to populate those values, this way it is easy to maintain data.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP