autopopulate field values (of user) using glideAjax

SWAPNIL KULKARN
Mega Contributor

Hello, I am using below client script to auto populate field values ('manager' and 'lastname'), when user in 'requested_for' field changes

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

   var id = g_form.getValue('u_requested_for');  // ref to sys_user
   
    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id',id);
    gr.query();
    
    while(gr.next()) {
        
        g_form.setValue('u_manager',gr.manager);
        g_form.setValue('u_lastname',gr.last_name);
        }
}

 

As its not best practice to use gliderecord in client script, can someone help to get the code for script include and client script, if I want this to get done using 'GlideAjax'.

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

This article which I wrote a while back gives some good explanation and examples:
- 2020-01-10 Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

This article which I wrote a while back gives some good explanation and examples:
- 2020-01-10 Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Aman Kumar S
Kilo Patron

Hey,

You can refer to below community article which meets your requirement.

Also, would suggest to have a look at Article shared by Mark above, will be helpful in order to understand how Ajax works.

Get Manager name based an selected user

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Do be aware, that the link you are sharing, the answer marked as correct a performance issue if using this for a reference field/variable.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn