Wednesday, February 14, 2024

Code to create the Invoice approval in D365 f&o

 My requirement is to create invoice approvals for the posted invoice register lines which were available in find voucher of invoice approval.

AP>Invoice approval>Create journal>Find voucher.


I need to create two journals with the details present in image.

class VendInvoiceApprovalAutomationService extends SysOperationServiceBase

{

   public void processRecords()

   {

       QueryRun                queryRun = this.query();

       LedgerJournalTrans      ledgerJournalTrans;

       LedgerJournalTable      ledgerJournalTable;

 

       while(queryRun.next())

       {

           ledgerJournalTrans = queryRun.get(tableNum(LedgerJournalTrans));

 

           try

           {

               ttsbegin;

               this.createHeader(ledgerJournalTable);

               if (ledgerJournalTable.RecId && ledgerJournalTrans.RecId)

               {

                   this.createApprovalLines(ledgerJournalTrans,ledgerJournalTable);

                   Info(strFmt("Vendor approval journal created -%1",ledgerJournalTable.JournalNum));

               }

               else

               {

                   throw error("Record not found.");

               }

               ttscommit;

           }

           catch

           {

               continue;

           }

       }

   }

 

   public void createApprovalLines(LedgerJournalTrans _ledgerJournalTrans,LedgerJournalTable _ledgerJournalTable)

   {

       LedgerJournalTrans                  ledgerJournalTrans,ledgerJournalTransLoc,ledgerJournalTransNew;

       RefRecId                            ledgerJournalTransRecId = 0;

       ledgerJournalTransRecId = _ledgerJournalTrans.RecId;

 

       LedgerJournalEngine_VendApprove  ledgerJournalEngine = LedgerJournalEngine::construct(LedgerJournalType::Approval);

       LedgerJournalEngine_Server::addVoucher(_ledgerJournalTable,LedgerJournalTrans::findRecId(ledgerJournalTransRecId,false));

 

       select ledgerJournalTransLoc where ledgerJournalTransLoc.RecId == ledgerJournalTransRecId;

       select firstonly ledgerJournalTrans

           where ledgerJournalTrans.JournalNum == _ledgerJournalTable.JournalNum;

 

       ledgerJournalEngine.newJournalActive(_ledgerJournalTable);

       ledgerJournalEngine.preCreate(ledgerJournalTrans);

       ledgerJournalEngine.active(ledgerJournalTransLoc);

 

       ledgerJournalTransNew.initValue();

 

       ledgerJournalTransNew.VendTransId = ledgerJournalTransLoc.VendTransId;

       ledgerJournalTransNew.AccountType = ledgerJournalTransLoc.AccountType;

       ledgerJournalTransNew.JournalNum  = _ledgerJournalTable.JournalNum;

       ledgerJournalEngine.initValue(ledgerJournalTransNew);

       ledgerJournalTransNew.setDefaultAccount(true, ledgerJournalTransLoc.parmAccount());

       ledgerJournalTransNew.LineNum = LedgerJournalTrans::firstLineNum(_ledgerJournalTable.JournalNum) - 1;

       if (ledgerJournalTransNew.LineNum == 0)

       {

           ledgerJournalTransNew.LineNum = -1;

       }

       ledgerJournalTransNew.CurrencyCode = ledgerJournalTransLoc.CurrencyCode;

       ledgerJournalEngine.preWrite(ledgerJournalTransNew);

       ledgerJournalEngine.preWriteUpdateDefaultDimension(ledgerJournalTransNew);

       ledgerJournalTransNew.insert();

       ledgerJournalEngine.write(ledgerJournalTransNew);

       

   }

 

   public void createHeader(LedgerJournalTable      ledgerJournalTable)

   {

       LedgerJournalName  ledgerJournalName;

 

       select firstonly ledgerJournalName

           where ledgerJournalName.JournalType == LedgerJournalType::Approval;

       if (!ledgerJournalName)

       {

           throw Error ("Journal Name does not exists in the system.");

       }

       ledgerJournalTable.clear();

       ledgerJournalTable.initValue();

       ledgerJournalTable.initFromLedgerJournalName(ledgerJournalName.JournalName);

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

       ledgerJournalTable.JournalType = LedgerJournalType::Approval;

       ledgerJournalTable.insert();

   }

 

   public QueryRun query()

   {

       QueryBuildDataSource qbds;

       QueryBuildRange qbr;

       LedgerJournalId vendInvoicePoolJournalNum = LedgerJournalTable::findVendorInvoicePool().JournalNum;

       Query query = new Query();

       qbds = query.addDataSource(tablenum(LedgerJournalTrans));

 

       qbr = qbds.addRange(fieldnum(LedgerJournalTrans, JournalNum ));

       qbr.value(queryValue(vendInvoicePoolJournalNum));

 

       qbr = qbds.addRange(fieldnum(LedgerJournalTrans, AccountType));

       qbr.value(queryValue(LedgerJournalACType::Vend));

 

       qbr = qbds.addRange(fieldnum(LedgerJournalTrans, Approved));

       qbr.value(queryValue(NoYes::No));

 

       qbds = qbds.addDataSource(tablenum(LedgerJournalTransAccountView));

       qbds.addLink(fieldnum(LedgerJournalTrans, RecId), fieldnum(LedgerJournalTransAccountView, RecId1));

       qbds.joinMode(JoinMode::ExistsJoin);

       qbds.fetchMode(QueryFetchMode::One2One);

 

       qbds = qbds.addDataSource(tablenum(VendTrans));

       qbds.joinMode(JoinMode::ExistsJoin);

       qbds.fetchMode(0);

 

       qbds.addLink(fieldnum(LedgerJournalTransAccountView, AccountNum), fieldnum(VendTrans, AccountNum));

       qbds.addLink(fieldnum(LedgerJournalTransAccountView, Voucher), fieldnum(VendTrans, Voucher));

       qbds.addLink(fieldnum(LedgerJournalTransAccountView, TransDate), fieldnum(VendTrans, TransDate));

       qbds.addLink(fieldnum(LedgerJournalTransAccountView, Invoice), fieldnum(VendTrans, Invoice));

 

       qbr = qbds.addRange(fieldnum(VendTrans, Arrival));

       qbr.value(queryValue(NoYes::Yes));

 

       qbr = qbds.addRange(fieldnum(VendTrans, JournalNum));

       qbr.value(queryValue(vendInvoicePoolJournalNum));

       

       QueryRun  qr = new QueryRun(query);

 

       return qr;

   }

}

.....................................................

Output :




No comments:

Post a Comment