Department is a string variable and need to autopopulate department name as per user

Momi
Tera Contributor

Kindly help and let me know how can i auto populate department name (department variable- string) for login user in service catalog. it auto populates sys id instead of department name. I need the variable single line text so that in case if some user donot have department name can manually entered. Please help.

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Momi 

 

1st thing, make the department a reference that will be better, please check in the dictionary of the department display value is true or not.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Juhi Poddar
Kilo Patron

Hello @Momi 

  • Seems like you are fetching the value using getValue() or getUniqueValue() method.
  • Try using getDiSplayValue() method.
  • It will be better if you can share the script so that I can help you modify the script.

If this helped, please like and mark it as an accepted solution. This helps future readers to locate the solution easily in community.

 

Thank You 

Juhi Poddar 

 

Ramya V
Kilo Sage

Hi @Momi,

I have used a String field named 'Department' in the catalog item form.

Script Include: Make sure that the Script Include is set to Client Callable

 

var GetUserDepartment = Class.create();
GetUserDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDepartment: function() {
var result = '';
var user = gs.getUser(); 
var departmentID = user.getDepartmentID(); 
if (!departmentID) {
result = 'Department not available';
} else {
var department = new GlideRecord('cmn_department');
if (department.get(departmentID)) {
result = department.getDisplayValue();
} else {
result = 'Department not available';
}
}
return result;
},
type: 'GetUserDepartment'
});
Catalog Client Script:
 
function onLoad() {
var ga = new GlideAjax('GetUserDepartment');
ga.addParam('sysparm_name', 'getDepartment'); 
ga.getXML(function(response) {
var department = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('department', department); //Replace 'department' with your field name
});
}
 
Please mark my answer as Correct and Helpful if this solution works for you.

ViktoriaMikoova
Tera Contributor

If you have a variable with reference to the User table that's default value is the current user, you can easily use the Auto-populate on the Department variable and specify the dot-walk to a value you want. But the Dependent question needs to be a reference in order for this approach to work. I have Requested for variable which is a reference to the user table and it's default value is the current signed in user. Below is the setting of the Department variable

ViktoriaMikoova_0-1738085470300.png