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
Masechaba Maseli 8Masechaba Maseli 8 

trigger partially updating record

Hello Guys

I have a trigger below that is partially updating the record, may anyone assist me in figuring out what is causing the second update to Fail. 

It updates Ship_From_Zone1__c  and Hub_Ship_From_Zone__c  but not the shipto and the hubshipto Ids
trigger PopulateRegion on Shipment_Order__c (before insert, before update) {


for(Shipment_Order__c shipmentorder : Trigger.new) {

List<Ship_To__c> shipto= new List<Ship_To__c>();  
shipto = [Select Id from Ship_To__c WHERE Ship_To__c.Ship_From_Zone__c = :shipmentorder.Ship_From_Zone1__c  AND Ship_To__c.Name =:shipmentorder.Ship_From_Country__c ORDER BY Id LIMIT 1];
List<Ship_To__c> hubshipto = new List<Ship_To__c>(); 
hubshipto = [Select Id from Ship_To__c WHERE Ship_To__c.Ship_From_Zone__c = :shipmentorder.Hub_Ship_From_Zone__c AND Ship_To__c.Name =:shipmentorder.Hub_Country__c ORDER BY Id LIMIT 1];
    
If(shipmentorder.Hub_Shipment_Formula__c == False && shipmentorder.Ship_From_Country__c != null && shipto.size() > 0 ){


        string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
        shipmentorder.Ship_From_Zone1__c = region; 
        shipmentorder.Ship_to__c = shipto[0].Id;
}
    
  
    
    If(shipmentorder.Hub_Shipment_Formula__c == True && shipmentorder.Hub_Country__c != null && shipto.size() > 0)  { 
    
     
    string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
    string hubregion = Country_Region_List__c.getInstance(shipmentorder.Hub_Country__c).Region__c;
   
      shipmentorder.Ship_From_Zone1__c = region; 
      shipmentorder.Hub_Ship_From_Zone__c = hubregion; 
      shipmentorder.Ship_to__c = hubshipto[0].Id ;
      shipmentorder.From_Hub_Destination__c = shipto[0].Id ;

}

}

}


 
Best Answer chosen by Masechaba Maseli 8
CarlosLimaCarlosLima
Please change the line 29 to List<Ship_To__c> hubshipto = Database.query(queryString + whereConditionHub);

All Answers

Wilfredo Morillo 20Wilfredo Morillo 20
Debug the values that are evaluated in the IF:

system.debug('shipmentorder.Hub_Shipment_Formula__c= 'shipmentorder.Hub_Shipment_Formula__c +' shipmentorder.Hub_Country__c = '+
shipmentorder.Hub_Country__c+ 'shipto.size() = '+shipto.size() );

Make sure all conditions evaluate to true.
CarlosLimaCarlosLima
I'd rather you remove the SELECT's inside of FOR primarily. You can enter in a infinite loop and your trigger will fail.
The values of the the lists are only being used as temporary values to assign to the Object in the trigger, and there you're not performing and DML statement to update the related Id's.

make sure that you're editing the values retreived by SOQL and put a Database.update(SObject) to be done.

However once you do this you must avoid the recursivity because you'll enter again at the same trigger.

I didn't tested this code, but this is an idea how you can accomplish this:

 
​trigger PopulateRegion on Shipment_Order__c (before insert, before update) {
    
    
    Map<String, String> mShipto = new Map<String, String>();
    Map<String, String> mhubShipTO = new Map<String, String>();

    String queryString = 'Select Id from Ship_To__c WHERE';
    String whereConditionShip;
    String whereConditionHub;

    for(Ship_To__c vShipTO : Trigger.new) {
        mShipto.put(vShipTO.Ship_From_Zone1__c, Ship_From_Country__c);
        mhubShipTO.put(vShipTO.Hub_Ship_From_Zone__c,vShipTO.Hub_Country__c);

    }

    for(String zone1 : mShipto.keySet()) {
       whereCondition += ' ' + Ship_From_Zone1__c  = \'' + zone1 + '\' AND Ship_To__c = \'' + mShipto.get(zone1) + '\';

    }

  
    for(String mHub : mShipto.keySet()) {
       whereConditionHub += ' ' + Ship_From_Zone__c  = \'' + mHub + '\' AND Name = \'' + mhubShipTO.get(mHub) + '\';

    }
    
    List<Ship_To__c> shipto = Database.query(queryString + whereCondition);
    List<Ship_To__c> hubshipto = Database.query(queryString + whereCondition);

    
    for (Shipment_Order__c shipmentorder : Trigger.new) {
      
        If(shipmentorder.Hub_Shipment_Formula__c == False && shipmentorder.Ship_From_Country__c != null && shipto.size() > 0 ){

            string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
            shipmentorder.Ship_From_Zone1__c = region; 
            shipmentorder.Ship_to__c = shipto[0].Id;
        }
        
        
        if (shipmentorder.Hub_Shipment_Formula__c == True && shipmentorder.Hub_Country__c != null && shipto.size() > 0)  { 
            
            string region = Country_Region_List__c.getInstance(shipmentorder.Ship_From_Country__c).Region__c;
            string hubregion = Country_Region_List__c.getInstance(shipmentorder.Hub_Country__c).Region__c;
            
            shipmentorder.Ship_From_Zone1__c = region; 
            shipmentorder.Hub_Ship_From_Zone__c = hubregion; 
            shipmentorder.Ship_to__c = hubshipto[0].Id ;
            shipmentorder.From_Hub_Destination__c = shipto[0].Id ;
            
        }
        
    }
    
}

 
CarlosLimaCarlosLima
Please change the line 29 to List<Ship_To__c> hubshipto = Database.query(queryString + whereConditionHub);
This was selected as the best answer
Masechaba Maseli 8Masechaba Maseli 8
@carlosHLima

Thank you for the advice, I am trying it now and will let you know.