Script include

Ashish Khairnar
Tera Contributor

 

I am currently working on a requirement where I need to populate the manager's name when a user is selected in the 'Assigned to' field on the Incident table. I have created a 'Manager' field as read-only, and I've developed a Script Include along with an OnChange Client Script to achieve this functionality. However, I'm facing issues with fetching and displaying the manager's name upon selecting the user in the 'Assigned to' field.

Could someone please assist me with the correct implementation? Additionally, I would like to know if this is the best approach. Specifically, I am interested in knowing which method would require less coding while maintaining efficiency.

Thank you in advance for your assistance. 🙂

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@Ashish Khairnar Instead of achieving this through a combination of Script Include and Client script. You can simply use the Auto Populate feature on the variable form to simply auto populate the value of Manager field.

 

Screenshot 2024-09-14 at 9.01.08 AM.png

For more information please refer to

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 https://docs.servicenow.com/bundle/xanadu-servicenow-platform/page/product/service-catalog-managemen...

Brad Bowman
Kilo Patron
Kilo Patron

First consider this possibility, to see if it meets the requirements in your case.  You could just display the Assigned to's Manager on the form, and similarly dot-walk to it for any reporting, notifications, etc.

 

To do this, right-click the gray header bar and choose Configure > Form Layout. Click once on Assigned to [+] in green from the Available box and a reference icon will appear above the arrow to move it into the Selected box

BradBowman_0-1726307174929.png

Clicking this icon will give you access to every field on the User table, so you can select Manager to show this field on the form

BradBowman_1-1726307380554.png

BradBowman_2-1726307415378.png

Then you can make it read-only via a UI Policy if you'd like.

 

If this doesn't meet your requirements, you could populate a new Manager field on the incident table via getReference in an onChange Client Script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading)
        return;
    
    var userRecord = g_form.getReference('assigned_to' , userInfo );

	function authorInfo(userRecord){
		g_form.setValue('u_manager', userRecord.manager);
    }
}

Where u_manager is the name of a reference field, referencing the sys_user table.  

 

A Glide Ajax call to a Script Include is a much more robust tool, and great to have in your belt when you need to do more.  Here is an excellent guide that might help you spot where yours is going wrong

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

Make certain that the Manager field is a reference and you are returning a sys_id from the Script Include.  If you really want just the name and Manager is a string field, then getReference won't work.  In this case you would want to return the name / user_name or whichever field from the Script Include.  Post your scripts using the insert code </> icon if you're still stuck.

 

Abhishek_Thakur
Mega Sage

Hello @Ashish Khairnar ,

 

Here is the script include & client script as per the ask. You can utilize this to populate the manager's name based on assigned_to field.

 

Script Include;

var GetManagerName = Class.create();
GetEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    demoTest: function() {
        var user = this.getParameter("sysparm_assigned_to");
		var gr = new GlideRecord('sys_user');
		gr.addQuery("sys_id",user);
		gr.query();
		if(gr.next()){
			return gr.manager.name;
		}
    },
    type: 'GetManagerName'

});

 

Client script

OnChange Client script:

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

   var ga = new GlideAjax('GetEmail');
   ga.addParam("sysparm_name","demoTest");
   ga.addParam("sysparm_caller_id",g_form.getValue("assigned_to"));
   ga.getXML(callback);
   function callback(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	alert("Manager name is " +answer);
       g_form.setValue("field_name",answer); // If you want to set the data on any field you can utilize this.
   }
   
}

 

Please mark my answer as accepted solution and give thumbs up, if it helps you. 

The above may work if the script include class is changed to 'GetEmail' (however a better name is GetUserManager). Or the script include has:

GetManagerName.prototype = Object.extendsObject(AbstractAjaxProcessor, {

for the 2nd line. And the Client script has:

var ga = new GlideAjax('GetManagerName')

 Needs testing though.