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
Alex ShAlex Sh 

Update Contact with opportunity LOST

Hi All,
Can someone help me with this.

I need functional that will select the contact and check statuses for all related Opportunities. If all Opportunities are LOST, then make a checkbox "All Opp Lost" on Contact record as True.
Probably it will be a trigger for Insert/Update /Delete on Opportunity object, but how to implement the logic.

Thanks in advance!
Best Answer chosen by Alex Sh
Piyush Gautam 6Piyush Gautam 6
Hi Alex,

As per your requirement, I have developed a code. Please have a look.
trigger updateContactOnOpportunity on Opportunity (after insert, after update, before delete) {
    
    list<Opportunity> oppList= trigger.isDelete? trigger.old : trigger.new;
    Set<Id> contactId= new Set<Id>();
    
    Map<id, Opportunity> oppMap= new Map<id, Opportunity>([SELECT id , (SELECT id, contactId from OpportunityContactRoles) FROM Opportunity]);
    Map<id, OpportunityContactRole> oppConMap=  new Map<id, OpportunityContactRole>([SELECT id, opportunityId, contactId FROM OpportunityContactRole]);
    
    for(opportunity opp: oppList){
            
           list<OpportunityContactRole> oppConList= oppMap.get(opp.Id).OpportunityContactRoles;
            if(opp.StageName=='Closed Lost' || trigger.isDelete){
                        for(OpportunityContactRole oppCon : oppConList){
							contactId.add(oppCon.contactId);
                        }
            }
        }
    
    Map<Id, contact> conMap= new Map<Id, contact>([SELECT Id, (SELECT Id, opportunity.StageName from OpportunityContactRoles) FROM contact]);
    List<Contact> conList= [SELECT Id, AllOppLost__c FROM Contact where Id IN :contactId];
    Set<Contact> conLostList= new Set<Contact>();
    Set<Contact> conWonList= new Set<Contact>();
    Set<Contact> conDelLastList= new Set<Contact>();
    
    for(Contact con: conList){
        List<OpportunityContactRole> oppContList=conMap.get(con.Id).OpportunityContactRoles;
        String isBoolean= 'True';
        Integer lastDelete= 0;
        
        for(OpportunityContactRole oppCon :oppContList){
            if(oppcon.opportunity.StageName =='Closed Lost'){
                isBoolean= 'False';
            }
            else if(oppcon.opportunity.StageName !='Closed Lost'){
                isBoolean= 'True';
            }
            
            if(trigger.isDelete){
                lastDelete++;
            }
        }
        
        if(isBoolean== 'False'){
            conLostList.add(con);
        }
        else{
            conWonList.add(con);
        }
        
        if(lastDelete<2){
            conDelLastList.add(con);
        }
    }
    
     conLostList.removeAll(conDelLastList);
     conWonList.removeAll(conDelLastList);


    List<Contact> conToUpdateList= new List<Contact>();    
    if(conLostList.size()>0){
        
        for(Contact con: conLostList){
            con.AllOppLost__c= True;
            conToUpdateList.add(con);
            }
        }

    if(conWonList.size()>0){
        
        for(Contact con: conWonList){
            con.AllOppLost__c= False;
            conToUpdateList.add(con);
            }
        }    

    if(conDelLastList.size()>0){
        
        for(Contact con: conDelLastList){
            con.AllOppLost__c= False;
            conToUpdateList.add(con);
            }
        }     
   
    if(conToUpdateList.size()>0){
        update conToUpdateList;
    }
}

I hope this will fulfill your requirement.
If you found it helping then mark as solved.

Thanks