Auto populate Department and Location field

Kiff
Giga Expert

In creating a new hardware record, i want to auto populate ''Department'' and ''Location'' fields when ''assigned to'' is not empty. Basically, i need the Department and Location field to have the same information from the user profile in the  ''assigned to'' field.

Can anyone help with a script for this or is their a condition i can use to achieve this?

find_real_file.png

1 ACCEPTED SOLUTION

jacob_benker
Tera Expert

Kiff,

This should be OOTB functionality.

You should have a Client Script on the [alm_asset] table called "Set Loc/CC/Dep/Com from assigned to".

View solution in original post

9 REPLIES 9

i agree with @jacob . ootb it's already populating

Harsh Vardhan
Giga Patron

You can either write onChange() client script on "assigned to" field to fulfill this requirement or you can simply add the field by doing dot walking from the form lay out..

Solution 1:

client script:

if (isLoading || newValue == '') {
return;
}

var at = g_form.getValue('assigned_to');
var ga = new GlideAjax("Requesterdetails");
ga.addParam("sysparm_name","requester");
ga.addParam("sysparm_user",at);
ga.getXMLAnswer(ajaxResponse);

function ajaxResponse(response) {
var result = response.evalJSON();
var department = result.department;
var location = result.location;
alert('see department: ' + department + ' and req for: ' + location);
g_form.setValue('department',department);
g_form.setValue('location',location);
}}

 

Script include

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

requester: function(){
var obj = {};
var adt = this.getParameter('sysparm_user');
var userGr = new GlideRecord('sys_user');
if(userGr.get('sys_id',adt)){
obj.location= userGr.getValue('location');
obj.department = userGr.getValue('department');
}
var result = new JSON().encode(obj);
return result;
},
type: 'Requesterdetails'
});

Solution 2:

 

 

 

 

 

same way you can do it for department as well

Priyanka136
Mega Guru

Hi Kiff,

You have to write OnChange Client script on Hardware ('alm_hardware') table.

Try the below script:-

function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
   if (isLoading || newValue === '') {
      return;
   }
	
	var id = g_form.getValue('assigned_to');
	var user = new GlideRecord('sys_user');

	user.addQuery('sys_id',id);

	user.query();

	if ( user.next() ) 
	{

		   g_form.setValue('department', user.department);

		   g_form.setValue('location', user.location);

	}	
   
}

find_real_file.pngfind_real_file.png

Let me know if you have any questions.

Please mark it Correct or Helpful, if it works based on impact....!!!!

Warm Regards,

Priyanka
find_real_file.png

www.dxsherpa.com

If you don't have the OOTB client script and have to build it, I would suggest putting it on the alm_asset table instead of the alm_hardware table. Unless you really want it to only apply to hardware assets and not all assets.

I would also suggest using g_form.getReference() instead of doing a GlideRecord query. GlideRecord can work but if there is an issue in the query you return blank or incorrect values. Using getReference pulls from the exact record without having to run a query.

 

  g_form.getReference('assigned_to', setLocation);
}

function setLocation(assignee) {
if (assignee && assignee.location != '')
  g_form.setValue('location', assignee.location);
  g_form.setValue('cost_center', assignee.cost_center);
  g_form.setValue('department', assignee.department);
  g_form.setValue('company', assignee.company);
}

brendanwilson84
Kilo Guru

You could dot walk from the assigned to field. Once information is in assigned to, department and location will auto populate. Go to list layout, find assigned to+ click on it, go and find department and location and put in desired layout.