+ Start a Discussion
Hermann OuréHermann Ouré 

Trigger To uncheck a checkbox

Hello,

I am trying to create a trigger to uncheck a checkbox field on Account called "Active Order".
Basically when an Order has benn deleted from an account and there is no Order remaining for the Account, the trigger should uncheck Active Order. 

No Order:
User-added image
Uncheck Active Order on Account
User-added image

Can't get my trigger working properly
 
trigger AccountActiveOrderTrigger on Account (after delete) {
    
    List<Order> lstOrder = new List<Order>();
    
    //Get Accounts with orders 
    Map<Id, Account> accWithOrders = new Map<Id, Account>([SELECT Id, Name, Active_Order__c, (SELECT Id FROM Orders)
                                                          FROM Account WHERE Id IN: Trigger.old]);
    
    //Iterate through each Accounts
    for(account a : Trigger.old) {
        System.debug('Accounts with orders' +accWithOrders.get(a.Id).Orders.size());
        
        //Check if an Account has an order
        if(accWithOrders.get(a.Id).Orders.size() == 0) {
            a.Active_Order__c = False;
           	
        } 
        
     update a;
    }
 
}

Thank you
Best Answer chosen by Hermann Ouré
Hermann OuréHermann Ouré
Hello,
Actually decided to call an apex class from a trigger which gives that:

Apex class
 
public class AccountManagerV2 {
    
    public static void updateActiveOrder(List<Order> newList, Map<Id,Order> oldMap) {
        
        Set<Id> accIds = new Set<Id>();
        
        for(order o: newList) {
            if(Trigger.isDelete) {
                if(o.AccountId != null) accIds.add(o.AccountId);
            } 
        }
        
        //Get Accounts with orders
        List<Account> lstAccount = new List<Account> ([SELECT Id, Name, Active_Order__c, (SELECT Id FROM orders)
                                                       FROM Account WHERE Id In: accIds]);
        
        if(lstAccount.size() > 0) {
            for(Account acc: lstAccount) {
                List<Order> lstOrder = new List<Order>();
                lstOrder = acc.Orders;
                if(lstOrder.size() == 0) {
                   acc.Active_Order__c = False;
                }
                
            }
            
            update lstAccount;
        }
       
    }

}

Apex trigger:
 
trigger AccountActiveOrderTrigger on Order (after delete) {
    
    if(Trigger.isDelete) {
        AccountManagerV2.updateActiveOrder(Trigger.old, null);
    }
}

That is working accordingly and unchecking the box when all orders have been delated from an Account

 

All Answers

mukesh guptamukesh gupta
Hi Hermman,

You need to write a trigger on Order instead of Account. because you are deleting order not Account.



If this solution is usefull for you, Please mark as a Best Answer to help others.


Regards
Mukesh

 
Hermann OuréHermann Ouré
Hello,
Actually decided to call an apex class from a trigger which gives that:

Apex class
 
public class AccountManagerV2 {
    
    public static void updateActiveOrder(List<Order> newList, Map<Id,Order> oldMap) {
        
        Set<Id> accIds = new Set<Id>();
        
        for(order o: newList) {
            if(Trigger.isDelete) {
                if(o.AccountId != null) accIds.add(o.AccountId);
            } 
        }
        
        //Get Accounts with orders
        List<Account> lstAccount = new List<Account> ([SELECT Id, Name, Active_Order__c, (SELECT Id FROM orders)
                                                       FROM Account WHERE Id In: accIds]);
        
        if(lstAccount.size() > 0) {
            for(Account acc: lstAccount) {
                List<Order> lstOrder = new List<Order>();
                lstOrder = acc.Orders;
                if(lstOrder.size() == 0) {
                   acc.Active_Order__c = False;
                }
                
            }
            
            update lstAccount;
        }
       
    }

}

Apex trigger:
 
trigger AccountActiveOrderTrigger on Order (after delete) {
    
    if(Trigger.isDelete) {
        AccountManagerV2.updateActiveOrder(Trigger.old, null);
    }
}

That is working accordingly and unchecking the box when all orders have been delated from an Account

 
This was selected as the best answer