Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

(0) ShareShare
ReportReport
Posted on by

Currently, I am struggling with overwriting the tax value inside sales & purchase line. I tried forcing a tax value inside a taxRegulation.allocateAmount(8888), however, (using Purchase order/ line as an example) that value was not successfully written into the VendInvoiceJour.SumTax. I am seeing the default tax percentage value in it (calculated through the tax group when setting up a purchase order)

As there is no error given by the AX, I really cannot find the source of the problem. I have also tried debugging through the allocateAmount and saveTaxRegulation, going through line by line to look for any potential leftout methods which I should call, but no luck...even after going through 3-4 times of debugging...(no customization done to standard AX methods, if this help in providing more info)

The code is as follows:

public void overwritePurchTaxRegulation(PurchTable _purchTable, PurchLine _purchLine, HISLedgerJournalImport _hISLedgerJournalImport)
    {
        TaxRegulation            taxRegulation;
        HISLedgerJournalImport   hisLedgerJournalImport = _hISLedgerJournalImport;

        PurchTable purchTable = PurchTable::find(_purchTable.PurchId);
        PurchLine  purchLine  = PurchLine::find(_purchLine.PurchId);
        if (!purchTable || !purchLine)
            return;
        
        taxRegulation = TaxRegulation::newTaxRegulation(PurchTotals::getTax(purchTable), null,
                                                        tableNum(PurchLine), purchLine.RecId);

        if(taxRegulation)
        {
            //taxRegulation.allocateAmount(hisLedgerJournalImport.LedgerJournalTrans_TaxAmount); 
            
            taxRegulation.allocateAmount(8888);
            taxRegulation.saveTaxRegulation();
        }
    }

Thanks in advance.

*This post is locked for comments

  • Zisis Profile Picture
    Zisis 40 on at
    RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    You can try to cast it if you will but the TaxRegulationDetail is a subclass of TaxRegulation. There is no guarantee that will always be a TaxRegulationDetail type.

  • AX user India Profile Picture
    AX user India 45 on at
    RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Hi Lincoln,

    taxRegulationDetail = TaxRegulation::newTaxRegulation(salesTotals.tax());

    In the above line you mention has a casting problem.

    Can you please tell how you acheived.

    Thanks

    AX User

  • Suggested answer
    Zisis Profile Picture
    Zisis 40 on at
    RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    I think the problem starts with the fact that you are trying to adjust the tax on the purchase order. (In the client you will actually see that you cant do that, you need an invoice to adjust the Tax).

    Having said that, your PurchTotals should be initialised from the VendInboiceInfoTable:

    VendInvoiceInfoTable vendInvoiceInfoTable = VendInvoiceInfoTable::findPurchId('O019786','');
        TaxRegulation       taxRegulation;
        PurchTotals         purchTotals;
        ;
        if (vendInvoiceInfoTable)
        {
            purchTotals = PurchTotals::newParmTable(vendInvoiceInfoTable,PurchUpdate::All,AccountOrder::None, vendInvoiceInfoTable.ParmId,'',vendInvoiceInfoTable.Ordering);
            purchTotals.calc();
            ttsBegin;
            taxRegulation = TaxRegulation::newTaxRegulation(purchTotals.tax());
            taxRegulation.allocateAmount(321);
            taxRegulation.saveTaxRegulation();
            ttsCommit;
       }

    The above will not work if you have multiple TaxCodes. You will have to do the adjustment per taxcode:

        VendInvoiceInfoTable vendInvoiceInfoTable = VendInvoiceInfoTable::findPurchId('PO019786','');
        TaxRegulation       taxRegulation;
        PurchTotals         purchTotals;
        TmpTaxRegulation    tmpTaxRegulation;
        ;
        if (vendInvoiceInfoTable)
        {
            purchTotals = PurchTotals::newParmTable(vendInvoiceInfoTable,PurchUpdate::All,AccountOrder::None, vendInvoiceInfoTable.ParmId,'',vendInvoiceInfoTable.Ordering);
            purchTotals.calc();
            ttsBegin;
            taxRegulation = TaxRegulation::newTaxRegulation(purchTotals.tax());
            tmpTaxRegulation  = taxRegulation.tmpTaxRegulationData();
    
            while select tmptaxRegulation
            {
    			//tmpTaxRegulation.taxCode is your taxCode
                tmpTaxRegulation.SourceRegulateAmountCur = 130;
                tmpTaxRegulation.OverrideCalculatedTax =true;
                tmpTaxRegulation.update();
                taxRegulation.updateOverrideCalculatedTax(tmpTaxRegulation);
                taxRegulation.saveTaxRegulation();
            }
            ttsCommit;
        }
  • RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Thanks, Linkoln!

  • Verified answer
    RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Hello Alexey

    I know my reply is kinda late, but I have replied to this post in case you or anyone else is still looking for a solution.

    My team has found a solution which appeared to work out well for us.

    I have tried modifying the code for an easier understanding for anyone out there with the same problem.

    A quick note though: I have not tested out this modified code yet, but it should work for you.

    Here is the code for your reference:

    public void overwriteSalesTaxRegulation(container _conRecords, SalesId _axSalesId, SalesParmTable _salesParmTable)
        {  
            TaxRegulationDetail     taxRegulationDetail;
            SalesTotals             salesTotals;
            TmpTaxRegulation        tmpTaxRegulationDetail;
            HISLedgerJournalImport  hisLedgerJournalImport;
            int                     num = 1;
            SalesLine               salesLine;
    
            while (num <= conLen(_conRecords))
            {
                hisLedgerJournalImport = conpeek(_conRecords,num);
                num++;
    
                if(_salesParmTable.SalesId != _axSalesId)
                    continue ;
    
                salesTotals = SalesTotals::construct(_salesParmTable, SalesUpdate::All, AccountOrder::Order, _salesParmTable.ParmId,
                                                        '',_salesParmTable.Ordering);
                salesTotals.calc();
                taxRegulationDetail = TaxRegulation::newTaxRegulation(salesTotals.tax());
    
                tmpTaxRegulationDetail = taxRegulationDetail.tmpTaxRegulationDetail();
    
                // The target sales line to get
                select salesLine
                    where salesLine.SalesId == _axSalesId
                        && salesLine.ExternalItemId == hisLedgerJournalImport.HISSalesExternalItemId 
                        && salesLine.Name           == hisLedgerJournalImport.HISSalesItemTxt        
                        && salesLine.SalesQty       == hisLedgerJournalImport.HISSalesOrderedQty     
                        && salesLine.SalesPrice     == hisLedgerJournalImport.HISSalesPrice          
                        && salesLine.LineAmount     == hisLedgerJournalImport.HISSalesLineAmount     
                        && salesLine.TaxGroup       == hisLedgerJournalImport.LedgerJournalTrans_TaxGroup     
                        && salesLine.TaxItemGroup   == hisLedgerJournalImport.LedgerJournalTrans_TaxItemGroup 
                join tmpTaxRegulationDetail
                    where tmpTaxRegulationDetail.SourceRecId            == salesLine.RecId
                        && tmpTaxRegulationDetail.SourceTableId         == tableNum(SalesLine)
                        && tmpTaxRegulationDetail.TaxAdjustmentType     == TaxAdjustmentType::Detail
                        && tmpTaxRegulationDetail.OverrideCalculatedTax == NoYes::No;
    			
    			//skip if no sales line was retrieved
                if(!salesLine)
                    continue ;
    
                if(tmpTaxRegulationDetail.SourceRegulateAmountCur != TaxAmountToOverwriteWith)
                {
                    taxRegulationDetail.updateTaxRegulationAmount(tmpTaxRegulationDetail, TaxAmountToOverwriteWith, true);
                    taxRegulationDetail.saveTaxRegulation();
                }
            }
        }


    Try it out and see if it works for you.

  • Alexey Tolkov Profile Picture
    Alexey Tolkov 5 on at
    RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Hi,

    I faced the same problem.. Have you found any answer?

  • RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Bump. Still seeking for help/ advice.

  • RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Bump, still seeking for help with this problem. It is quite urgent..in fact,very urgent.

    I have recreated all my scripts into a super summerized version and tried executing it inside the main method of a runnable class (with hard-coded purchId, fields, etc), and yet still having the same problem.

    There is something wrong with what I did, which after debugging, I still don't know where the problem lies with.

    Is it the problem/ timing where I change my tax amount?

    At which point do you call the method to change the tax amount?

    Right after invoice's comfirmation? Right after the init of purchFormLetter (purchFormLetter.init())?

    Even when I comment out my code for allocateAmount, D365 is using the default tax value (because I have assigned the tax group).

    If anyone has any idea/ faced or are currently facing the same problem as me, please do share your solution/ what methods have you tried to tackle this problem.

    Thanks in advance.

    Regards

    Lincoln

  • RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Hello,
    Thanks for the response.
    I have tried your suggestion and pass a lineNum parameter to

    PurchLine::find(_purchLine.PurchId, _purchLine.lineNum);

    But it is still appearing with the same problem.

    Just to double confirm that other purchase line is not messing up everything else, I only tried creating 1 purchase line using X++. Does the creation of invoice (using PurchFormLetter) overwrites my manually set tax value, inside any of the init method of PurchFormLetter? I kept trying in debugging but I cannot find anywhere suspicious...

    Manual creation of purchase order >> purchase line >> confirmation >> invoice >> manually set tax amount does seem to work. However, my X++ code is not working as expected.

    This is getting really desperate...and depressing.

    Kinds regards

    Lincoln

  • Temur Mukbaniani Profile Picture
    Temur Mukbaniani 260 on at
    RE: Using X++ to manually adjust the tax value inside Sales/ Purch lines during invoice stage

    Hi,

    try to pass lineNum also to "find" method of PurchLine. Without it, you always get the first line, if there are multiple lines.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,436 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans