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
Dhananjay123456789Dhananjay123456789 

Apex trigger bulkification problem

custom objects
Associate__c
Product__c
Order__c
       Associate__c(Lookup of Associate)
Order_Entry__c
      Order__c (Lookup of Order__c)
      Product__c (Lookup of Product__c)
      Quantity__c (Number 5,0)
Inventory__c
      Status__c (Picklist => 'Assigned', 'Ordered', 'Sold')
      Associate__c (Lookup of Associate__c)
      Product__c (Lookup of Product__c)
trigger: 
Update Inventory to sold when Product or Quantity is changed on Order Entry.
Get all Inventories which is related to Product and Status__c = 'Assigned' and Associate__c = Order.Associate__c and LIMIT = Quantity and update status to 'Sold' Order By CreatedDate LIMIT Quantity
trigger TriUpdateInventorySatus on Order_Entry__c (after update) {
    
    list<Id> ProductIdList = New List <Id>();
    list<Id> AssociateIdList = New List <Id>();
    Integer Quantity;
    List<Id> OrderIdList  = new List<Id>();
    List<Inventory__c> InventoryUpdation = new List<Inventory__c>();
    for(Order_Entry__c OdEnRecord: trigger.new){
        OrderIdList.add(OdEnRecord.Order__c);
        ProductIdList.add(OdEnRecord.Product__c);
        Quantity = Integer.valueOf(OdEnRecord.Quantity__c);
    }
    List<Order__c> OrderRecordList = [select id,name from Order__c where id IN: OrderIdList ];
    For(Order__c OrderRecord: OrderRecordList){
        AssociateIdList.add(OrderRecord.Associate__c);
    }
    List<Inventory__c> InventorySoqlList = [ select Id,Status__c from Inventory__c where 
                                            Product__c In: ProductIdList AND Status__c ='Assigned' 
                                            And  Associate__c IN: AssociateIdList Order By CreatedDate LIMIT: Quantity ];
    for(Inventory__c InventoryRecord: InventorySoqlList){
        InventoryRecord.Status__c ='Sold';
        InventoryUpdation.add(InventoryRecord);
    }
    update InventoryUpdation;
}

 
SwethaSwetha (Salesforce Developers) 
HI Dhananjay,
Where are you stuck? Are you seeing any errors? Thanks
Dhananjay123456789Dhananjay123456789
Hi Swetha,
Thanks for replying
above code can't handle when bulk record going to update because the Quantity__c on the object Order_Entry__c is going to Inventory__c soql
List<Inventory__c> InventorySoqlList = [ select Id,Status__c from Inventory__c where 
                                            Product__c In: ProductIdList AND Status__c ='Assigned' 
                                            And  Associate__c IN: AssociateIdList Order By CreatedDate 
                                         LIMIT: Quantity ];

As multiple record updated on Order_Entry__c object Quantity__c may have diffrent values thus for every record There should be new query...
 
Dhananjay123456789Dhananjay123456789
trigger TriUpdateInventorySatus on Order_Entry__c (after update) {
    
    Set<Id> AssociateIdSet = New Set <Id>();
    Integer Quantity;
    Map < String, String > orderProductMap = new Map < String, String > ();
    List<Id> OrderIdList  = new List<Id>();
    List<Inventory__c> InventoryUpdation = new List<Inventory__c>();
    for(Order_Entry__c OdEnRecord: trigger.new){
        if (OdEnRecord.Product__c!= trigger.oldmap.get(OdEnRecord.Id).Product__c ||
      OdEnRecord.Order__c != trigger.oldmap.get(OdEnRecord.Id).Order__c)
        orderProductMap.put(OdEnRecord.Order__c, OdEnRecord.Product__c);
        Quantity = Integer.valueOf(OdEnRecord.Quantity__c);
    }
    List<Order__c> OrderRecordList = [select id,name from Order__c where id IN: orderProductMap.keyset() ];
    For(Order__c OrderRecord: OrderRecordList){
        AssociateIdSet.add(OrderRecord.Associate__c);
    }
    List<Inventory__c> InventorySoqlList = [ select Id,Status__c from Inventory__c where 
                                            Product__c In: orderProductMap.values() AND Status__c ='Assigned' 
                                            And  Associate__c IN: AssociateIdset Order By CreatedDate LIMIT: Quantity ];
    for(Inventory__c InventoryRecord: InventorySoqlList){
        InventoryRecord.Status__c ='Sold';
        InventoryUpdation.add(InventoryRecord);
    }
    update InventoryUpdation;
}

Please Suggest Somthing