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 

delete all contacts which is not in OpportunityContactRole

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 of OpportunityContactRole related contact list
       list<OpportunityContactRole> roleList=[select Id,contactId from OpportunityContactRole where contactId in :ConId AND opportunityId in :oppId];
        //set to store ids of OpportunityContactRole of contact 
       set<id>cId=new set<id>();
        for(OpportunityContactRole R: roleList){
           cId.add(R.contactId);
            }
        //list of all opportunity contact role of current opportunity and contact
      
       list<contact>cont=[select id,name from contact where id=:cId];
        
        //system.debug(cont);
        //itrate over opportunity contact role list
        for(contact cnt:con){
            if(cId.Contains(cnt.Id)){
             //check if is there any opportunity contact role in list
              //dont allow to delete contact
				cnt.addError('contact cant be deleted');
            }
            
        }   
        
       
                             
        
    }
    

}

my code is working on only single record 
but when iam trying to query some contacts  into list which includes OpportunityContactRole as well as Normal contacts it should delete nomal contacts but not OpportunityContactRole
so please can  you help me that
Danish HodaDanish Hoda
Please refer below code and mark the answer as Best if it suits your requirement:
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>();
		
		//list of Contacts not in  OpportunityContactRole
		List<Contact> contaListNotInOCR = new List<Contact>();
        
        //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 of OpportunityContactRole related contact list
       list<OpportunityContactRole> roleList=[select Id,contactId from OpportunityContactRole where contactId in :ConId AND opportunityId in :oppId];
        //set to store ids of OpportunityContactRole of contact 
       set<id>cId=new set<id>();
        for(OpportunityContactRole R: roleList){
           cId.add(R.contactId);
            }
        //list of all opportunity contact role of current opportunity and contact
      
       list<contact>cont=[select id,name from contact where id=:cId];
        
        //system.debug(cont);
        //itrate over opportunity contact role list
        for(contact cnt:con){
            if(cId.Contains(cnt.Id)){
             //check if is there any opportunity contact role in list
              //dont allow to delete contact
				cnt.addError('contact cant be deleted');
            }
            else{
				contaListNotInOCR.add(cnt);
			}
        }
		if(!contaListNotInOCR.isEmpty())	delete contaListNotInOCR;
    }
}

 
AnimeLoverAnimeLover
Danish Hoda thank you very much bro

i want to ask you 
when iam querying the contacts into a list from Account1 and Account2  and Account2 has contact which is related to Opportunity contact role  when iam trying to delete that list it should delete all quried contacts of Account1 and Account2 but dont delete the Opportunity contactRle
Danish HodaDanish Hoda
Hi Rahul,
you can add the contacts from Account1 into contaListNotInOCR and delete the list.