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
MLamb2005MLamb2005 

Using Apex to update a Lookup (new to Triggers)

Hello all,

I have a process where my users enter project hours as Lab Project Hour records.  For naming purposes, right now I look at the Current User and just user their name as text on the new Lab Project Hour record.  However, I need to modify it where each Lab Project Hour is actually named with a Lookup to my custom Employee object. 

On each Employee object I have a field (Employee Page ID) that contains the unique ID of each user's corresponding Employee page.  So, what I need the Trigger to do is:

 - When each Lab Project Record gets created,
 - Determine the Current User and access their User record
 - Get the Employee Page ID information from the current user's User page
 - Take the Employee Page ID and place it into the Employee field on the new Lab Project Record

What is the best way to accomplish this?  Can you point me in some direction to get started?  I've never written a trigger before, but I'm comfortable coding in several languages, so the learning curve shouldn't be too steep.

Thanks a ton,
Matt
NaishadhNaishadh
Hi

Hope this will help!

trigger labprojectTrigger on labProject (before insert,before update) {
          List<String> userId = new List<String> ();
          for(labProject lb : Trigger.new) {
                      userId.add(lb.userId);
          }

          Map<Id,UserRecord> userMap = [select Id,employeePageId from userRecord where Id : userId];

          for(labProject lb : Trigger.new) {
                      UserRecord urTemp =    userMap.get(lb.userId);
                      lb.employeeId = urTemp.employeePageId;
          }
}
MLamb2005MLamb2005
Thanks!  I've gotten this far...

Code:
trigger updateEmployeeOnProjectHour on Lab_Project_Hours__c (before insert, before update) {
    List<String> userId = new List<String> ();
    for(Lab_Project_Hours__c lb : Trigger.new) {
        userId.add(lb.Id);                         //I have no idea what this line does?
 } Map<Id,User> userMap = [select Id, Employee_Page__c from User where Id in :userId]; for(Lab_Project_Hours__c lb : Trigger.new) { User urTemp = userMap.get(lb.userId); lb.Employee_Name__c = urTemp.Employee_Page__c; } }
 
I resolved several errors on my own, but I can't figure this one out: Error: Compile Error: Illegal assignment from LIST:SOBJECT:User to MAP:Id,SOBJECT:User at line 7 column 13

Any ideas?
Matt
 

MLamb2005MLamb2005
So I started to rethink this, and I came up with the following code that works properly.  Does anyone see any potential pitfalls to this?  The nature of these records is such that they will (almost) never be uploaded or updated in mass.  Each one is created individually by my users.

Code:
trigger updateEmployeeOnProjectHour on Lab_Project_Hours__c (before insert, before update) {
    
    String currentUserId = UserInfo.getUserId();
    
    User thisUser = [select thisUser.Employee_Page__c from User thisUser where (thisUser.Id =: currentUserId)];
    
    for(Lab_Project_Hours__c lb : Trigger.new) {
        lb.Employee_Name__c = thisUser.Employee_Page__c;
    }

}

 

NaishadhNaishadh
Hi,

Your code is perfectly fine. Reason for using map is for bulk operation and the error which you was facing is due to the improper map initialization
it should be

Map<Id,User> userMap = new Map<Id,User> ([]);

thanks