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
AnimeLoverAnimeLover 

dont allow to delete contact if it is opportunity contact role in same account

there are contacts in my account some of them are used ijn opportunity contact role 
so if iam trying to delete the contact which is used in opportunity contact role it should show error and dont allow to delete it

here is my code there is some error it is not allowing to delete all the contact
so plz help me with that
///Trigger Handler

public with sharing class ContactRoleOnOppo {
    //class with sharing rule of context user
    
    public static void  checkContactRole(list<Contact> con){
        
        //set For  store accountIds of current contacts
        set<id>accId=new set<id>();
        
        //set for store contact ids from current contac list
        set<id>ConId=new set<id>();
        
        //iterate loop for add accountId and contact Id in set
        for(contact co:con){
            accId.add(co.AccountId);
            conId.add(co.Id);
             }
        
        //list of opportunity related account list
        list<Opportunity>opp=[select Id,Name from opportunity where AccountId=:accId];
        
        //set to store ids of opportunity of contact account
        set<id>oppId=new set<id>();
        for(Opportunity op:opp){
            oppId.add(op.ID);
            }
        
        list<OpportunityContactRole> roleList=[select Id from OpportunityContactRole where opportunityId=:oppId];
        //list of all opportunity contact role of current opportunity and contact
        //itrate over opportunity contact role list
        for(contact c:con){
            //check if is there any opportunity contact role in list
          if(roleList != Null){
              //dont allow to delete contact
          c.addError('cant be deleted contact it is related to Opportunity contact role...!');
        }   
        }
       
       // list<Account>ac=[select Id,Name from account where Id=:sId];
        //list<OpportunityContactRole> roleList=[select Id from OpportunityContactRole where contactId=:conId];
        
                                             
        
    }

}
//Trigger

trigger CheckOppoContRole on Contact (before delete) {
////call method from handler and pass old record

    ContactRoleOnOppo.checkContactRole(trigger.old);

}

 
Best Answer chosen by AnimeLover
Ajay K DubediAjay K Dubedi
Hi Rahul,

I have gone through your question. Please try the below code - 
 
//Trigger - 

trigger OnContactTrigger on Contact(before delete) {
    if(Trigger.isBefore&&Trigger.isDelete) {
        OnContactHandler.preventDelete(Trigger.old);
    }
}


//Handler Class - 

public class OnContactHandler {
    public static void  preventDelete(list<Contact> conListOld){                
        set<id>accId=new set<id>();                
        set<id>ConId=new set<id>();
        set<id>oppId=new set<Id>();
        Set<Id> ocrId = new Set<Id>();
        
        list<Opportunity> oppList = [select Id,Name,AccountId from opportunity where AccountId IN :accId];
        list<OpportunityContactRole> ocrList = [select Id,opportunityId,contactId from OpportunityContactRole where opportunityId IN :oppId];
        
        for(contact con:conListOld) {
            accId.add(con.AccountId);
            conId.add(con.Id);
        }
        
        
        for(Opportunity opp:oppList) {
            oppId.add(opp.Id);
        }
        
        
        
        for(OpportunityContactRole ocr : ocrList){
            ocrId.add(ocr.ContactId);
        }
        
        for(Contact con: conListOld){
            if(ocrId.Contains(con.Id)){
                con.addError('Can not delete contact!');
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Rahul,

I have gone through your question. Please try the below code - 
 
//Trigger - 

trigger OnContactTrigger on Contact(before delete) {
    if(Trigger.isBefore&&Trigger.isDelete) {
        OnContactHandler.preventDelete(Trigger.old);
    }
}


//Handler Class - 

public class OnContactHandler {
    public static void  preventDelete(list<Contact> conListOld){                
        set<id>accId=new set<id>();                
        set<id>ConId=new set<id>();
        set<id>oppId=new set<Id>();
        Set<Id> ocrId = new Set<Id>();
        
        list<Opportunity> oppList = [select Id,Name,AccountId from opportunity where AccountId IN :accId];
        list<OpportunityContactRole> ocrList = [select Id,opportunityId,contactId from OpportunityContactRole where opportunityId IN :oppId];
        
        for(contact con:conListOld) {
            accId.add(con.AccountId);
            conId.add(con.Id);
        }
        
        
        for(Opportunity opp:oppList) {
            oppId.add(opp.Id);
        }
        
        
        
        for(OpportunityContactRole ocr : ocrList){
            ocrId.add(ocr.ContactId);
        }
        
        for(Contact con: conListOld){
            if(ocrId.Contains(con.Id)){
                con.addError('Can not delete contact!');
            }
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
AnimeLoverAnimeLover
Thank you so much
 
Deepali KulshresthaDeepali Kulshrestha
Hi Rahul,

I've read your code and Use below code:

Apex trigger-->

trigger ContactRoleOnOpp on Contact (before delete) 
{
  if(Trigger.isDelete && Trigger.isBefore)
  {
      ContactRoleOnOppHandler.checkAccountIsSameOrNot(Trigger.Old);
  }
}


Apex Class-->


public class ContactRoleOnOppHandler
{
    public static void checkAccountIsSameOrNot(List<Contact> allcontacts)
    {
        try
        {
            Map<id,id> contactAccountMap=new Map<id,id>();
            Map<id,id> IdOfcontactOpportunityMap=new Map<id,id>();
            Set<id> contactId=new Set<id>();
            Map<id,Contact>  contactMap=new Map<id,Contact>();
            
            for(Contact con:allcontacts)
            {
                if(con.AccountId!=null)
                {
                    contactId.add(con.id);
                    contactMap.put(con.id,con);
                    contactAccountMap.put(con.id,con.AccountId);
                } 
            }
            
            System.debug('contactMap---'+contactMap);
            System.debug('contactAccountMap---'+contactAccountMap);
            
            List<OpportunityContactRole> AllcontactRoles=new List<OpportunityContactRole>([select id,OpportunityId,ContactId from OpportunityContactRole where ContactId IN:contactId]);
            Set<Id> OpportunityId=new Set<Id>();
            
            for(OpportunityContactRole oppRole:AllcontactRoles)
            {
                IdOfcontactOpportunityMap.put(oppRole.ContactId,oppRole.OpportunityId);
                OpportunityId.add(oppRole.OpportunityId);
            }
            
            List<Opportunity> allOpportunity=new List<Opportunity>([select id,AccountId from Opportunity where Id IN:OpportunityId]);
            Map<Id,Id> IdOfOpportunityAccountMap=new Map<Id,Id>();
            
            for(Opportunity opp:allOpportunity)
            {
                IdOfOpportunityAccountMap.put(opp.Id,opp.AccountId);
            }
            
            for(Contact con:allcontacts)
            {
                if(contactMap.containsKey(con.Id))
                {
                    if(contactAccountMap.get(con.Id)==IdOfOpportunityAccountMap.get(IdOfcontactOpportunityMap.get(con.Id)))
                    {
                        con.AddError('Cant Delete the Contact');
                    }
                }
            }
            
    
        }
        catch(Exception e)
        {
            System.debug('Error in-->'+e.getLineNumber()+'and Error is-->'+e.getMessage());
        }
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com