Hello friends.
I have the following code in where I make a call in SalesLine.insert to call this method to SalesTable to create a misc charge by code:
void CreateUpdateMiscCharge(MarkUpValue _freightCharge, boolean _createOK=true)
{
MarkUpTrans markUpTrans;
MarkUpValue freight;
;
freight = decRound(_freightCharge, 2);
ttsbegin;
info(strfmt("SalesTable ID: %1", this.TableId));
info(strfmt("SalesTable RecID: %1", this.RecId));
select firstonly forupdate markUpTrans
where markUpTrans.TransTableId == this.TableId &&
markUpTrans.TransRecId == this.RecId &&
markUpTrans.MarkupCode == 'SomeCode1';
if (markUpTrans)
{
markUpTrans.Value = freight;
markUpTrans.update();
}
else if (_createOK)
{
markUpTrans.initFromSalesTable(this);
markUpTrans.Value = freight;
markUpTrans.MarkupCode = 'SomeCode1';
markUpTrans.Txt = MarkUpTable::find(markUpTrans.ModuleType,'SomeCode1').Txt;
markUpTrans.insert();
ttscommit;
}
The problem I am having is applying a SECOND misc charge of a different markUpCode to the same order.
So if I call CreateUpdateMiscCharge and then CreateUpdateMiscCharge2 (2 is outlinedbelow), I get an error:
"Cannot create a record in markUpTrans the record already exists.
void CreateUpdateMiscCharge2(MarkUpValue _freightCharge, boolean _createOK=true)
{
MarkUpTrans markUpTrans;
MarkUpValue freight;
;
freight = decRound(_freightCharge, 2);
ttsbegin;
info(strfmt("SalesTable ID: %1", this.TableId));
info(strfmt("SalesTable RecID: %1", this.RecId));
select firstonly forupdate markUpTrans
where markUpTrans.TransTableId == this.TableId &&
markUpTrans.TransRecId == this.RecId &&
markUpTrans.MarkupCode == 'SomeCode2';
if (markUpTrans)
{
markUpTrans.Value = freight;
markUpTrans.update();
}
else if (_createOK)
{
markUpTrans.initFromSalesTable(this);
markUpTrans.Value = freight;
markUpTrans.MarkupCode = 'SomeCode2';
markUpTrans.Txt = MarkUpTable::find(markUpTrans.ModuleType,'SomeCode2').Txt;
markUpTrans.insert();
ttscommit;
}
The error is being thrown in the markUpTrans.insert() call.
I was thinking that the identical TransRecId and the TransTableId records in the MarkUpTrans table are what is causing the error but upon investigating some existing records, I see that there are cases were there are two records both with the same TransRecId and TransTableId.
The only difference is that one misc charge record was created programmatically (using above code) but the other was set to calculate out of the box using the "auto misc charge" option in AR > Setup > Misc Charge area.
Any ideas are greatly appreciated! Thank you!
*This post is locked for comments