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 Lookup Field From Child To Parent Using Trigger

Senario:

Opportunity is child and case is Parent Object.
When New Opportunity Is Created along with associated Account,,,and associated case object.
that account Lookup name should be autoPopulate in Case Object of Account Lookup Field.

I create a Lookup Relation where Opportunity is Child and Case is Parent.


ERROR:
TriggerOnOpportunity: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 5005g00000DAPbrAAH Class.Data.insertOpportunity: line 16, column 1 Trigger.TriggerOnOpportunity: line 3, column 1

FIELDS IN HANDLER CLASS:
MY_ACC__c-->On case Object(Text(20))
Case__c-->Looupfield on Opportunity



Handler class:


public class Data{
    
    
    public static void insertOpportunity(List<opportunity> oppList)
    {
        List<opportunity> opp = [select id,Case__r.id,Case__r.MY_ACC__c,Account.name, Accountid from opportunity where id=:oppList ];
        system.debug(opp[0].Case__r.id);
        system.debug('oppList'+oppList);
        if(opp[0].Case__r.id != null){
        List<case> casselist = [select id, MY_ACC__c from case where id=:opp[0].Case__r.id LIMIT 1];
            case c = new case();
            c.Id = casselist[0].id;
            c.MY_ACC__c = opp[0].Account.name;
            casselist.add(c);
      //  casselist[0].MY_ACC__c = opp[0].Account.name;
        Upsert casselist;
        }
    }
    
}


Trigger Class:

trigger TriggerOnOpportunity on Opportunity (after insert, after update) {
    if (Trigger.isAfter    && Trigger.isInsert || Trigger.isUpdate){     
    Data.insertOpportunity(Trigger.new);
    }
       
   }
Best Answer chosen by VICKY_SFDC
CharuDuttCharuDutt
Hii VikasKumar
Try Below TriggerHandlerClass
public class Data{
    public static void InsertMethod(list<Opportunity> newOpp){
        set<Id> lstAccId = new set<Id>();
    	set<Id> lstCaseId = new set<Id>();
    
    	 for(Opportunity opp : newOpp){
             if(opp.AccountId != null){
                 lstAccId.add(opp.AccountId);
            	}
             if(opp.Case__c != null){
                 lstAccId.add(opp.Case__c);
            	}
    		}   
    	
    	  	list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
    		list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
    			for(Account s :lstAcc){
        
        		for(Case c : lstCase){
            		c.AccountId = s.Id;
        		}
    		}

    update lstCase;    
    }
    public static void UpdateMethod(list<Opportunity> newOpp,map<Id,Opportunity> oldmap){
        set<Id> lstAccId = new set<Id>();
    	set<Id> lstCaseId = new set<Id>();
    
    	for(Opportunity opp : newOpp){
             if(opp.AccountId != null && opp.AccountId != oldmap.get(opp.Id).AccountId){
                 lstAccId.add(opp.AccountId);
            	}
                if(opp.Case__c != null && opp.Case__c != oldmap.get(opp.Id).Case__c){
                 lstCaseId.add(opp.Case__c);
            	}
    		}   
    	
    	  	list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
    		list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
    			for(Account s :lstAcc){
        
        		for(Case c : lstCase){
            		c.AccountId = s.Id;
        		}
    		}

    update lstCase;    
    }
}
----------------------------------------------------
trigger OppTrigger on Opportunity (after insert,after update) {
  
    If(Trigger.IsAfter){
        If(Trigger.IsInsert){
         Data.InsertMethod(Trigger.new);
        }
        If(Trigger.IsUpdate){
         Data.UpdateMethod(Trigger.new,Trigger.oldMap);   
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 

All Answers

CharuDuttCharuDutt
Hii VikasKumar
Try below Trigger
trigger dfsdfsdas on Opportunity (after insert,after update) {
    set<Id>lstAccId = new Set<Id>();
    set<Id>lstCaseId = new Set<Id>();
    If(Trigger.IsAfter){
        If(Trigger.IsInsert){
         for(Opportunity opp : trigger.new){
             if(opp.AccountId != null){
                 lstAccId.add(opp.AccountId);
            	}
             if(opp.Case__c != null){
                 lstAccId.add(opp.Case__c);
            	}
    		}   
        }
        If(Trigger.IsUpdate){
            for(Opportunity opp : trigger.new){
             if(opp.AccountId != null && opp.AccountId != Trigger.oldMap.get(opp.Id).AccountId){
                 lstAccId.add(opp.AccountId);
            	}
                if(opp.Case__c != null && opp.Case__c != Trigger.oldMap.get(opp.Id).Case__c){
                 lstCaseId.add(opp.Case__c);
            	}
    		}   
        }
    }
    list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
    list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
    for(Account s :lstAcc){
        
        for(Case c : lstCase){
            c.AccountId = s.Id;
        }
    }

    update lstCase;
}
Please Mark It As Best Answer If It Helps
Thank You!
 
CharuDuttCharuDutt
Hii VikasKumar
Try Below TriggerHandlerClass
public class Data{
    public static void InsertMethod(list<Opportunity> newOpp){
        set<Id> lstAccId = new set<Id>();
    	set<Id> lstCaseId = new set<Id>();
    
    	 for(Opportunity opp : newOpp){
             if(opp.AccountId != null){
                 lstAccId.add(opp.AccountId);
            	}
             if(opp.Case__c != null){
                 lstAccId.add(opp.Case__c);
            	}
    		}   
    	
    	  	list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
    		list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
    			for(Account s :lstAcc){
        
        		for(Case c : lstCase){
            		c.AccountId = s.Id;
        		}
    		}

    update lstCase;    
    }
    public static void UpdateMethod(list<Opportunity> newOpp,map<Id,Opportunity> oldmap){
        set<Id> lstAccId = new set<Id>();
    	set<Id> lstCaseId = new set<Id>();
    
    	for(Opportunity opp : newOpp){
             if(opp.AccountId != null && opp.AccountId != oldmap.get(opp.Id).AccountId){
                 lstAccId.add(opp.AccountId);
            	}
                if(opp.Case__c != null && opp.Case__c != oldmap.get(opp.Id).Case__c){
                 lstCaseId.add(opp.Case__c);
            	}
    		}   
    	
    	  	list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
    		list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
    			for(Account s :lstAcc){
        
        		for(Case c : lstCase){
            		c.AccountId = s.Id;
        		}
    		}

    update lstCase;    
    }
}
----------------------------------------------------
trigger OppTrigger on Opportunity (after insert,after update) {
  
    If(Trigger.IsAfter){
        If(Trigger.IsInsert){
         Data.InsertMethod(Trigger.new);
        }
        If(Trigger.IsUpdate){
         Data.UpdateMethod(Trigger.new,Trigger.oldMap);   
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
This was selected as the best answer
VICKY_SFDCVICKY_SFDC
@Charu ,Thanks for this code.No Error but there is a small Correction that required.
               So i Updated 


Handler:

public class Data{
    public static void InsertMethod(list<Opportunity> newOpp){
        set<Id> lstAccId = new set<Id>();
        set<Id> lstCaseId = new set<Id>();
    
         for(Opportunity opp : newOpp){
             if(opp.AccountId != null){
                 lstAccId.add(opp.AccountId);
                }
             if(opp.Case__c != null){
              
                  lstCaseId.add(opp.Case__c);
                }
            }   
        
              list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
            list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
                for(Account s :lstAcc){
        
                for(Case c : lstCase){
                    c.AccountId = s.Id;
                }
            }

    update lstCase;    
    }
    public static void UpdateMethod(list<Opportunity> newOpp,map<Id,Opportunity> oldmap){
        set<Id> lstAccId = new set<Id>();
        set<Id> lstCaseId = new set<Id>();
    
        for(Opportunity opp : newOpp){
             if(opp.AccountId != null && opp.AccountId != oldmap.get(opp.Id).AccountId){
                 lstAccId.add(opp.AccountId);
                }
                if(opp.Case__c != null && opp.Case__c != oldmap.get(opp.Id).Case__c){
                 lstCaseId.add(opp.Case__c);
                }
            }   
        
              list<Account> lstAcc = [Select id from Account where id IN :lstAccId];
            list<Case> lstCase = [Select id,AccountId from Case where id IN :lstCaseId];
                for(Account s :lstAcc){
        
                for(Case c : lstCase){
                    c.AccountId = s.Id;
                }
            }

    update lstCase;    
    }
}

Trigger:

trigger OppTrigger on Opportunity (after insert,after update) {
  
    If(Trigger.IsAfter){
        If(Trigger.IsInsert){
         Data.InsertMethod(Trigger.new);
        }
        If(Trigger.IsUpdate){
         Data.UpdateMethod(Trigger.new,Trigger.oldMap);   
        }
    }

}