Exception:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: TotalPrice (total price must be specified): [TotalPrice]
Sample Code to reproduce the issue:
insert new OpportunityLineItem(
OpportunityId = '0063t000013wBfmAAE',
Product2Id = '01t3t000006Ory8AAC',
Quantity = 1,
Discount = .5
);
Exception:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: UnitPrice (only one of unit price or total price may be specified): [UnitPrice]
Sample Code to reproduce the issue:
insert new OpportunityLineItem(
OpportunityId = '0063t000013wBfmAAE',
Product2Id = '01t3t000006Ory8AAC',
Quantity = 1,
Discount = .5,
UnitPrice = 1000,
TotalPrice = 1000
);
To avoid this issue, kindly make sure the below for OpportunityLineItem(Opportunity Product) records:
1. We cannot set both TotalPrice and UnitPrice to null in the same Insert, update and Upsert call.
2. UnitPrice field or TotalPrice is required. You cannot specify both.
3. If you specify Discount and Quantity, UnitPrice field or TotalPrice is required.
4. TotalPrice field cannot be null if UnitPrice field is null.