Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Applying Misc Charges by code

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    Re: Applying Misc Charges by code

    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;
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans