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
Liz MalmanLiz Malman 

Auto Populating Lookup Field from another Lookup Field

I am trying to auto populate a lookup field using an apex trigger and could use some help. My case only uses opportunity objects. On each opportunity there are 2 lookup fields. Example: Once lookup_field_A on Opp1 gets filled with Opp2, I would like to fill Opp2's lookup_field_B with Opp1. 

I've started writing this code out but I'm unsure how to grab Opp2 from lookup_field_A and then set lookup_field_B to be Opp1.

Anyone have any ideas? Any insight would be helpful.
Best Answer chosen by Liz Malman
Waqar Hussain SFWaqar Hussain SF
First of all create a new class to avoid recursion of the trigger.
New class
public class checkRecursive {
     public static Set<Id> SetOfIDs = new Set<Id>();
}

then try below code
trigger PreviousRenewalOpportunity on Opportunity (after insert, after update) {
    list<Opportunity> OppsToUpdate = new list<Opportunity>();
    
    for (Opportunity o:Trigger.new){
        if (o.Upcoming_Renewal_Opportunity__c != null && !checkRecursive.SetOfIDs.contains(o.Id)){
            If(){
                Opportunity opp = new Opportunity();
                opp.Id = o.Upcoming_Renewal_Opportunity__c;
                Opp.Previous_Renewal_Opportunity__c = o.Id;
                OppsToUpdate.add(opp);
            }
        }
    }
    
    if(OppsToUpdate.size() > 0)
    update OppsToUpdate;
}

 

All Answers

Waqar Hussain SFWaqar Hussain SF
Can you please share your code?
Liz MalmanLiz Malman
trigger PreviousRenewalOpportunity on Opportunity (before insert, before update, after insert) {
    for (Opportunity o:Trigger.new) 
        if (o.Upcoming_Renewal_Opportunity__c != null)
            Opportunity opp = [SELECT Id, Name, Upcoming_Renewal_Opportunity__c FROM Opportunity WHERE Id = :o.Id];
            //opp.Previous_Renewal_Opportunity__c = Opportunity o;
            //List<Opportunity> opportunities = new List<Opportunity>();
            //opportunities.add(opp.opportunity__r);
            //o.Previous_Renewal_Opportunity__c = o.Upcoming_Renewal_Opportunity__c;
           
}

Anything that is commented out, is something I've tried but was unsuccessful. Thank you for the response!!
Waqar Hussain SFWaqar Hussain SF
First of all create a new class to avoid recursion of the trigger.
New class
public class checkRecursive {
     public static Set<Id> SetOfIDs = new Set<Id>();
}

then try below code
trigger PreviousRenewalOpportunity on Opportunity (after insert, after update) {
    list<Opportunity> OppsToUpdate = new list<Opportunity>();
    
    for (Opportunity o:Trigger.new){
        if (o.Upcoming_Renewal_Opportunity__c != null && !checkRecursive.SetOfIDs.contains(o.Id)){
            If(){
                Opportunity opp = new Opportunity();
                opp.Id = o.Upcoming_Renewal_Opportunity__c;
                Opp.Previous_Renewal_Opportunity__c = o.Id;
                OppsToUpdate.add(opp);
            }
        }
    }
    
    if(OppsToUpdate.size() > 0)
    update OppsToUpdate;
}

 
This was selected as the best answer
Liz MalmanLiz Malman
The second if statement is empty and causing an error. When I commented out that if statement I got a long error when I tested it:PreviousRenewalOpportunity: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0061C00000Yo7Y9QAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PreviousRenewalOpportunity: maximum trigger depth exceeded Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate Opportunity trigger event AfterUpdate: [] Trigger.PreviousRenewalOpportunity: line 14, column 1

So do I need to add a condition to that if statement? 
Liz MalmanLiz Malman
Also, thanks again for the help! Much appreciated.
Liz MalmanLiz Malman
I believe it is an issue when updating the list, OppsToUpdate, in the last line. 
Liz MalmanLiz Malman
Got it to work!! So awesome! Thank you! 
Waqar Hussain SFWaqar Hussain SF
Great