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
VICKY_SFDCVICKY_SFDC 

Update Lookp field using Apex Trigger

Here Case Is Parent and Opportunity is Child,,
1)When from Existing Case  [Without Parent Account],i create an opportunity and then select Parent account the its reflect on Opportunity Parent as well
2)But Update is Not Working,,when i change case parent  Account its not reflecting in Opportunity Acccount field.
Below is the Trigger:

public class Data {
    public static void InsertMethod(list<Case> newCas){
         set<Id> lstAccId = new set<Id>();
                set<Id> lstOppId = new set<Id>();//Opportunity Parent
       
        for(Case cas:newCas){
            if(cas.AccountId!=null){
             lstAccId.add(cas.AccountId);  
            }
           
            if(cas.Opportunity__c!=null){
             lstOppId.add(cas.Opportunity__c);  
            }
        }
            list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
            list<Opportunity> lstOpp=[Select id,AccountId from Opportunity where id IN :lstOppId];
       
        for(Account s :lstAcc){
           
            for(Opportunity op:lstOpp){
                op.AccountId=s.Id;
            }
        }
       
        update lstOpp; 
    }
   



    public static void UpdateMethod(list<Case> newCas,map<Id,Case> oldmap){
         set<Id> lstAccId = new set<Id>();
                set<Id> lstOppId = new set<Id>();//Opportunity Parent
       
    
    for(Case cas:newCas ){
            if(cas.AccountId!=null && cas.AccountId!=oldmap.get(cas.id).AccountId){
             lstAccId.add(cas.AccountId);  
            }
           
            if(cas.Opportunity__c!=null && cas.Opportunity__c!=oldmap.get(cas.id).Opportunity__c){
             lstOppId.add(cas.Opportunity__c);  
            }
        }
   
   list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
            list<Opportunity> lstOpp=[Select id,AccountId from Opportunity where id IN :lstOppId];
       
        for(Account s :lstAcc){
           
            for(Opportunity op:lstOpp){
                op.AccountId=s.Id;
            }
        }
       
        update lstOpp; 
    }
       
}

Trigger:

trigger TriggerOnCase on Case (after insert,after update) {
      If(Trigger.IsAfter){
        If(Trigger.IsInsert){
         Data.InsertMethod(Trigger.new);
        }
        If(Trigger.IsUpdate){
        Data.UpdateMethod(Trigger.new,Trigger.oldMap);
        }
    }
}
 
Best Answer chosen by VICKY_SFDC
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vikas ,

It is not updating the account on opportunity when you are updating the acccount on case because of the below line.
 
if(cas.Opportunity__c!=null && cas.Opportunity__c!=oldmap.get(cas.id).Opportunity__c){
             lstOppId.add(cas.Opportunity__c);
As you are not updating the opportunity on case so this results in null and so it is not updating the opportunity.

If you want to change the opportunity irrespective of the previous value then you can use the below line of code.
if(cas.Opportunity__c!=null );

If this solution helps, Please mark it as best answer.

Thanks,


 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vikas ,

It is not updating the account on opportunity when you are updating the acccount on case because of the below line.
 
if(cas.Opportunity__c!=null && cas.Opportunity__c!=oldmap.get(cas.id).Opportunity__c){
             lstOppId.add(cas.Opportunity__c);
As you are not updating the opportunity on case so this results in null and so it is not updating the opportunity.

If you want to change the opportunity irrespective of the previous value then you can use the below line of code.
if(cas.Opportunity__c!=null );

If this solution helps, Please mark it as best answer.

Thanks,


 
This was selected as the best answer
VICKY_SFDCVICKY_SFDC
@Sai Praveen
 Thanks for Quick Reply.
Its Working Perfectly.