You need to sign in to do that
Don't have an account?
Masechaba 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
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 ; } } }
All Answers
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.
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:
Thank you for the advice, I am trying it now and will let you know.