function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lapaullapaul 

How to get $User.Department in Apex class

HI,

I want to get the information from $User.Department and assign it to a variable in the Apex class.

However, I don't know how to do this. Basically I want to get the user department object info and then

prefill the custom object field Department__c. Please help.

 

thanks

Paul

 

srisomsrisom
 User u = [Select Department From User Where id = :UserInfo.getUserId()];

String dept = u.Department;

 

That will get the department.

 

Then in your custom object:

 

Cust_obj_c obj = new Cust_obj_c(Department__c = dept); // plus any other fields here

 insert obj;

 

hope this helps !!

lapaullapaul

Thanks so much. it worked.

 

Paul