0

I wrote a trigger which converts a Quote to Order on changing Status to a particular value. This Quote has QuoteLineItems, hence, they should converted to corresponding OrderItems. Now when the trigger is run, corresponding Order from Quote is successfully created, but on inserting OrderItems it is giving me an exception as below:

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (must have the same currency as the order): [PricebookEntryId]

Also the Pricebook to which Order is related has USD currency, while the PricebookEntries for respective OrderItems has AUD as its currency.

Posting relevant part of code for better understanding.

Map<Id, Opportunity> mapOpportunities = new Map<Id, Opportunity> ([SELECT Id, AccountId FROM Opportunity WHERE Id IN :oppoIds]);
List<Order> newOrdersInsert = new List<Order>();
for (Quote qu : Trigger.New) {
    if (qu.Status.equals('Signed - Approved') && !Trigger.oldMap.get(qu.Id).Status.equals(qu.Status)) {
        if (qu.OpportunityId != null) {
            Order orderNew = new Order();
            orderNew.Status = 'Draft';
            orderNew.AccountId = mapOpportunities.get(qu.OpportunityId).AccountId;
            orderNew.OpportunityId = qu.OpportunityId;
            orderNew.EffectiveDate = System.today();
            orderNew.QuoteId = qu.Id;
            orderNew.PriceBook2Id = qu.PriceBook2Id;
            newOrdersInsert.add(orderNew);
        }
    }
}
if (newOrdersInsert.size() > 0) {
    insert newOrdersInsert;

    Map<Id, Id> mapOrderQuoteId = new Map<Id, Id>();
    for (Order ord : newOrdersInsert) {
        mapOrderQuoteId.put(ord.QuoteId, ord.Id);
    }

    List<QuoteLineItem> lstQuoteLineItems = [SELECT Id, PricebookEntryId, Product2Id, QuoteId, Quantity, UnitPrice FROM QuoteLineItem WHERE QuoteId IN :Trigger.newMap.keyset()];

    for (QuoteLineItem qli : lstQuoteLineItems) {
        OrderItem ori = new OrderItem();
        ori.Product2Id = qli.Product2Id;
        ori.OrderId = mapOrderQuoteId.get(qli.QuoteId);
        ori.PricebookEntryId = qli.PricebookEntryId;
        ori.Quantity = qli.Quantity;
        ori.UnitPrice = qli.UnitPrice;
        newOrderItemsInsert.add(ori);
    }
    if (newOrderItemsInsert.size() > 0) {
        insert newOrderItemsInsert;
    }
}

It is throwing exception on line where I insert OrderItems. How do I combat this scenario?

  • 1
    N.B. the lines if (newOrdersInsert.size() > 0) { and similar one further down are superfluous; no DML statements are burned on empty lists – cropredy Nov 29 '19 at 18:00
2

I don't see you setting the currency of your new orders. Try adding

orderNew.CurrencyIsoCode = qu.CurrencyIsoCode;

after instantiation of a new Order object.

The complaint is simply, that the used PricebookEntry does not have the same CurrencyIsoCode as the Order. As the PricebookEntry is used on the Quote, it will have the same currency as the Quote. But as you don't explicitly set the currency of your Order, that will default to whatever your org is set to.

  • Setting Quote's currency to Order's currency worked properly. – Nilang Pandey Dec 2 '19 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.