You need to sign in to do that
Don't have an account?
Pablo Escobar
APEX trigger help / Update a Lookup field
Hi I'm new developing appex triggers and i can't make a trigger to work
I have to update a field on a custom object called pse__Timecard_Header__c The problem is that the field to update is a lookup field
- This field to be updated is: Manager__c
- The manager info is located in the salesforce standard object caled "User" and the field name is called "Manager"
The Problem is that there is no direct link between the "user" object and my custom object called "timecard"
There is a looup field at the timecard to a custom "contact" and from the contact there is a direct link to the salesforce "User"
Summary: Timecard -> Contact -> Salesforce User
Thanks!!
Pablo
Go into Eclipse, and the schema. Find the Timecard object and expand fields through the rows until you get through contact and find the User field on the Contact. Check that box and it should give you the the fieldname to use from the timecard object.
for(TimeCard tC : Trigger.new){ constacts.add(tC.Contact__c);}
After that, you need to fetch user data from Contact objetc like
Map<Id, Contact> mapContacts = new Map<Id, Contact>([Select Id, User__c from Contact where Id IN: Contacts]);
Again loop your TimeCard record and set user data like
for(TimeCard tC : Trigger.New)
tC.UserManager__c = mapContact(tC.Contact__c).User__c;
Hi Tech Force,
Thanks for your help!
I'm getting an error on my first line
trigger Update_timecard_manager on pse__Timecard_Header__c (before insert, before update) {
for(Timecard_Header tC : Trigger.new){ constacts.add(tC.pse__Resource__c);}
Map<Id, Contact> mapContacts = new Map<Id, Contact>([Select Id, pse__Salesforce_User__c from Contact where Id IN: Contacts]);
for(Timecard_Header tC : Trigger.New) tC.Manager__c = mapContact(tC.pse__Resource__c).pse__Salesforce_User__c;
}
What do you think needs to be fixed?
Thanks again!
Pablo
Done, but i got an error where it says contacts .. is that ok that the contact object name includes an "S" at the end?
Thanks
Pablo
trigger Update_timecard_manager on pse__Timecard_Header__c (before insert, before update) {
for(pse__Timecard_Header__c tC : Trigger.new){ constacts.add(tC.pse__Resource__c);}
Map<Id, Contact> mapContacts = new Map<Id, Contact>([Select Id, pse__Salesforce_User__c from Contact where Id IN: Contacts]);
for(pse__Timecard_Header__c tC : Trigger.New) tC.Manager__c = mapContact(tC.pse__Resource__c).pse__Salesforce_User__c;
}
Create a set of contact ids like
Set<Id> contacts = new Set<Id>();
and use it in where clasue