You need to sign in to do that
Don't have an account?
MLamb2005
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
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
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;
}
}
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
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