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
Pablo EscobarPablo 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

 

  1. This field to be updated is: Manager__c  
  2. 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

 

 

dphilldphill
So, that value would be something similar to timecard.Contact__r.User__c

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.
Bhawani SharmaBhawani Sharma
You will have to create a set of all contacts related to Timecard first like
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;
Pablo EscobarPablo Escobar

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

Bhawani SharmaBhawani Sharma
In your loops , use pse__Timecard_Header__c. Currently you are using Timecard_Header__c. Please paste teh error also if you get any.
Pablo EscobarPablo Escobar

 

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;
}

Bhawani SharmaBhawani Sharma
before your first for loop:
Create a set of contact ids like
Set<Id> contacts = new Set<Id>();
and use it in where clasue