+ Start a Discussion
debradebra 

Methods to de-activate an activated Order using automation/code


We have a requirement when creating Orders on a Contract that only one Order is Activated at a time.  Looking to write some code that runs when a user activates an Order any other Order that is activated would be updated to a "De-activated" state/status.   I have tried  prototype this using Process Builder/Flows but it appears that this is not possible even when running the process as System Admin user.  I would prefer not to use Apex although it woudl be an option for us if required.  The only way to de-activate is to use the stamdard button on the Order.
 
Best Answer chosen by debra
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Ok in that case, j​ust replace AccountId with ContractId
trigger beforeUpdateOfOrders on Order (after update) {
    Set<Id> setContracts = new Set<Id>();
    List<Order> lstOrder = new List<Order>();
    for(Order o: trigger.new){
        if(o.Status=='Activated' && o.Status!=trigger.oldMap.get(o.Id).Status)
            setContracts.add(o.ContractId);
    }
    for(Order o: [Select Id, ContractId, Status from Order where ContractId IN:setContracts AND Id NOT IN:trigger.new]){
        o.Status = 'Draft';  
        lstOrder.add(o); 
    }
    if(lstOrder.size()>0)
        update lstOrder;
}

All Answers

SalesFORCE_enFORCErSalesFORCE_enFORCEr
You have to write a trigger for this. 
trigger beforeUpdateOfOrders on Order (after update) {
    Set<Id> setAccounts = new Set<Id>();
    List<Order> lstOrder = new List<Order>();
    for(Order o: trigger.new){
        if(o.Status=='Activated' && o.Status!=trigger.oldMap.get(o.Id).Status)
            setAccounts.add(o.AccountId);
    }
    for(Order o: [Select Id, AccountId, Status from Order where AccountId IN:setAccounts AND Id NOT IN:trigger.new]){
        o.Status = 'Draft';  
        lstOrder.add(o); 
    }
    if(lstOrder.size()>0)
        update lstOrder;
}

Please mark it as Best Answer if it helps you.
debradebra
Thanks for the suggestion I'll give it a try.   Pretty sure I won't need to capture the AccountIDs for the activated orders but instead the ContractIDs since I want at most one activated order per Contract.  I'll post back later with my results.   Does bring up a question that maybe is my newness using Flows/PB and that is why a trigger would allow this when a Flow update cannot do it :-)
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Ok in that case, j​ust replace AccountId with ContractId
trigger beforeUpdateOfOrders on Order (after update) {
    Set<Id> setContracts = new Set<Id>();
    List<Order> lstOrder = new List<Order>();
    for(Order o: trigger.new){
        if(o.Status=='Activated' && o.Status!=trigger.oldMap.get(o.Id).Status)
            setContracts.add(o.ContractId);
    }
    for(Order o: [Select Id, ContractId, Status from Order where ContractId IN:setContracts AND Id NOT IN:trigger.new]){
        o.Status = 'Draft';  
        lstOrder.add(o); 
    }
    if(lstOrder.size()>0)
        update lstOrder;
}
This was selected as the best answer