Ask Search:
Seva TsurikovSeva Tsurikov 

Best practice for trigger/apex for bulk inserts

Hi, 

I have a scenario when large batches of Orders (~1000 records per batch) are inserted in the database with multiple order line items.  
For each order I need to query a single Order Line Item and bring data to parent Order (each OLI has the same data, so I need to query only one).
Please advice the best practive for a such task. I can't came up with an architecture that avoid quering each sindle child record.

My current solution:

Trigger:

//triggers loops through all updated orders and select only those that need an update, then pass List of Orders to a Class
List<Order> orders = new List<Order>();
        for(Order o: Trigger.new){
        if  (o.Shipper__c == null && o.Count_of_Order_lines__c >= 1) {
            orders.add(o);
        }
OrderTriggerActions.updateOrderWithShipperData(orders);
Class:
public static void updateOrderWithShipperData(List<Order> orders) {
        for ( Order o : orders ) {
                 Order_Lines__c oli = [Select Id, Shipper__c, Shipping_Type__c, eWinery_ShippingTypeCode__c FROM Order_Lines__c WHERE Order__c = :o.Id LIMIT 1];
                o.Shipper__c = oli.Shipper__c;
        }
    Database.update(orders);
    }

Please advice how to improve pattern to ommit multiple SOQL requests
Best Answer chosen by Seva Tsurikov
Amit SinghAmit Singh
try below code
Trigger
//triggers loops through all updated orders and select only those that need an update, then pass List of Orders to a Class
Set<Id> orders = new Set<Id>();
        for(Order o: Trigger.new){
        if  (o.Shipper__c == null && o.Count_of_Order_lines__c >= 1) {
            orders.add(o.Id);
        }
OrderTriggerActions.updateOrderWithShipperData(orders);

Class
public static void updateOrderWithShipperData(Set<Id> orders) {
		Map<Id, Order_Lines__c> orderMap = new Map<Id, Order_Lines__c>();
		List<Orders> orderList = new List<Order>();
		orderMap = [Select Id,Shipper__c, 
					(Select Id, Shipper__c, Shipping_Type__c,Order__c , eWinery_ShippingTypeCode__c FROM Order_Liness__r LIMIT 1) 
					From  Order Where Id In : orders];
        for(Order_Lines__c o : orderMap.keySet()) {
			Order or = new Order();
			or.id = o.Order__c;
			or.Shipper__c = o.Shipper__c;
			orderList.add(or);
        }
    Database.update(orderList);
    }

If this not works then I would suggest you post this question to developer board for the quick response. This community is focused on configuration and design questions. Programming questions are best to post in one of the developer forums for a quick response here
https://developer.salesforce.com/forums/#!/feedtype=RECENT&criteria=ALLQUESTIONS
http://salesforce.stackexchange.com/


Regards,
Amit Singh
GIRIKON LLC
https://sfdcpanther.wordpress.com/

All Answers

Chris DuncombeChris Duncombe
This forum is geared towards config and point/click solutions.  I would take this coding question to the developer forums as they are more focused on coding questions such as this

https://salesforce.stackexchange.com/

https://developer.salesforce.com/forums/
Amit SinghAmit Singh
try below code
Trigger
//triggers loops through all updated orders and select only those that need an update, then pass List of Orders to a Class
Set<Id> orders = new Set<Id>();
        for(Order o: Trigger.new){
        if  (o.Shipper__c == null && o.Count_of_Order_lines__c >= 1) {
            orders.add(o.Id);
        }
OrderTriggerActions.updateOrderWithShipperData(orders);

Class
public static void updateOrderWithShipperData(Set<Id> orders) {
		Map<Id, Order_Lines__c> orderMap = new Map<Id, Order_Lines__c>();
		List<Orders> orderList = new List<Order>();
		orderMap = [Select Id,Shipper__c, 
					(Select Id, Shipper__c, Shipping_Type__c,Order__c , eWinery_ShippingTypeCode__c FROM Order_Liness__r LIMIT 1) 
					From  Order Where Id In : orders];
        for(Order_Lines__c o : orderMap.keySet()) {
			Order or = new Order();
			or.id = o.Order__c;
			or.Shipper__c = o.Shipper__c;
			orderList.add(or);
        }
    Database.update(orderList);
    }

If this not works then I would suggest you post this question to developer board for the quick response. This community is focused on configuration and design questions. Programming questions are best to post in one of the developer forums for a quick response here
https://developer.salesforce.com/forums/#!/feedtype=RECENT&criteria=ALLQUESTIONS
http://salesforce.stackexchange.com/


Regards,
Amit Singh
GIRIKON LLC
https://sfdcpanther.wordpress.com/
This was selected as the best answer