Autopopulate the Business Unit based on the requested for user's.

vinuth v
Tera Expert

Hi All,


We have a Single line text field - "Business Unit" in the form On load of this form the Business Unit Field should auto populate with the User's Business Unit[Requested for] value.

The User Table has the Department Field which is may be of type Business Unit / Sub business Unit/Division...etc

Our Expectation is When we glide record to User Table it should check whether the department is of type Business unit or not.

if (User's "Department is Business Unit" in cmn_department table )
return the same value to Business Unit Field.

else(check the Department's Parent. If Parent is of type Business Unit)
return that parent as Business Unit with respect to th User.

Note : User Details : sys_user Table
Sys_user Table has a Reference Field : Department.

Department : cmn_department Table
it has a field Organization type : Business Unit/sub business Unit.
Parent Field: Refering it's parent Department from the cmn_department table itself.

vinuthv_0-1666355097027.png

 

vinuthv_2-1666355219204.png

 

Here I need to populate name in the Bussiness Unit field.

vinuthv_3-1666355331488.png

 

vinuthv_5-1666355425802.png

 

Please any one provide me the input.

Thanks,

Vinuth

 

 

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @vinuth v 

 

Create a client callable Script Include as below:

 

var UserBusinessUnit = Class.create();
UserBusinessUnit.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getBusinessUnit : function() {
		
		var bUnit = '';
		var usrID = this.getParameter('sysparm_userID');
		
		var grUser = new GlideRecord('sys_user');
		grUser.addQuery('sys_id',usrID);
		grUser.query();
		
		if(grUser.next())
			{
				var dpt = new GlideRecord('cmn_department');
				dpt.addQuery('sys_id',grUser.department);
				dpt.addQuery('u_organisation_type','business_unit');
				dpt.query();
				if(dpt.next())
					{
						bUnit = dpt.name.toString();
					} else {
						if(dpt.parent.u_organisation_type == 'business_unit')
							{
								bUnit = dpt.parent.name.toString();
							}
					}
			
			}
		return bUnit;
		
	},

    type: 'UserBusinessUnit'
});

 

You can right onChange Client script on change of Requested For field. To make it run on Load also, remove 'isLoading' from If Condition from Line 2 of Client script. As below:

 

function onChange(control, oldValue, newValue, isLoading) {
   if ( newValue == '') {
      return;
   }
	
	var ga = new GlideAjax('UserBusinessUnit'); 
ga.addParam('sysparm_name','getBusinessUnit'); 
ga.addParam('sysparm_userID',g_form.getValue('requested_for')); 
ga.getXML(BUnitFunc);  


function BUnitFunc(response) {  
   var answer = response.responseXML.documentElement.getAttribute("answer"); 
    g_form.setValue('businnes_unit',answer);
}
}

 

Field names may differ based on your instance field name values. 

Also if Organisation Type field is Reference and not Choice, use sys_id instead of values I have put.

 

 

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

3 REPLIES 3

AnubhavRitolia
Mega Sage
Mega Sage

Hi @vinuth v 

 

Create a client callable Script Include as below:

 

var UserBusinessUnit = Class.create();
UserBusinessUnit.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getBusinessUnit : function() {
		
		var bUnit = '';
		var usrID = this.getParameter('sysparm_userID');
		
		var grUser = new GlideRecord('sys_user');
		grUser.addQuery('sys_id',usrID);
		grUser.query();
		
		if(grUser.next())
			{
				var dpt = new GlideRecord('cmn_department');
				dpt.addQuery('sys_id',grUser.department);
				dpt.addQuery('u_organisation_type','business_unit');
				dpt.query();
				if(dpt.next())
					{
						bUnit = dpt.name.toString();
					} else {
						if(dpt.parent.u_organisation_type == 'business_unit')
							{
								bUnit = dpt.parent.name.toString();
							}
					}
			
			}
		return bUnit;
		
	},

    type: 'UserBusinessUnit'
});

 

You can right onChange Client script on change of Requested For field. To make it run on Load also, remove 'isLoading' from If Condition from Line 2 of Client script. As below:

 

function onChange(control, oldValue, newValue, isLoading) {
   if ( newValue == '') {
      return;
   }
	
	var ga = new GlideAjax('UserBusinessUnit'); 
ga.addParam('sysparm_name','getBusinessUnit'); 
ga.addParam('sysparm_userID',g_form.getValue('requested_for')); 
ga.getXML(BUnitFunc);  


function BUnitFunc(response) {  
   var answer = response.responseXML.documentElement.getAttribute("answer"); 
    g_form.setValue('businnes_unit',answer);
}
}

 

Field names may differ based on your instance field name values. 

Also if Organisation Type field is Reference and not Choice, use sys_id instead of values I have put.

 

 

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi @AnubhavRitolia ,

As of now the above script is working, But instead of single line text type I need to use Reference field for the question "Business Unit" .

I wrote the onload script also and it is also working fine.

The above code is working for the single line type but it is not working for the Reference type.

I need to use Reference type for the "Business Unit" and requested for department also need to populate when I load the form and when I change the requested for then also I need to populate the users Department.

 

Please help me, is this possible for reference type???

 

Thanks,

Vinuth 

Scouse
Tera Contributor

Hi, can you please let me know how you resolved the issue with the reference field type?