+ Start a Discussion
Liz MalmanLiz Malman 

Auto Populating a field from two different objects

Hi all,

I have two object: Events and Opportunities. What I would like to have happen is the following:

When the field Engineer__c is filled out on an Opportunity, I would like for the related Events field Client_Services__c field to be filled out with the same content. 

I know this will most likey be done with an Apex Trigger but I need some help with the code. In the past I have successfully done something similar but they were both the same object type. It seems the two different object types make it more complicated. 

Any insight would be appreciated.

Thanks,
Liz
Best Answer chosen by Liz Malman
Raj VakatiRaj Vakati
small mistake .. you need to use after insert trigger  .. In case of begore insert this wnt work checkRecursive.SetOfIDs.contains(o.Id)
 
trigger ClientServicesEventsTrigger on Opportunity (after insert) {
    list<Events__c> EvtsToUpdate = new list<Events__c>();
    
    for (Opportunity o:Trigger.new){
        if (o.Engineer__c != null && !checkRecursive.SetOfIDs.contains(o.Id)){
                Events__c evt = new Events__c();
                evt.Id = o.Events__c;
                evt.Client_Services__c = o.Engineer__c;
                EvtsToUpdate.add(evt);
        }
    }    
    if(EvtsToUpdate.size() > 0){
        update EvtsToUpdate;
    }

 

All Answers

Liz MalmanLiz Malman
This is my current code and nothing heppens when I update an opportunity's engineer field.

trigger ClientServicesEventsTrigger on Opportunity (before insert) {
    list<Events__c> EvtsToUpdate = new list<Events__c>();
    
    for (Opportunity o:Trigger.new){
        if (o.Engineer__c != null && !checkRecursive.SetOfIDs.contains(o.Id)){
                Events__c evt = new Events__c();
                evt.Id = o.Events__c;
                evt.Client_Services__c = o.Engineer__c;
                EvtsToUpdate.add(evt);
        }
    }    
    if(EvtsToUpdate.size() > 0){
        update EvtsToUpdate;
    }    
Liz MalmanLiz Malman
I have no errors and successfully ran a test with 100% coverage but I had no success in actually updating the Client Services field on my event object.
 
Raj VakatiRaj Vakati
small mistake .. you need to use after insert trigger  .. In case of begore insert this wnt work checkRecursive.SetOfIDs.contains(o.Id)
 
trigger ClientServicesEventsTrigger on Opportunity (after insert) {
    list<Events__c> EvtsToUpdate = new list<Events__c>();
    
    for (Opportunity o:Trigger.new){
        if (o.Engineer__c != null && !checkRecursive.SetOfIDs.contains(o.Id)){
                Events__c evt = new Events__c();
                evt.Id = o.Events__c;
                evt.Client_Services__c = o.Engineer__c;
                EvtsToUpdate.add(evt);
        }
    }    
    if(EvtsToUpdate.size() > 0){
        update EvtsToUpdate;
    }

 
This was selected as the best answer
Liz MalmanLiz Malman
Thanks for the response, Raj. Yea I figured this out late last night but I forgot to post on here. Thansk for the answer though, I appreciate it!

Liz