
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
I have the same question (0)Hey guys,
Found out what was wrong with this. You have to bump up the line number in MarkUpTrans in order to create another misc charge by code. It was erroring because both misc charges I was trying to create had a line number set to zero. So the way I solved this was to check for the previous mark up code and bump the line up 1 so that they both went in. Works great. Thought this might be useful to others who use this. Code changes below:
void CreateUpdateMiscCharge2(MarkUpValue _freightCharge, boolean _createOK=true)
{
MarkUpTrans markUpTrans, localMarkUpTrans;
MarkUpValue freight;
;
freight = decRound(_freightCharge, 2);
ttsbegin;
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)
{
//check if SomeCode1 is there and bump up the line number if so, so it creates a new record
select firstonly localMarkUpTrans
where localMarkUpTrans.TransTableId == this.TableId &&
localMarkUpTrans.TransRecId == this.RecId &&
localMarkUpTrans.MarkupCode == 'SomeCode1';
if(localMarkUpTrans)
{
markUpTrans.LineNum = localMarkUpTrans.LineNum + 1;
}
markUpTrans.initFromSalesTable(this);
markUpTrans.Value = freight;
markUpTrans.MarkupCode = 'SomeCode2';
markUpTrans.Txt = MarkUpTable::find(markUpTrans.ModuleType,'SomeCode2').Txt;
markUpTrans.insert();
ttscommit;
}