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
TJMeadowsTJMeadows 

Error Updating Order in Update Trigger

Hello all,

I'm running into an error that just doesn't make sense to me. I have a trigger on OrderItem that evaluates related payments to an order to determine if it is paid off. The error claims that a change to the order currency is happening, but that is not the case. I've debugged the before and after object maps to verify that CurrencyIsoCode is unchanged.
 
Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OrderItemTrigger: execution of AfterInsert
caused by: System.DmlException: Update failed. First exception on row 0 with id 8013D0000009C8hQAE; first error: FIELD_INTEGRITY_EXCEPTION, You can't edit the order currency when the order has order products.: Contract Currency: [CurrencyIsoCode]

Here is the trigger handler:
private void evaluatePaidOffStatus(List<OrderItem> orderItems) {
	if (shouldRun('evaluatePaidOffStatus')) {
		methodRunMap.put('evaluatePaidOffStatus', false);
		
		Set<Id> orderIds = new Set<Id>();
		for (OrderItem orderItem : orderItems) {
			orderIds.add(orderItem.OrderId);
		}
			
		OrderTransferService.evaluateOrderPayments(orderIds);
	}
}



and the erroring method:
public static void evaluateOrderPayments(Set<Id> orderIds) {
	Order[] orders = [select Id, Grand_Total__c, Paid_Off__c, (select Id, pymt__Amount__c from Payments__r) from Order where Id in :orderIds];// and Type != 'EDI'];
	for (Order order : orders) {
		Decimal amountPaid = 0.00;
		for (pymt__PaymentX__c payment : order.Payments__r) {
			amountPaid += payment.pymt__Amount__c;
		}

		order.Paid_Off__c = amountPaid >= order.Grand_Total__c;
	}
		
	update orders;
}

Thanks for any insight you all can give
 
Wilfredo Morillo 20Wilfredo Morillo 20
Why don't you use roll up summary for this porpuse.
Ref:
https://help.salesforce.com/articleView?id=fields_about_roll_up_summary_fields.htm&type=0
https://trailhead.salesforce.com/en/modules/point_click_business_logic/units/roll_up_summary_fields
TJMeadowsTJMeadows
I need to make call outs based on an order being paid off or not. I can use a roll-up field to simplify the sum of payments, but a trigger still has to be written to make any callouts.

I'll look in to what that implementation would look like, but that still doesn't provide insight as to why this error is being thrown.
PC2012PC2012
@TJMeadows: Did you find what was causing the error? I am getting the same error in my code .