function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gvgv 

Trigger Question

Hi,

 

I have a general logic question that I am trying to use in my trigger. Seems simple but am stuck

 

I have a product and its got orders as a related list. There can be multiple orders but only the recent one can be made as active. All fine. But the problem is if there are multiple order at the same time two orders are marked as active. I want only one order to be active. 

 

Here is my logic now.

 

I take the order in the trigger.new and make it as active. I go theorugh the previous orders for that product and make everything inactive. In this process only the latest one will be active

 

But if there are 2 orders at the same time for the product , there are 2 active ones.  How would I solve that issue

 

this is a production issue

 

Thanks in advance

 

 

Set<Id> productIds = new Set<Id> (); for (Product_orders__c prod:Trigger.New){ if (prod.product__c!=null){ prod.active__c = true; productIds.add(prod.product__c);} } //this loop is to make the current one always active List<Product_orders__c> updateOrders = new List<Product_orders__c>(); // this list is to hold the orders that are to be updated List<Product_orders__c> OrdersList = ([Select e.product__c, e.Name,active__c from Product_orders__c e where e.product__r.Id in :productIds //the orderslist is to get the list of all the other orders and make them inactive so the previous active one will be inactive now ]); for(Product_orders__c ord : OrderList){ if (ord.active__c == true){ ord.active__c = false; updateShipments.add(ord); } }//for loop if (updateOrders .size() > 0) update updateOrders ; }

 


 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

Make the Order active only if it is not in the Set.

 

 

 for (Product_orders__c prod:Trigger.New){
            if ( (prod.product__c!=null) && (productIds.contains(prod.product__c) == false) {
                prod.active__c = true;
                productIds.add(prod.product__c);}
        } //this loop is to make the current one always active
 

All Answers

Kiran  KurellaKiran Kurella

Make the Order active only if it is not in the Set.

 

 

 for (Product_orders__c prod:Trigger.New){
            if ( (prod.product__c!=null) && (productIds.contains(prod.product__c) == false) {
                prod.active__c = true;
                productIds.add(prod.product__c);}
        } //this loop is to make the current one always active
 

This was selected as the best answer
gvgv
Thanks CodeWizard. That seems to be working. Thanks again