How to Get logged in user department in workflow

RudhraKAM
Tera Guru

Can some one give me the code for getting the logged in user department 

I tried gs.getUser().getDepartmentID();  and getDepartmentID()  none of them worked 

1 ACCEPTED SOLUTION

RudhraKAM
Tera Guru

obj.u_department = current.opened_by.department;  is the fix 

View solution in original post

15 REPLIES 15

no luck with that 😞

Hello,

 

Try below code to get department name:

 

var gr=new GlideRecord('sys_user');

gr.addQuery('sys_id',gs.getUserId());

gr.query();

if(gr.next())

{

var department=gr.department; // should be field name of department.

var dp=new GlideRecord('cmn_department'); // this should be department table name

dp.addQuery('sys_id',department);

dp.query();

if(dp.next())

{

var departmentName=dp.name;

gs.addInfoMessage(departmentName);

gs.info("Department Name:"+departmentName);

}

}

 

Note: Please hit like, helpful or correct depending on the impact of the response.

Jim Coyne
Kilo Patron

Let's take a step back here for a moment.  A Workflow runs behind the scenes and there is no relation to a logged-in user.  It just makes no sense to get the Department of a logged-in user in this context.

What exactly are you trying o do?  Is the Workflow running on a Catalog Item or some other table?  You should be retrieving the Department from a reference variable or field on a table.

yep its running on Cat Item and thanks for responding . below code fixed my problem.

 

obj.u_department = current.opened_by.department;

RudhraKAM
Tera Guru

obj.u_department = current.opened_by.department;  is the fix