Dynamically populating reference fields

Chris169
Kilo Contributor

Hi,

 I’m trying to re-use a script to enable the approval fields to be dynamically set to the users departments various assigned approver – referencing the department and then populating the Level 1, 2 and 3 approval fields:

 find_real_file.png

 The data is held in the ‘Department’ table:

 find_real_file.png

 This is what I’ve been playing around with but I can’t seem to get it working:

 function onChange(control, oldValue, newValue, isLoading) {

   var userObject = g_form.getReference('cmn_department', doAlert); // doAlert is our callback function

}

function doAlert(userObject) { //reference is passed into callback as first arguments

   g_form.setValue('cmn_department', userObject.dept_head);

 

 Also, is there a way of having 1 client script to populate all three of the approval fields or would I need 1 script per field?

 Any help greatly appreciated.

 Thanks

 Chris

4 REPLIES 4

rad2
Mega Sage

Hi Chris,

The name in the getReference call should be 'department' since that is the field name on the table - u_par.

Do try if this would work -  var userObject = g_form.getReference('department', doAlert); 

Regarding the client scripts, i am not sure what the use case is here. If you know all the three approvals from referencing the department, then you can do it one client script.

Hope this helps!

Chris169
Kilo Contributor

Thanks for the reply Rad.

I've tried that and it too is not working - any other suggestions would be greatly appreciated.

 

Thanks


Chris

Jim Coyne
Kilo Patron

The "Department" field is grey, so I'm assuming you have an onChange Client Script triggered by the "Requested by" field, correct?

In order to populate reference fields in the most efficient manner, you should use a GlideAjax call to a Script Include which will return both the sys_id and display values for those fields in order to cut down on calls back to the server.

The Script Include would look like so:
Name:  uCustomAjaxUtils (or whatever you want to call it)
Active:  checked
Client callable:  checked
Script:

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

	getDepartmentApprovers: function(){
		var result = {};
		result.department_id = "";
		result.department_name = "";
		result.approver1_id = "";
		result.approver1_name = "";
		result.approver2_id = "";
		result.approver2_name = "";
		result.approver3_id = "";
		result.approver3_name = "";

		var gr = new GlideRecord("sys_user");
		if (gr.get(this.getParameter("sysparm_user_id"))){
			result.department_id = gr.department.toString();
			result.department_name = gr.department.getDisplayValue();
			result.approver1_id = gr.department.approver1fieldname.toString();
			result.approver1_name = gr.department.approver1fieldname.getDisplayValue();
			result.approver2_id = gr.department.approver2fieldname.toString();
			result.approver2_name = gr.department.approver2fieldname.getDisplayValue();
			result.approver3_id = gr.department.approver3fieldname.toString();
			result.approver3_name = gr.department.approver3fieldname.getDisplayValue();
		}
		return JSON.stringify(result);
	},

		type: "uCustomAjaxUtils"
});

 

And the Client Script would look something like:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if (newValue == ""){
		//clear any extra information
		g_form.setValue("department", "");
		g_form.setValue("approver1", "");
		g_form.setValue("approver2", "");
		g_form.setValue("approver3", "");
		return;
	}

	var ga = new GlideAjax("uCustomAjaxUtils");
	ga.addParam("sysparm_name", "getDepartmentApprovers");
	ga.addParam("sysparm_user_id", newValue);
	ga.getXMLAnswer(u_showDepartmentInfo);
}

// Callback function to process the response returned from the server
function u_showDepartmentInfo(response) {
	var result = JSON.parse(response);  //transform the JSON string to an object
	//using a third parameter with the display value cuts out the requirement of the platform to go back to the server to get it
	g_form.setValue("department", result.department_id, result.department_name);
	g_form.setValue("approver1", result.approver1_id, result.approver1_name);
	g_form.setValue("approver2", result.approver2_id, result.approver2_name);
	g_form.setValue("approver3", result.approver3_id, result.approver3_name);
}

Modify any of the field names to match your own.  The code is not tested, but should do the trick.

Chris169
Kilo Contributor

Thanks for the help on this.

 

I've not got it working as it's a little past my ability to configure but as it stands the requirements have been reduced a little but will resurrect this as needed.

 

Thanks

C