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
Pete Watson 5Pete Watson 5 

Apex class filling field value with same value for each batch on bulk update

Hi All, 

Many thanks in advance for reading.

I have a class that works as expected on single record updates but when it fires on mass inserts or updates it assigns the same value accross each batch... 

the code block is finding the previous order record and assigning the dispatch date of the previous record to the current record (so we can then calculate time between orders, etc.)

I beleive the issue is in the LastOrder list and that its assigning the same value to all records in each batch but I'm unsure of the solution in the code block and would really appreicate some assistance? 

Many thanks 
 
public class OrderTriggerHandler {
    
    public static void OrderName(List<Order__c> orderlist){
    
    Id AccId;
    Date DisDate;
        
        for(Order__c a:orderlist) {
            AccId = a.Account__c;
            DisDate = a.Dispatch_Date__c;
            System.debug('AccId: ' + AccId);
            System.debug('DisDate: ' + DisDate);
        }
        
    	List<Order__c> LastOrder = [Select Id,Dispatch_Date__c,Account__c From Order__c 
                                Where Account__c =: AccId AND Dispatch_Date__c <: DisDate 
                                Order By Dispatch_Date__c DESC 
                                LIMIT 1];
        System.debug('LastOrder List Size: ' + LastOrder.size());
        
        For(Order__c ol : orderlist){
        For(Order__c o : LastOrder ){
            ol.Previous_Order_Date__c = o.Dispatch_Date__c;
                System.debug('Previous_Order_Date__c: ' + ol.Previous_Order_Date__c);
                System.debug('Dispatch_Date__c: ' + o.Dispatch_Date__c);
        
        }
     }

 
kamala swarnalathakamala swarnalatha
Hi Pete,

Yes, you are correct. The error is in the OrderList only.

You have to change the OrderList to identify the previous record by some specific condition.

For example: First, you have to create one record. Then try to insert or update 10 records at the same time. The code you have written only identifies the first record that which you created not identified the current record which you inserting or updating so you have applied some specific condition to know the previous record.

Hope this helps!

Please let me know if you have further questions.

Thanks,
K.Kamala
Sweet Potato Tec.

 
Pete Watson 5Pete Watson 5
Many thanks for your reply! (And appologies, I replied a couple of days ago via email but it didnt acctually post it seams, sorry)

I understand the concept but am unsure how to put that into action here sorry, is there any examples of how I may do this you could provide please? 

Thanks again

Pete
 
kamala swarnalathakamala swarnalatha
Hi Pete,

Will you please explain your requirement briefly I try to change the code like that?

Thanks
Kamala
Pete Watson 5Pete Watson 5
Hi Kamala, 

The requirement is to associate via a lookup field to the next most recent order/record on the custom object Order__c. 'Most recent' being defined by the Dispatch_Date__c field. 

As stated above the code detailed is working fine when triggered by a single record insert or update but when we bulk insert order__c records (which will be a overnight process) in batches (say 200 for this example) then the code is assigning the same 'previous record' to all 200 records in that batch opposed to running the code for each record within the batch. 

For the time being, as a workaround I have implemented a schedualed batch class to run after the nightly import and set the batch size to 1. This is acheiving the desired outcome but I feel there must be a way to do this on insert to make this additional process redundant. 

Many thanks once again
Pete
kamala swarnalathakamala swarnalatha
Hi Pete,

Would you please try this code?
public class OrderTriggerHandler {
    
    public static void OrderName(List<Order__c> orderlist){
    
    Id AccId;
    Date DisDate;
        
        for(Order__c a:orderlist) 
{
            AccId = a.Account__c;
            DisDate = a.Dispatch_Date__c;
            System.debug('AccId: ' + AccId);
            System.debug('DisDate: ' + DisDate);
            List<Order__c> LastOrder = [Select Id,Dispatch_Date__c,Account__c From Order__c Where Account__c =: AccId AND Dispatch_Date__c <: DisDate             Order By Dispatch_Date__c DESC LIMIT 1];
            System.debug('LastOrder List Size: ' + LastOrder.size());
            For(Order__c o : LastOrder )
{
            ol.Previous_Order_Date__c = o.Dispatch_Date__c;
                System.debug('Previous_Order_Date__c: ' + ol.Previous_Order_Date__c);
                System.debug('Dispatch_Date__c: ' + o.Dispatch_Date__c);
        
        }
     }
}
}
Thanks
Kamala
 
Pete Watson 5Pete Watson 5
Hi Kamala, 

Thanks very much but the list in the for loop is causing errors (to many queries). 

Thanks 
kamala swarnalathakamala swarnalatha
Hi,

Could you please try this
public class OrderTriggerHandler {
    
    public static void OrderName(List<Order__c> orderlist){
    
    Id AccId;
    Date DisDate;
        
        for(Order__c a:orderlist) 
{
            AccId = a.Account__c;
            DisDate = a.Dispatch_Date__c;
            System.debug('AccId: ' + AccId);
            System.debug('DisDate: ' + DisDate);
            List<Order__c> LastOrder = [Select Id,Dispatch_Date__c,Account__c From Order__c Where Account__c =: AccId AND Dispatch_Date__c <: DisDate             Order By Dispatch_Date__c DESC LIMIT 1];
            System.debug('LastOrder List Size: ' + LastOrder.size());
            For(Order__c o : LastOrder )
{
            a.Previous_Order_Date__c = o.Dispatch_Date__c;
                System.debug('Previous_Order_Date__c: ' + ol.Previous_Order_Date__c);
                System.debug('Dispatch_Date__c: ' + o.Dispatch_Date__c);
        
        }
     }
}
}

Thanks
​​​​​​​