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
Paul Fitzpatrick 19Paul Fitzpatrick 19 

Apex Trigger to auto-populate a Lookup Field depending on the Logged-in User's Role


I am trying to figure out a way to auto-populate a lookup field with a value on initial entry(clicking New Contact) into the Contact screen.
The lookup field is a lookup to a custom object called Project Centre   ( API Name is Project_Centre__c ). The Project Centre can take on up to 5 different record values (won't list all  5, such as "Aisling Shangan" or "Aisling Woodhazel") 

I wish to set the value in the Project Centre lookup field depending on the currently logged in User's Role. So for example, if the current logged-in User's Role is "Aisling Shangan Staff" then I wish the Project Centre lookup value on the Contact object to be set to the value of "Aisling Shangan".

I understand that writing an Apex trigger may be the only way of achieving this. I haven't written a trigger yet, so any help would be much appreciated!
Abdul RazzaqAbdul Razzaq
Just to confirm, you want to populate a lookup field(project centre) present on contact object with current logged-in user's role.
Paul Fitzpatrick 19Paul Fitzpatrick 19
Hi Abdul, Almost correct! I want to auto-populate a lookup field(project centre) present on Contact object with a specific value from the Project Centre Object data. So for example, if the Role of the current-logged in User is "Aisling Transition Staff" then populate the Project Centre lookup field with "Aisling Transition" value (which is one of the 'records' in the Project Centre Object). Hope this makes sense!