Auto Populate Reference Field based on String Field (email)

WP1
Mega Expert

Hello Everyone,

 

I am looking to populate a reference field (manager) based on a string field (manager_email) on sys_user table. The string field has the user's email address. In order to ensure the manager field has the correct user name, we would like to reference the email to auto populate the manager name. Please advise with a script if possible

1 ACCEPTED SOLUTION

WP1
Mega Expert

I used a "before" business rule  "insert/update" and this worked. Script below

 

(function executeRule(current, previous /*null when async*/) {

 var gr = new GlideRecord('sys_user');
 if (gr.get ('email', current.getValue('u_manager_email')));
 current.manager=gr.getUniqueValue();

})(current, previous);

View solution in original post

7 REPLIES 7

Hi @WP1 ,

 

You can refer the below link,

https://snprotips.com/blog/2016/2/6/gliderecord-client-side-vs-server-side

This has different methods and also detailed explanation of which one could be the most efficient one. You can easily build your script that you are looking for.

 

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Hi WP1,

 

Client script: (change the Table and Field name values below to your table.)

Screenshot 2024-09-24 100548.png

Script:

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

   //Type appropriate comment here, and begin script below
 	var ga = new GlideAjax('GetUserManager');
	ga.addParam('sysparm_name', 'getAssignedToManager');
	ga.addParam('sysparm_manager_email', g_form.getValue("manager_email"));
	ga.getXMLAnswer(getResponse);

	// callback function for returning the result from the script include
	function getResponse(response) {
		var man = response;
		alert('Manager: ' + man);
		g_form.setValue('u_manager', man);	// manager reference field name
	}
   
}

Check the manager reference field name, I have 'u_manager' above. Script include:

 

Screenshot 2024-09-24 101241.png

Updated script for your use case:

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getAssignedToManager: function() {
        var userEmail = this.getParameter("sysparm_manager_email");
		var user = new GlideRecord('sys_user');
		user.addQuery('email', userEmail);
		user.query();
//		gs.info("GetUserManager: Found " + user.getRowCount() + " user records.");
		if (user.next()) {
			result = user.manager.getValue();
//			gs.info('GetUserManager: user manager = '+ result);
		}
		else {
			result = "Unknown";
		}
//		gs.info("GetUserManager: Returning string: " + result);
		return result;
    },

    type: 'XUserDetailsAjax'
});

Good luck.

WP1
Mega Expert

I used a "before" business rule  "insert/update" and this worked. Script below

 

(function executeRule(current, previous /*null when async*/) {

 var gr = new GlideRecord('sys_user');
 if (gr.get ('email', current.getValue('u_manager_email')));
 current.manager=gr.getUniqueValue();

})(current, previous);