You need to sign in to do that
Don't have an account?

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
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); }
I have gone through your question. Please try the below code -
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
I have gone through your question. Please try the below code -
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
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