Tuesday, October 31, 2023

Code to populate data from BillingSchedule to SalesOrder for custom fields in D365 X++

 [Extensionof(classStr(SubBillCreateSalesOrder))]

internal final class DaxSubBillCreateSalesOrder_Extension

{

   public static SalesTable initSalesTable(

       SalesTable _salesTable,

       boolean _isInvoiceCreator,

       NumberSeq _numberSeq,

       SubBillSalesLineConsolidated _salesLineConsolidated,

       ParmId _curParmId)

   {

      

       SubBillScheduleTable    subBillScheduleTable;

 

       next initSalesTable(_salesTable,_isInvoiceCreator,_numberSeq,_salesLineConsolidated,_curParmId);

    

       select subBillScheduleTable

           where subBillScheduleTable.SubBillBillingScheduleNumber == _salesLineConsolidated.SubBillBillingScheduleNumber;

       _salesTable.DaxName = subBillScheduleTable.DaxName;

 

       return _salesTable;

   }

}

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

Output :

SB>All Billing Schedules


AR>All sales Orders






Friday, October 20, 2023

Code to create a AP invoice journal in D365F&O X++

public static void main(Args _args)

   {

       MCRLedgerJournal        journalTable;

       LedgerJournalTable      ledgerJournalTable;

       Counter                 recordsInserted;

        ttsbegin;

       //Creates the journal header table

       journalTable = new MCRLedgerJournal_VendInvoiceRegister(LedgerJournalType::VendInvoiceRegister,"APInvoice");

       ledgerJournalTable = journalTable.createLedgerJournalTable();

       journalTable.parmLedgerJournalTable(ledgerJournalTable);

       journalTable.parmMCRCCGeneralLedgerId();

       journalTable.parmCurrencyCode(CompanyInfoHelper::standardCurrency());

 

       // creates a new line in general journal

       journalTable.parmLineNum();

       journalTable.parmLedgerAccountType(LedgerJournalACType::Vend);

       journalTable.parmLedgerAccount(LedgerDynamicAccountHelper::getDynamicAccountFromAccountNumber("1001",LedgerJournalACType::Vend)); //You need to use the vendor account as per your environment's data.

       journalTable.parmTransDate(today());

       journalTable.parmTransTxt("Acquisition");

       journalTable.parmInvoice("123");

       journalTable.parmExchRate(0.00);

       journalTable.createLedgerJournalTrans(abs(0),abs(100),LedgerJournalACType::Vend);

       recordsInserted++;

       ttscommit;

       info(strFmt("Journal number %1 is created", ledgerJournalTable.JournalNum));

   }

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


AP>Invoices>Invoice Journal





Friday, October 13, 2023

Code to get vendor Voucher transcations from Mainaccount in D365 X++

 My requirement is to get the vendor for main account transcations if the voucher is available.

        MainAccount                 mainAccount;

       GeneralJournalAccountEntry  generalJournalAccountEntry;

       GeneralJournalEntry         generalJournalEntry,generalJournalEntryloc;

       VendTrans                   vendTrans;

       while select generalJournalAccountEntry

           where generalJournalAccountEntry.MainAccount == mainAccount.RecId // get the current record of main account.

       {

           select generalJournalEntry

               where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry;

 

           select vendTrans

               where vendTrans.Voucher == generalJournalEntry.SubledgerVoucher;

           if(vendTrans)

           {

               update_recordset generalJournalEntryloc

                       setting AccountNum = vendTrans.AccountNum

                   where generalJournalEntryloc.RecId == generalJournalEntry.RecId;

           }

       }

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

Other Way :

[ExtensionOf(formDataSourceStr(LedgerTransAccount, GeneralJournalAccountEntry))]

internal final class DaxLedgerTransAccountForm_Extension

{

   public display VendAccount displayVendAccount(GeneralJournalAccountEntry _entry)

   {

       NoYes multipleTransWithinOneVoucher = LedgerParameters::find().AllowMultipleTransactionsWithinOneVoucher;

       VendTable vendorTable = this.findVendTable(_entry);

       VendAccount vendAccounts = vendorTable.AccountNum;

 

       if (multipleTransWithinOneVoucher == NoYes::Yes && vendorTable)

       {

           vendorTable = this.findVendTableWithGeneralJournalEntry(vendAccounts, _entry);

 

           if (vendorTable)

           {

               vendAccounts += "@GeneralLedger:LedgerTransVoucherCustVendMultiple";

           }

        }

       return vendAccounts;

   }

   private VendTable findVendTable(GeneralJournalAccountEntry _entry)

   {

       VendTable vendorTable;

       VendTrans vendTrans;

       GeneralJournalEntry vendGeneralJournalEntry;

       LedgerJournalVoucherChanged ledgerJournalVoucherChanged;

       LedgerJournalVoucherChanged ledgerJournalAndApprovalVoucherChanged;

           select firstonly AccountNum from vendorTable

               exists join vendGeneralJournalEntry

                   where vendGeneralJournalEntry.RecId == _entry.GeneralJournalEntry

               exists join vendTrans

                   where vendTrans.AccountNum == vendorTable.AccountNum

                       && vendGeneralJournalEntry.SubledgerVoucher == vendTrans.Voucher

                       && vendGeneralJournalEntry.SubledgerVoucherDataAreaId == vendTrans.DataAreaId;

 

       if (!vendorTable.AccountNum)

       {

           select firstonly AccountNum from vendorTable

                   exists join vendGeneralJournalEntry

                       where vendGeneralJournalEntry.RecId == _entry.GeneralJournalEntry

                   exists join ledgerJournalVoucherChanged

                       where ledgerJournalVoucherChanged.ToVoucher == vendGeneralJournalEntry.SubledgerVoucher

                           && ledgerJournalVoucherChanged.Todate == vendGeneralJournalEntry.AccountingDate

                   exists join vendTrans

                       where vendTrans.AccountNum == vendorTable.AccountNum

                           && ledgerJournalVoucherChanged.FromVoucher == vendTrans.Voucher

                           && ledgerJournalVoucherChanged.FromDate == vendTrans.TransDate

                           && vendGeneralJournalEntry.SubledgerVoucherDataAreaId == vendTrans.DataAreaId;

       }

       return vendorTable;

   }

   private VendTable findVendTableWithGeneralJournalEntry(VendAccount _vendAccounts, GeneralJournalAccountEntry _entry)

   {

       VendTable vendorTable;

       VendTrans vendTrans;

       GeneralJournalEntry vendGeneralJournalEntry;

           select firstonly AccountNum from vendorTable

                   exists join vendGeneralJournalEntry

                       where vendGeneralJournalEntry.RecId == _entry.GeneralJournalEntry

                   exists join vendTrans

                       where vendTrans.AccountNum == vendorTable.AccountNum

                           && vendGeneralJournalEntry.SubledgerVoucher == vendTrans.Voucher

                           && vendGeneralJournalEntry.SubledgerVoucherDataAreaId == vendTrans.DataAreaId

                           && vendorTable.AccountNum != _vendAccounts;

       return vendorTable;

   }

}


Output :

General Ledger >> Chart of Accounts >> Accounts >> Main Accounts

Click on the transactions

Account number data is from form init method

Vendor Account is from display method.









Tuesday, October 10, 2023

Code to terminate the schedule line in D365F&O X++

 

public static void main(Args _args)

   {

       SubBillScheduleTable    SubBillScheduleTable;

       SubBillScheduleLine     SubBillScheduleLine;

       SubBillTerminationType  enumtype = SubBillTerminationType::AdjustSchedule;

       int numLinesProcessed = 0;

 

       select SubBillScheduleTable

           where SubBillScheduleTable.SubBillBillingScheduleNumber == "USMF-000000001";

      while select SubBillScheduleLine

           where SubBillScheduleLine.SubBillBillingScheduleNumber ==  SubBillScheduleTable.SubBillBillingScheduleNumber

           && SubBillScheduleLine.SubBillScheduleStatus == SubBillScheduleStatus::Active

       {

           SubBillScheduleLine.terminate(mkDate(04,10,2023),enumtype,"PRICE","price","");

           //SubBillScheduleTable.checkScheduleTermination();

           ++numLinesProcessed;

       }

       if (numLinesProcessed == 1)

       {

           info("@SubBill:OneScheduleLineTerminated");

       }

       else

       {

           info(strFmt("@SubBill:ScheduleLinesTerminated", numLinesProcessed));

       }

   }

Code to terminate with refund the billing schedule line in D365 X++

 

public static void main(Args _args)

   {

       boolean                             isScheduleHeader;

       SubBillScheduleTable                scheduleTable = SubBillScheduleTable::find("USMF-000000001");

       SubBillScheduleLine                 lineTable,billingScheduleLine;

       SubBillMassTerminationParmLine      parmLine,subBillMassTerminationParmLine;

       RecordInsertList                    parmLineList;

       SubBillMassTerminationParmUpdate    parmUpdate,massTerminationParmUpdate;

       parmId                              parmId,curParmId;

       SubBillCreditOption                 enumtype;

       SubBillTerminationType              termEnumType;

       SubBillScheduleLineTmp              subBillScheduleLineTmp;

 

 

       select firstonly lineTable where lineTable.SubBillScheduleStatus == SubBillScheduleStatus::Active

       && lineTable.SubBillBillingScheduleNumber == scheduleTable.SubBillBillingScheduleNumber;

 

       parmId = NumberSeq::newGetNum(CompanyInfo::numRefParmId()).num();

       SubBillMassTerminationParmUpdate::createFromParmId(parmId);

 

       ttsbegin;

       select forupdate parmUpdate where parmUpdate.ParmId == parmId;

       parmUpdate.SubBillOpenedFromHeader  = isScheduleHeader;

       parmUpdate.SubBillInvTransType      = scheduleTable.SubBillInvTransType;

       parmUpdate.SubBillTerminationType   = isScheduleHeader ? scheduleTable.SubBillTerminationType : lineTable.SubBillTerminationType;

       parmUpdate.SubBillIssueCredit       = (parmUpdate.SubBillTerminationType != SubBillTerminationType::NoAdjustment);

       parmUpdate.SubBillTerminationDate   = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());

       parmUpdate.SubBillTermReasonCode    = 'PRICE';

       parmUpdate.SubBillTermNotes         = '';

       parmUpdate.SubBillCreditOption      = str2Enum(enumtype,'Issue credit');

       parmUpdate.TransDate = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone());

       parmUpdate.update();

 

       parmLineList = new RecordInsertList(tableNum(SubBillMassTerminationParmLine));

      

 

       if (isScheduleHeader)

       {

           while select billingScheduleLine where billingScheduleLine.SubBillScheduleStatus == SubBillScheduleStatus::Active

           && billingScheduleLine.SubBillBillingScheduleNumber == scheduleTable.SubBillBillingScheduleNumber

           {

                parmLine.initializeParmLine(scheduleTable, billingScheduleLine, parmUpdate);

                parmLineList.add(parmLine);

           }

       }

       else

       {

           Container lineNumbers;

 

           parmLine.initializeParmLine(scheduleTable, lineTable, parmUpdate);

           parmLineList.add(parmLine);

           lineNumbers += lineTable.LineNum;

       }

 

       parmLineList.insertDatabase();

       ttscommit;

 

       massTerminationParmUpdate = SubBillMassTerminationParmUpdate::find(parmId);

       curParmId = parmId;

       ttsbegin;

       delete_from subBillScheduleLineTmp

       where subBillScheduleLineTmp.ParmId == curParmId;

 

       parmLineList = new RecordInsertList(tableNum(SubBillScheduleLineTmp), true, true, true, false, true, subBillScheduleLineTmp);

 

       while select * from subBillMassTerminationParmLine

       where subBillMassTerminationParmLine.ParmId == curParmId

       {

           subBillScheduleLineTmp.clear();

           subBillScheduleLineTmp.ParmId = curParmId;

           subBillScheduleLineTmp.ScheduleLnRecId = subBillMassTerminationParmLine.ScheduleLnRecId;

           //Only set refund amount if IssueCredit is true, otherwise, leave it 0

           if (massTerminationParmUpdate.SubBillIssueCredit)

           {

               subBillScheduleLineTmp.RefundAmount = subBillMassTerminationParmLine.RefundAmount;

           }

           subBillScheduleLineTmp.TotalLineAmount = subBillMassTerminationParmLine.TotalLineAmount;

           parmLineList.add(subBillScheduleLineTmp);

       }

       parmLineList.insertDatabase();

       ttscommit;

 

       // Using common code to get the count of the termination lines.

       int countLines = SubBillMassTermination::massTerminationProcess(massTerminationParmUpdate, subBillScheduleLineTmp);

       if (countLines == 1)

       {

           info("@SubBill:OneScheduleLineTerminated");

       }

       // Clear temp tables.

       ttsbegin;

       SubBillScheduleLineTmp subBillScheduleLineTmp1;

       delete_from subBillScheduleLineTmp1

       where subBillScheduleLineTmp1.ParmId == curParmId;

 

       SubBillMassTerminationParmLine subBillMassTerminationParmLine1;

       delete_from subBillMassTerminationParmLine1

       where subBillMassTerminationParmLine1.ParmId == curParmId;

 

       SubBillMassTerminationParmUpdate subBillMassTerminationParmUpdate;

       delete_from subBillMassTerminationParmUpdate

       where subBillMassTerminationParmUpdate.ParmId == curParmId;

       ttscommit;

   }

}


Output :