Hi all I have written a trigger but i want it to fire only when there is update in IsActive field else it should not fire so please could you help me This is my trigger
public static void processAccounts(Map<Id, Account> accMap,Map<Id, Account> accoldMap,LIst<Account>accList) {
Boolean status;
Set<Id> accountsWithNew = new Set<Id>();
Set<Id> accountsWithRenew = new Set<Id>();
List<Contact> contactsToCheck =[SELECT Id,
Type__c,
AccountId
FROM Contact
WHERE (Type__c = 'New' OR Type__c = 'Renew')
AND Accountid IN :accMap.keySet()];
for (Contact c : contactsToCheck) {
if (c.Type__c == 'New') {
accountsWithNew.add(c.AccountId);
} else if (c.Type__c == 'Renew') {
accountsWithRenew.add(c.AccountId);
}
}
for (Account acc_i : accMap.values()) {
/*Boolean hasNewCon = accountsWithNew.contains(acc_i.Id);
Boolean hasRenewCon = accountsWithRenew.contains(acc_i.Id);
if(acc_i.IsActive__c==true){
if (hasNewCon && hasRenewCon) {*/
if(accountsWithNew.contains(acc_i.Id) && accountsWithRenew.contains(acc_i.Id)){
status=true;
}
if(acc_i.IsActive__c==true){
if(status==true){
System.debug('You can update');
acc_i.IsActive__c = true;
} else {
acc_i.addError('You should have at least 1 contact of type new and 1 of type renew');
acc_i.IsActive__c = false;
}
}
}
}