You need to sign in to do that
Don't have an account?
Joy___
Apex Trigger - Update Field Values from Order object ?
Hi !
Can anyone tell me how to write trigger for this scenario, please ?
On Order object, I have 2 custom fields :
- Net_amount__c : is total amount without shipment cost
- Shipment_cost__c
I would like to update Net_amount__c field.
My trigger is bellow, but there are many problems :
1) When an order is created :
Total amount : 0
Net_amount__c :
Shipment_cost__c : 100
2) When an order is updated :
Total amount : 100
Net_amount__c : -50
Shipment_cost__c : 50
trigger CalculMontantTrigger on Order (before update) {
for (Order o: Trigger.new) {
o.Net_amount__c = o.TotalAmount - o.Shipment_cost__c;
}
}
Can anyone tell me how to write trigger for this scenario, please ?
On Order object, I have 2 custom fields :
- Net_amount__c : is total amount without shipment cost
- Shipment_cost__c
I would like to update Net_amount__c field.
My trigger is bellow, but there are many problems :
1) When an order is created :
Total amount : 0
Net_amount__c :
Shipment_cost__c : 100
2) When an order is updated :
Total amount : 100
Net_amount__c : -50
Shipment_cost__c : 50
trigger CalculMontantTrigger on Order (before update) {
for (Order o: Trigger.new) {
o.Net_amount__c = o.TotalAmount - o.Shipment_cost__c;
}
}
Order Amount (TotalAmount): 0,00
Shipment cost (Shipment_cost__c): 100,00
so TotalAmount - Shipment_cost__c => 0,00 - 100,00 = -100,00
Regards,
Omar
All Answers
I recommend you to use Record-Triggered Flow instead of coding.
Anyways try the code below:
Thanks,
Omar
Now, Net_Amount__c is a formula field (TotalAmount - Shipment_cost__c)
When an order is created or a record is delated Net_Amount__c is negative ...
Please check below :
regards,
Omar
TotalAmount - Shipment_cost__c
Regards,
Joy
Order Amount (TotalAmount): 0,00
Shipment cost (Shipment_cost__c): 100,00
so TotalAmount - Shipment_cost__c => 0,00 - 100,00 = -100,00
Regards,
Omar