Tuesday, April 14, 2020

X++ code to apply GST tax adjustment

                                          
While importing invoices from an external system, we may find penny difference between how tax is calculated between these two systems. But this penny difference turns into big amount when we integrate/import thousands of invoices from an external system. This could be real pain in reconciliation. So we would need to match the taxes from both the systems before posting.

There is manual way to apply adjustment on tax document generated in D365 finance and operations, but user can not manually adjust those thousands of invoices.

This can be taken care through x++ code. Please find below the sample code to adjust CGST amount on Free text invoice before posting. This code updates CGST amount to 212 on free text invoice line.

class Custom_ApplyTaxAdjustment
{        
     public static void main(Args _args)
    {        
        RefRecId                        invRec = 68719504909;//hardcoded for testing purpose      
        CustInvoiceTable                invoiceTable;
        CustInvoiceLine                 custLine;
        TaxDocumentRowMeasureAdjustment taxAdj;
        
        ITaxableDocument            taxableDocumentObject;
        ITaxDocument                    taxDoc;
        
        ttsbegin;

        invoiceTable    =   CustInvoiceTable::findRecId(invRec);

        select custLine
                where custLine.ParentRecId    ==  invoiceTable.RecId;

        taxDoc=TaxBusinessService::getTaxDocumentBySource(invoiceTable.TableId, invoiceTable.RecId);
        
        taxableDocumentObject = TaxableDocumentObject::constructServer(TaxableDocumentDescriptorFactory::getTaxableDocumentDescriptor(invoiceTable));
        taxableDocumentObject.parmTaxDocument(taxDoc);
        
        TaxDocumentRowMeasureAdjustment::createAndUpdate(custLine.TableId, custLine.RecId,'Header/Lines/GST/CGST/Tax Amount',212,212,0,invoiceTable.TableId, invoiceTable.RecId); //hardcoded for testing purpose      
              
        TaxBusinessService::recalculateTax(taxableDocumentObject);
        ttscommit; 
    }

}

Please do comment if this code helps you and also let me know if you need any specific code. 
Thank you for reading...!! 

2 comments:

  1. From where are you going to take the RefRecID value? I understand it is hardcoded right now but from which table will you be getting it? Also, some explanation on this part will be helpful, since I'm a beginner here :

    TaxDocumentRowMeasureAdjustment::createAndUpdate(custLine.TableId, custLine.RecId,'Header/Lines/GST/CGST/Tax Amount',212,212,0,invoiceTable.TableId, invoiceTable.RecId); //hardcoded for testing purpose

    ReplyDelete
    Replies
    1. That is recid of CustInvoiceTable. You need to pass that while calling this code.

      Delete