Get the requested for manager details on the form through onload script

Community Alums
Not applicable

Hi All, 

 

Having a requirement that need to get the requested for manager details like email id and phone number.

 

trying with get reference but not getting the empty values. 

 

Any suggestions. 

 

Thanks. 

5 REPLIES 5

Hemanth M1
Giga Sage
Giga Sage

Hi @Community Alums ,

 

Use GlideAjax https://docs.servicenow.com/bundle/vancouver-api-reference/page/script/ajax/topic/p_AJAX.html 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ref = g_form.getReference('requested_for', req);
    }
function req(ref) {
         g_form.setValue('<where you want to save the manager>', ref.manager);
    }
}

Run this on change of requested for. 

-Anurag

Community Alums
Not applicable

HI @Anurag Tripathi 

 

we are already having the requested for - manager population on the form. 

based on the manager we need to get the manager details like manager email and manager phone number etc,.

 

Any suggestion. 

For this you need to write a script include and call that from the client side using GlideAjax.

 

Client Side

function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading || newValue == '') {
              return;
      }
      var ga = new GlideAjax('getManagerDetails');
      ga.addParam('sysparm_name', 'getDetails');
      ga.addParam('sysparm_requestedFor', g_form.getValue("requested_for"));
      ga.getXML(updateFields);
}

function updateFields(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
              var returneddata = answer.evalJSON(true);
              g_form.setValue("<field to populate>", returneddata.name);
              g_form.setValue("<field to populate>", returneddata.phone);
              g_form.setValue("<field to populate>", returneddata.email);
}

 

Script include

var getManagerDetails= Class.create();
getManagerDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
      getDetails: function () {
              var requestedFor= this.getParameter('sysparm_requestedFor');
              var loc = new GlideRecord('sys_user');
              if (loc.get(requestedFor)) {
                              var json = new JSON();
                              var results = {
//fetchRequested for's Manager's details
                                     "phone": loc.manager.getValue("phone"),
                                     "email": oc.manager.getValue("email")
 "name": oc.manager.getValue("name")
                             };
                             return json.encode(results);
                     }
             } else {    
                     return null;
             }            
     }
};
-Anurag