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
AnantGargAnantGarg 

Getting error while creating reduction order programmatically

I am trying to create a reduction order for an already created order. Though reduction order is being created successfully, it is returning this error:

Refund Order Error: field integrity exception: OriginalOrderId (reduction orders may not reference other reduction orders for reduction)

I have correctly passed the required parameters as:

   
Order reductionOrder = new Order(
    IsReductionOrder = true,
    OriginalOrderId = originalOrder.Id,
    Pricebook2Id = originalOrder.Pricebook2Id,
    EffectiveDate = originalOrder.EffectiveDate,
    Status = 'Draft',
    AccountId = originalOrder.AccountId);

    insert reductionOrder;

Why do I get this error? How can I fix it?

The original order is not a reduction order though
User-added image
Best Answer chosen by AnantGarg
AnantGargAnantGarg
ok, I get this. there were some more reduction orders which were being created in my code in a loop and the first reduction order is replacing the 'orginalorderId' for next reduction order. So the error is returning for the next reduction order in a loop.

All Answers

Raj VakatiRaj Vakati
From Setup, enter Order Settings in the Quick Find box, then select Order Settings.
Make sure that Enable Orders is selected.
Select Enable Reduction Orders.
Save your changes.
 
Order reductionOrder = new Order(
IsReductionOrder = true,
OriginalOrderId = originalOrder.Id,
Pricebook2Id = originalOrder.Pricebook2Id,
EffectiveDate = originalOrder.EffectiveDate,
Status = 'Draft',
AccountId = originalOrder.AccountId);

insert reductionOrder;

//insert reduction order products here

reductionOrder.Status = 'Activated';
update reductionOrder;

 
AnantGargAnantGarg
Reduction order is already enabled that’s why reduction order is being created successfully. Everything is working fine but the error message is still being returned
AnantGargAnantGarg
ok, I get this. there were some more reduction orders which were being created in my code in a loop and the first reduction order is replacing the 'orginalorderId' for next reduction order. So the error is returning for the next reduction order in a loop.
This was selected as the best answer