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
Ramesh RageRamesh Rage 

Issue with Trigger while inserting Multiple Records from VF page

Hi All,

I have written Bulk Trigger to update date field based on the anothet date field on the same object. But my trigger is working for only first record only while inserting multiple records and for the rest of the records it is not updating.

Below is the code which i have used to update, not sure what wrong with my code and why it is not working for multiple records(bulk records). Please correct me if my code is wrong or if i have missed anything ???

Please reply if you find any sol.

Thanks a lot.
Ram

CODE:
trigger Update_PlannedShipmentDate on ccrz__E_OrderShipment__c (After insert, After Update) {

set<String> ccshipmentID= new set<String>();
List<ccrz__E_OrderShipment__c> shipmentList = New List<ccrz__E_OrderShipment__c>();

 
If(Recursion.isRecursion)
{
  system.debug('^^^^^^^ isRecursion:'+Recursion.isRecursion);
 
  For(ccrz__E_OrderShipment__c ccshipments : Trigger.New){
      ccshipmentID.add(ccshipments.id);
      system.debug('^^^^^^^ New SHipments:'+ccshipmentID);
  }
 
  system.debug('^^^^^^^ New SHipments:'+ccshipmentID);
  List<ccrz__E_OrderShipment__c> updateShipmentList = New List<ccrz__E_OrderShipment__c>();
  If(ccshipmentID.size() > 0 )
  {
     shipmentList = [select CCArq_Delivery_Time__c,Arq_Revised_Delivery_Date__c,Arq_Planned_Shipment_Date__c
                     FROM ccrz__E_OrderShipment__c
                     WHERE ID IN: ccshipmentID AND ccrz__Order__r.ccrz__Storefront__c ='ArqivaESales'];
     system.debug('^^^^^^^ Shipments list:'+shipmentList);
    
    
     For(ccrz__E_OrderShipment__c ccshipment : shipmentList )
     {
        if(ccshipment.CCArq_Delivery_Time__c != null && ccshipment.Arq_Revised_Delivery_Date__c == null)
        {
           ccshipment.Arq_Planned_Shipment_Date__c = date.newinstance(ccshipment.CCArq_Delivery_Time__c.year(), ccshipment.CCArq_Delivery_Time__c.month(), ccshipment.CCArq_Delivery_Time__c.day())- 5;
           updateShipmentList.add(ccshipment);
           system.debug('^^^^^^^ shipment:'+ccshipment);
        }
       else if(ccshipment.Arq_Revised_Delivery_Date__c != null)
        {
           ccshipment.Arq_Planned_Shipment_Date__c = ccshipment.Arq_Revised_Delivery_Date__c - 5;
           updateShipmentList.add(ccshipment);
           system.debug('^^^^^^^ update list:'+ccshipment);
        }
     }
    
      Recursion.isRecursion = false;
      system.debug('^^^^^^^ isRecursion:'+Recursion.isRecursion);
  }
  if(updateShipmentList.size() > 0){
          system.debug('^^^^^^^ update list:'+updateShipmentList);
          Update updateShipmentList;
         
   }
}

}
ShashForceShashForce
Instead of updating ccshipment, please try to instantiate a new shipment object, something like this:

For(ccrz__E_OrderShipment__c ccshipment : shipmentList )
     {
        if(ccshipment.CCArq_Delivery_Time__c != null && ccshipment.Arq_Revised_Delivery_Date__c == null)
        {
           ccrz__E_OrderShipment__c ccship = new ccrz__E_OrderShipment__c();
           ccship.Id = ccshipment.Id;
           ccship.Arq_Planned_Shipment_Date__c = date.newinstance(ccshipment.CCArq_Delivery_Time__c.year(), ccshipment.CCArq_Delivery_Time__c.month(), ccshipment.CCArq_Delivery_Time__c.day())- 5;
           updateShipmentList.add(ccship);
           system.debug('^^^^^^^ shipment:'+ccshipment);
        }
       else if(ccshipment.Arq_Revised_Delivery_Date__c != null)
        {
           ccrz__E_OrderShipment__c ccship = new ccrz__E_OrderShipment__c();
           ccship.Id = ccshipment.Id;
           ccship.Arq_Planned_Shipment_Date__c = ccshipment.Arq_Revised_Delivery_Date__c - 5;
           updateShipmentList.add(ccship);
           system.debug('^^^^^^^ update list:'+ccshipment);
        }
     }
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Ramesh,

Write Before trigger,like before insert, before update
1. Collect the ccrz__Order__c value in set collection variable  & do query on correspondence object with set ids.
 2. Do Comparison for all record in before Trigger and assign the value.
 
Note: dont need to write insert or update DML, trigger will automatically take care,

Please let me know if you find any issue.

Thanks & Regards,
Ranjeet Singh.
Ramesh RageRamesh Rage
Hi Ranjeet Singh,

Thanks fro the reply.

I just tried your solution and below is the code. But still it didn't work. Shipmnet record itself is not inserting now.
Please check the code and let me know where am doing wrong.

Am getting an error at line "DateTime dt = mapShipments.get(ccship.id).CCArq_Delivery_Time__c;" saying that referencing null value.
is there anything wrong in my code.

CODE:
trigger UpdatePlannedShipmentDate on ccrz__E_OrderShipment__c (before insert, before update) {
 
  set<String> shipmentIds = new set<String>();
 
  For(ccrz__E_OrderShipment__c ccshipment : Trigger.New ){
     shipmentIds.add(ccshipment.id); // add shipmnets to set
  }
 
  Map<Id,ccrz__E_OrderShipment__c> mapShipments = New Map<Id,ccrz__E_OrderShipment__c>(
    [select CCArq_Delivery_Time__c,Arq_Revised_Delivery_Date__c,Arq_Planned_Shipment_Date__c
     FROM ccrz__E_OrderShipment__c
     WHERE ID IN: shipmentIds
     AND ccrz__Order__r.ccrz__Storefront__c ='ArqivaESales']
    );
   
   For(ccrz__E_OrderShipment__c ccship : Trigger.New){
      if(ccship.CCArq_Delivery_Time__c != null && ccship.Arq_Revised_Delivery_Date__c == null)
      {
         system.debug('^^^^^^^ shipment date:'+ mapShipments.get(ccship.id).CCArq_Delivery_Time__c);
         DateTime dt = mapShipments.get(ccship.id).CCArq_Delivery_Time__c;
         If(dt != null)
           ccship.Arq_Planned_Shipment_Date__c = date.newinstance(dt.year(), dt.month(), dt.day())- 5;
      }
      else if(ccship.Arq_Revised_Delivery_Date__c != null)
      {
         ccship.Arq_Planned_Shipment_Date__c = mapShipments.get(ccship.id).Arq_Revised_Delivery_Date__c - 5;
      }
   }
}
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Ramesh,

followed by your code, I have written the code. Please try:
1. You have to check the API Name, based on your ORG.
2. Correct the Syntex error, if you find.

trigger UpdatePlannedShipmentDate on ccrz__E_OrderShipment__c (before insert, before update) {
  
   set<id> tempccids = new set<id>();
   for(ccrz__E_OrderShipment__c ccE:Trigger.New){
      tempccids.add(ccE.ccrz__Order__c);
   }
   //collect the ids of ccrz__Order__c record whose ccrz__Storefront__c ='ArqivaESales'
   set<id> ccrzORdIds = new set<Id>();
   for(ccrz__Order__c cc:[SELECT id,ccrz__Storefront__c FROM ccrz__Order__c WHERE id=:tempccids AND ccrz__Storefront__c ='ArqivaESales']){
       ccrzORdIds.add(cc.id);
   }
  
   for(ccrz__E_OrderShipment__c ccship : Trigger.New){
      if(ccrzORdIds.contains(ccship.ccrz__Order__c)){
      if(ccship.CCArq_Delivery_Time__c != null && ccship.Arq_Revised_Delivery_Date__c == null){
    //system.debug('^^^^^^^ shipment date:'+ mapShipments.get(ccship.id).CCArq_Delivery_Time__c);
    //DateTime dt = mapShipments.get(ccship.id).CCArq_Delivery_Time__c;
    DateTime dt = ccship.CCArq_Delivery_Time__c;
    If(dt != null)
       ccship.Arq_Planned_Shipment_Date__c = date.newinstance(dt.year(), dt.month(), dt.day())- 5;
         }else if(ccship.Arq_Revised_Delivery_Date__c != null) {
                ccship.Arq_Planned_Shipment_Date__c = ccship.Arq_Revised_Delivery_Date__c - 5;
         }
  
   }
   }
}

Please try this code, I hope it should fit for your business.
let me know if you find any issue.


Thanks & Regards,
Ranjeet Singh.