web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Invoice Approval journal create and Post through X++ job

(0) ShareShare
ReportReport
Posted on by 135

After posting Invoice register ,unique voucher no will be created which cen be used to fetch vouchers and post it.

I am trying to create a Invoice Approval Journal through X++ job , But facing difficulty to create the extra line which is getting created from the form\create method , when doing the same manually.

Without that Extra line , system throw error while posting the Invoice Approval Journal.

Below are the steps which I am performing.

Step1 - I have xml file ,with voucher details.(Extracting the Voucher No from the xml)

Step 2 - I am fetching that Voucher no from the xml as of now , to create the entries in Invoice Approval journal .

My code :-

static void CreateInvoiceApprovalJournal(Args _args)
{

    //Variable Creation
    #define.node('axMsg:'BInayxml')
    XmlDocument xmlDocument;
    XmlNodeList lineList;
    XmlNode lineRow;
    int         i;
    int         j, k,cnt;
    str va;
    Voucher voucher;
    str fileName                = 'C:\\binay\\myxml2.xml';

    LedgerJournalTable  ledgerJournalTable;
    LedgerJournalNameIdDaily    JOurnalnum;
    LedgerJournalType  ledgerJournalType;
    LedgerJournalTrans  lt,lt2;
     LedgerJournalEngine_VendApprove     ledgerJournalEngine;
   

//Read XML CODE
    xmlDocument             = xmlDocument::newFile(fileName);
    lineList = xmlDocument.getElementsByTagName('LedgerTrans');
     lineRow = lineList.nextNode();
    while (lineRow)
    {
         voucher = lineRow.getNamedElement('axLGJ:Voucher').text();

        lineRow = lineList.nextNode();
    }

    //Invoice Approval Header Creation
    ledgerJournalTable.initValue();
    ledgerJournalTable.Name = 'MyTest';
    ledgerJournalTable.JournalName = 'BINJRN';
    ledgerJournalTable.JournalType =ledgerJournalType::Approval;
    ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();

    //ledgerJournalTable.
    ledgerJournalTable.insert();
   
    //Create Lines
    Select * from lt where lt.Voucher == voucher;
   
    ledgerJournalTable     = LedgerJournalTable::find(ledgerJournalTable.JournalNum);
    lt      = LedgerJournalTrans::findRecId(lt.RecId, false);
      
    LedgerJournalEngine_Server::addVoucher(ledgerJournalTable, lt); // Create Invoice Journal Lines

       
}

After running this job , 3 lines are getting created but In manual creation of Invoice Journal Approval (using Find Vouchers action) at the top of Invoice Journal Approval (Lines) form ,total no of lines created is 4 (RECID = 0)

Can anyone please help me how to create the 4th line through job as without that Journal cannot be posted.

For reference - Extra Line creation is being done by code written in

(\Forms\LedgerJournalTransApprove\Data Sources\LedgerJournalTrans\Methods\create ) - Standard code

  {
            ledgerJournalTransEntityManager.getFormDataEventManager().beginEntityCreateEvent();

            ledgerJournalEngine.preCreate(ledgerJournalTrans); 
            element.setDebCredProposal(true);

            super(append);

}

Please let me know if anyone needs more details .

*This post is locked for comments

I have the same question (0)
  • Verified answer
    BINAY KUMAR PANDEY Profile Picture
    135 on at

    I have created a class which read the xml ,pick the voucher no and create the Invoice approval journal and also post it.

    As I am beginner in AX ,any suggestion to improve the quality of code will be highly appreciated.

    Follow the below steps

    Method 1-

    class CreateInvoiceApprovalJournalBatch

    {

       #define.node('binayxml')

       XmlDocument                          xmlDocument;

       XmlNodeList                            lineList;

       XmlNode                                 lineRow;

       int                                            cnt;

       int64                                        nRecId;

       str                                            fileName,pfilename;

       Voucher                                   voucher;

       VendTrans                               vendTrans;

       AmountCurDebit                    amountCurDebit;

       AmountCurCredit                   amountCurCredit;

       AmountCurCredit                   total;

       Approved                               approved;

       LedgerJournalACType            acttype;

       LedgerJournalCheckPost        ledgerJournalCheckPost;

       LedgerJournalTable                ledgerJournalTable;

       LedgerJournalNameIdDaily    JOurnalnum;

       LedgerJournalType                 ledgerJournalType;

       LedgerJournalTrans                ledgerJournalTrans,_ledgerJournalTrans;

       FileIoPermission                     _perm;

       //MoveFileName

       #Evat_NL

       #File

       // variable declaration to retrieve files from folder

       Filename                              ProcessedFolder;

       container                             filesS;

       int                                         i;

       str                                         charHolder;

    }

    Mehod-2

    public container findMatchingFiles(str _folderPath  ,   str _filePattern   = '*.*')

    {

       System.IO.DirectoryInfo     directory;

       System.IO.FileInfo[]        files;

       System.IO.FileInfo          file;

       InteropPermission           permission;

       counter     filesCount;

       counter     loop;

       container   mathchingFiles;

       ;

       permission  = new InteropPermission(InteropKind::ClrInterop);

       permission.assert();

       directory   = new System.IO.DirectoryInfo(_folderPath);

       files       = directory.GetFiles(_filePattern);

       filesCount  = files.get_Length();

       for (loop = 0; loop < filesCount; loop++)

       {

           file            = files.GetValue(loop);

           fileName        = file.get_FullName();

           mathchingFiles  = conins(mathchingFiles, conlen(mathchingFiles) + 1, fileName);

       }

       CodeAccessPermission::revertAssert();

       return mathchingFiles;

    }

    Method3:-

    [SysEntryPointAttribute(false)]

    public void processRecords()

    {

       amountCurCredit =   0;

       amountCurDebit  =   0;

       //XML FILE LOCATION

       filesS = this.findMatchingFiles(@"\\xyz\BINAY\Source");//Source File location

       ProcessedFolder =   @"\\abc\\ProcessedXmlFile\";//Processed File Location -      

                                                                                      //Destination

       try

       {

           for (i = 1; i <= conlen(filesS); i++)

           {

               fileName = conpeek(filesS, i);

               _perm = new FileIoPermission(fileName,'r');

               _perm.assert();

               info(fileName);

               //Read XML CODE

               xmlDocument             = xmlDocument::newFile(fileName);

               lineList = xmlDocument.getElementsByTagName('axLGJ:LedgerJournalTrans');

               cnt = lineList.length();

               lineRow = lineList.nextNode();

               while (lineRow)

               {

                   voucher = lineRow.getNamedElement(binay:Voucher').text();

                   if(!voucher)

                   {

                       throw Global::error("Invalid Voucher No - %1");

                   }

                 lineRow = lineList.nextNode();

               }

               //Invoice Approval Header Creation

               ledgerJournalTable.initValue();

               ledgerJournalTable.Name = 'X++ Job ROws ';

               ledgerJournalTable.JournalName = 'AP-XXXX';//Journal Name

               ledgerJournalTable.JournalType =ledgerJournalType::Approval;

               ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();

               //ledgerJournalTable Insertion

               ledgerJournalTable.insert();

               //Create Lines

               Select firstOnly ledgerJournalTrans

                   where ledgerJournalTrans.Voucher == voucher;

               ledgerJournalTable      = LedgerJournalTable::find(ledgerJournalTable.JournalNum);

               ledgerJournalTrans      = LedgerJournalTrans::findRecId(ledgerJournalTrans.RecId, false);

               //Create 3 Lines

               LedgerJournalEngine_Server::addVoucher(ledgerJournalTable, ledgerJournalTrans);

               select firstOnly voucher from vendTrans

                   where vendTrans.Voucher ==  voucher;

               ledgerJournalTrans.VendTransId = vendTrans.RecId;

               Select sum(AmountCurCredit),sum(AmountCurDebit) from ledgerJournalTrans

                   where ledgerJournalTrans.Voucher        == voucher

                       && ledgerJournalTrans.AccountType   == LedgerJournalACType::Ledger

                       && ledgerJournalTrans.Approved      == NoYes::Yes;

               if(ledgerJournalTrans)

               {

                   amountCurCredit = ledgerJournalTrans.AmountCurCredit;

                   amountCurDebit  = ledgerJournalTrans.AmountCurDebit;

                   total           = abs(amountCurDebit - amountCurCredit);

               }

               ledgerJournalTrans.clear();

               select * from ledgerJournalTrans

                   where ledgerJournalTrans.Voucher            == voucher

                       && ledgerJournalTrans.Approved          == NoYes::Yes

                       && ledgerJournalTrans.AccountType       == LedgerJournalACType::Ledger

                       && ledgerJournalTrans.AmountCurCredit   == total;

               //Insert 4th Ledger Journal Trans Line

               if (ledgerJournalTrans)

               {

                    ttsbegin;

                   _ledgerJournalTrans.initValue();

                   _ledgerJournalTrans.Txt                   = ledgerJournalTrans.Txt;

                   _ledgerJournalTrans.TransDate             = ledgerJournalTrans.TransDate;

                   _ledgerJournalTrans.CurrencyCode          = ledgerJournalTrans.CurrencyCode;

                   _ledgerJournalTrans.AmountCurDebit        = total;

                   _ledgerJournalTrans.Voucher               = ledgerJournalTrans.Voucher;

                   _ledgerJournalTrans.AccountType           = ledgerJournalTrans.AccountType;

                   _ledgerJournalTrans.Approved              = ledgerJournalTrans.Approved;

                   _ledgerJournalTrans.Approver              = ledgerJournalTrans.Approver;

                   _ledgerJournalTrans.Company               = ledgerJournalTrans.Company;

                   _ledgerJournalTrans.CurrencyCode          = ledgerJournalTrans.CurrencyCode;

                   _ledgerJournalTrans.CashDiscCode          = ledgerJournalTrans.CashDiscCode;

                   _ledgerJournalTrans.DateCashDisc          = ledgerJournalTrans.DateCashDisc;

                   _ledgerJournalTrans.DocumentDate          = ledgerJournalTrans.DocumentDate;

                   _ledgerJournalTrans.DocumentNum           = ledgerJournalTrans.DocumentNum;

                   _ledgerJournalTrans.Due                   = ledgerJournalTrans.Due;

                   _ledgerJournalTrans.ForeignCompany        = ledgerJournalTrans.ForeignCompany;

                   _ledgerJournalTrans.Invoice               = ledgerJournalTrans.Invoice;

                   _ledgerJournalTrans.vatNumJournal         = ledgerJournalTrans.vatNumJournal;

                   _ledgerJournalTrans.JournalNum            = ledgerJournalTrans.JournalNum;

                   _ledgerJournalTrans.Payment               = ledgerJournalTrans.Payment;

                   _ledgerJournalTrans.PaymentAccount        = ledgerJournalTrans.PaymentAccount;

                   _ledgerJournalTrans.PaymentNotes          = ledgerJournalTrans.PaymentNotes;

                   _ledgerJournalTrans.PaymReference         = ledgerJournalTrans.PaymReference;

                   _ledgerJournalTrans.PaymentStatus         = ledgerJournalTrans.PaymentStatus;

                   _ledgerJournalTrans.PaymId                = ledgerJournalTrans.PaymId;

                   _ledgerJournalTrans.PaymMode              = ledgerJournalTrans.PaymMode;

                   _ledgerJournalTrans.PaymSpec              = ledgerJournalTrans.PaymSpec;

                   _ledgerJournalTrans.PostingProfile        = ledgerJournalTrans.PostingProfile;

                   _ledgerJournalTrans.SettleVoucher         = ledgerJournalTrans.SettleVoucher;

                   _ledgerJournalTrans.PurchLedgerPosting    = ArrivalPostingType::None;

                   _ledgerJournalTrans.Invisible             = NoYes::No;

                   _ledgerJournalTrans.NoEdit                = NoYes::No;

                   _ledgerJournalTrans.TransactionType       = ledgerJournalTrans.TransactionType;

                   _ledgerJournalTrans.LedgerDimension       = ledgerJournalTrans.LedgerDimension;

                   _ledgerJournalTrans.ExchRate              = ledgerJournalTrans.ExchRate;

                   _ledgerJournalTrans.ExchRateSecond        = ledgerJournalTrans.ExchRateSecond;

                   _ledgerJournalTrans.Triangulation         = ledgerJournalTrans.Triangulation;

                   _ledgerJournalTrans.VendTransId           = vendTrans.RecId;

                   _ledgerJournalTrans.insert();

                   //Posting Ledger Journal Trans

                   ledgerJournalCheckPost                    =    ledgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes);

                   ledgerJournalCheckPost.run();

                   pfilename = ProcessedFolder + Voucher +'.xml';

                   //Move file after processing

                   System.IO.File::Move(fileName,pfilename);

                   ttsCommit;

               }

           }

       }

       catch(Exception::Error)

       {

           info("Caught 'Exception::Error'.");

       }

    }

    Add the class in the Action Menu Item ,to make it batch executable do below steps

    Action/MenuItem /Properties

    Object Type - Class

    Object -SysOperationServiceController

    Parameters-ClassName.method

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans