-4

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;
            }
        }
    }
}  

closed as unclear what you're asking by David Cheng, glls, Derek F, Reshma, Vijay Ganji Oct 4 at 11:17

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Not really clear what you're looking to do because the code you posted isn't a trigger. In short, in your trigger you can see the old versus new value of isActive to compare when it changes. – Kris Goncalves Oct 3 at 11:31
  • this is a handler class for my trigger – user74548 Oct 3 at 11:57
2

You passed the maps into your handler code so you really just need to compare them by using the acc ID as the key between the two maps

 for (Account acc_i : accMap.values()) {
        if(acc_i.IsActive__c==true && acc_i.IsActive__c != accoldMap.get(acc_i.Id).IsActive__c){
                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;
            }
        }
    }
  • I tried it but it is not working for bulk – user74548 Oct 3 at 13:49
  • 2
    You'll need to provide more info on any errors you get or what doesn't work. Doesn't mean you can just copy and paste whatever I provide and hope it works if you don't try to learn/understand it – Kris Goncalves Oct 3 at 14:47
  • Towards the bottom of How to Answer, there's a section that says "Not all questions can or should be answered" that I, myself, take to heart. Sure, this is a pretty easy question to answer, but the signs point to someone who wants people to do their work for them. Still, +1 from me. – Derek F Oct 3 at 17:45
1

If you want your trigger to fire only when IsActive__c is changed to true, then you will have to check for two conditions.

  1. Old value of IsActive__c is not same as new value of IsActive__c
  2. New value of IsActive__c is true

So that is how your condition should be, so replace below code

if(acc_i.IsActive__c==true)

with

if(acc.IsActive__c != accoldMap.get(acc.Id).IsActive__c && acc_i.IsActive__c==true)
  • I tried but it is not working for bulk can u please give me some other solution – user74548 Oct 3 at 13:50
  • it is only executing first record successfully then it is showing error – user74548 Oct 3 at 14:14
  • What error is it showing ? Please edit your post and mention the error so that community can help you. – Vijay Ganji Oct 3 at 14:38
0

Try 'after update':

trigger AccountTrigger on Account(  after update){  

    if( Trigger.isAfter && Trigger.isUpdate){ 
        AccountTriggerHelper.handleAfterUpdate(Trigger.newMap, Trigger.oldMap);
    }    
}

public with sharing class AccountTriggerHelper{

    public static void handleAfterUpdate(Map <Id, Account> newMap, Map<Id,Account> oldMap){
        Map <Id, Account> accsToUpdateById = new Map <Id, Account>();
        for (Account newAcc: newMap.values()){
            if( newAcc.IsActive__c != oldMap.get(newAcc.Id).IsActive__c ){
                accsToUpdateById.put( newAcc.Id, newAcc ); 
                // your code: do something with accsToUpdateById  or each element
            }
        }
    }
  • Welcome to SFSE! Please take a look at How to Answer. The best answers tend to explain how to solve the problem, as well as why the solution should work. The idea is that this helps people understand the underlying problem, and how to recognize and solve similar issues in the future. – Derek F Oct 3 at 18:21
  • The key part of the solution here is using oldMap to do a comparison. I don't think moving to an after trigger really makes any positive difference here. It would mean you'd need to perform DML and worry about recursion if you want to update a field on a record in trigger.new/trigger.newMap. – Derek F Oct 3 at 18:24
  • Thanks for your suggestions, I have solved the issue – user74548 Oct 4 at 6:44
-1

Welcome to SF SE

You can do something like:

trigger Compare_OldandNewvalues on Account (before update) {

for (Account acc: Trigger.new) {
    Account oldAccount = Trigger.oldMap.get(acc.ID);
    if(acc.IsActive__c != oldAccount.IsActive__c) {
       System.debug('--Is Active is changed*--');
       //code to execute when is IsActive is changed
    }
    else{
        System.debug('Is Active has not been Updated**--');
        //code to execute when is IsActive is NOT changed
    }
  }
}
  • I tried to do this but it is not working – user74548 Oct 3 at 11:53
  • actually i want to add this logic to my handler so how can i do tht – user74548 Oct 3 at 11:58
  • I think this answer would be improved if you went over what the difference is between the original code is and what your answer is suggesting. Basically, explain the specific thing that you're using to solve the problem, and explain how it solves the problem. – Derek F Oct 3 at 18:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.