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 RELATED OPPORTUNITY ACCOUNT NAME ON CASE OBJECT

REQUIREMENT:

WE HAVE A CASE WITH ACCOUNT,

IN CASE RELATED LIST ,WHEN WE CREATE AN OPPORTUNITY 
THEN IT  ACCOUNT NAME SHOULD BE AUTOMATIC UPDATED AS CASE ACCOUNT NAME.

ALSO WHEN I UPDATE THE CASE ACCOUNT FIELD,,IT SHOULD REFLECT IN THE RELATED OPPORTUNITY OBJECT
Best Answer chosen by VICKY_SFDC
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vikas,

Yes we can write a single handler as below.

Handler
public class triggerHandler {
    public  void insertoppy ( list<Opportunity> oppylist){
        
        Set<Id> caseid = new Set<Id>();
    Map<Id, Id> conAccMap = new Map<Id,Id>();

    for(Opportunity o :oppylist) {
        caseid.add(o.case__c);
    }
    
    for(Case c: [SELECT Id, AccountId From case WHERE Id IN:caseid]){
        conAccMap.put(c.Id, c.AccountId);
    }
    
    for(Opportunity ol :oppylist) {
        ol.AccountId = conAccMap.get(ol.case__c);
    }     
    }
    
    public void updatecase(List<case> caselist){
        List<opportunity> opplist= new List<opportunity>();
           List<opportunity> opplist1= new List<opportunity>();
       
    
    for(Case cas: [SELECT Id,AccountId,(SELECT Id,name,accountid FROM opportunities__r) FROM Case WHERE Id in : caselist] ){
        if(cas.opportunities__r.size()>0) {
           opplist.addall(cas.opportunities__r);
        } 
    
        for (opportunity opp:opplist){
            opp.AccountId=cas.accountid;
            opplist1.add(opp);
        }
}
        update opplist1;
    }
    
}
trigger on case
 
trigger TriggerOnCase on Case (after update) {
    TriggerHandler th= new Triggerhandler();
      If(Trigger.IsAfter && Trigger.IsUpdate ){

    th.updatecase(Trigger.new);
    }
}

Trigger on opportunity
trigger TriggeronOpportunity on Opportunity (before insert) {
    TriggerHandler th= new Triggerhandler();
    if (Trigger.isbefore && Trigger.isinsert){
        th.insertoppy(Trigger.new);
    }
    
}

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vikas,

Here we have to write triggers on two different object.

For creation of opportunity on case we have to write the trigger on opportunity as below.
trigger TriggeronOpportunity on Opportunity (before insert,before update) {
    Set<Id> caseid = new Set<Id>();
    Map<Id, Id> conAccMap = new Map<Id,Id>();
    if(Trigger.isinsert && Trigger.isbefore)
    {
    for(Opportunity o : Trigger.New) {
        caseid.add(o.case__c);
    }
    
    for(Case c: [SELECT Id, AccountId From case WHERE Id IN:caseid]){
        conAccMap.put(c.Id, c.AccountId);
    }
    
    for(Opportunity ol : Trigger.New) {
        ol.AccountId = conAccMap.get(ol.case__c);
    }     
    }
}

For update of the account field on the case object we have to write trigger on case object as below.
 
trigger TriggerOnCase on Case (after update) {
      If(Trigger.IsAfter){
        If(Trigger.IsUpdate){
    List<opportunity> opplist= new List<opportunity>();
           List<opportunity> opplist1= new List<opportunity>();
       
    
    for(Case cas: [SELECT Id,AccountId,(SELECT Id,name,accountid FROM opportunities__r) FROM Case WHERE Id in : Trigger.new] ){
        if(cas.opportunities__r.size()>0) {
           opplist.addall(cas.opportunities__r);
        } 
    
        for (opportunity opp:opplist){
            opp.AccountId=cas.accountid;
            opplist1.add(opp);
        }
}
        update opplist1;
        }
    }
}

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

Thanks,
 
VICKY_SFDCVICKY_SFDC
@sai Praveen

Thanks Bro thats working perfectly.

Bro is there any possibility that we can merge this 2 code in one Handler class and call that handler class with help of apex trigger?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vikas,

Yes we can write a single handler as below.

Handler
public class triggerHandler {
    public  void insertoppy ( list<Opportunity> oppylist){
        
        Set<Id> caseid = new Set<Id>();
    Map<Id, Id> conAccMap = new Map<Id,Id>();

    for(Opportunity o :oppylist) {
        caseid.add(o.case__c);
    }
    
    for(Case c: [SELECT Id, AccountId From case WHERE Id IN:caseid]){
        conAccMap.put(c.Id, c.AccountId);
    }
    
    for(Opportunity ol :oppylist) {
        ol.AccountId = conAccMap.get(ol.case__c);
    }     
    }
    
    public void updatecase(List<case> caselist){
        List<opportunity> opplist= new List<opportunity>();
           List<opportunity> opplist1= new List<opportunity>();
       
    
    for(Case cas: [SELECT Id,AccountId,(SELECT Id,name,accountid FROM opportunities__r) FROM Case WHERE Id in : caselist] ){
        if(cas.opportunities__r.size()>0) {
           opplist.addall(cas.opportunities__r);
        } 
    
        for (opportunity opp:opplist){
            opp.AccountId=cas.accountid;
            opplist1.add(opp);
        }
}
        update opplist1;
    }
    
}
trigger on case
 
trigger TriggerOnCase on Case (after update) {
    TriggerHandler th= new Triggerhandler();
      If(Trigger.IsAfter && Trigger.IsUpdate ){

    th.updatecase(Trigger.new);
    }
}

Trigger on opportunity
trigger TriggeronOpportunity on Opportunity (before insert) {
    TriggerHandler th= new Triggerhandler();
    if (Trigger.isbefore && Trigger.isinsert){
        th.insertoppy(Trigger.new);
    }
    
}

Thanks,
 
This was selected as the best answer
VICKY_SFDCVICKY_SFDC
@sai Praveen Thanks for your Qucik response Response