Monday, June 25, 2018

Code to create a trade agreement in D365

 PriceDiscTable              priceDiscTable;
 PriceDiscAdmTrans           priceDiscAdmTrans;
 RecordInsertList            priceDiscAdmTransInsertList         = new   RecordInsertList(tableNum(PriceDiscAdmTrans), true);
 PriceDiscAdmTable           priceDiscAdmTable;
 PriceDiscAdmCheckPost       priceDiscAdmCheckPost = new priceDiscAdmCheckPost();     PriceDiscAdmName            PriceDiscAdmName;     

 select PriceDiscAdmName where PriceDiscAdmName.JournalName == "DISC";
 priceDiscAdmTable.clear();
 priceDiscAdmTable.JournalName     = PriceDiscAdmName.JournalName;                 priceDiscAdmTable.Name            = PriceDiscAdmName.Name;                 priceDiscAdmTable.DefaultRelation = PriceDiscAdmName.DefaultRelation;                 priceDiscAdmTable.insert();

priceDiscAdmTrans.clear();
priceDiscAdmTrans.JournalNum    = _priceDiscAdmTable.JournalNum;        priceDiscAdmTrans.ItemCode      = PriceDiscProductCodeType::Table;
priceDiscAdmTrans.ItemRelation  = _inventTable.ItemId;
currencyExchangeHelper = CurrencyExchangeHelper::newExchangeDate(Ledger::current(), systemDateGet());
amountMST = currencyExchangeHelper.calculateCurrencyToCurrency(_currencyCode,Ledger::accountingCurrency(CompanyInfo::current()),_purchasePrice ,true);
priceDiscAdmTrans.Amount        = amountMST;
priceDiscAdmTrans.FromDate      = today();
priceDiscAdmTrans.AccountCode   = PriceDiscPartyCodeType::GroupId;
priceDiscAdmTrans.relation      = PriceType::PricePurch;
priceDiscAdmTrans.AccountRelation   = "ItemGroupId";
priceDiscAdmTrans.Currency      = _currencyCode;
priceDiscAdmTrans.UnitId        = InventTablemodule::find(_inventTable.ItemId,ModuleInventPurchSales::Purch).UnitId;
priceDiscAdmTrans.PriceUnit     = 1;
priceDiscAdmTrans.InventDimId   = _inventTable.defaultInventDim(inventitemordersetuptype::Purch).inventDimId; priceDiscAdmTrans.doInsert();

Creating and revising the Project Budget from Project Forecasts in D365

With the following code we can able to import the project budget and revising too


        MenuItemName                actionName;
        MenuFunction                menuFunction;
        Args                        args = new args();
        IProjBudgetGridUpdatable    projBudgetGridUpdatable;
        ProjBudget                  projBudget,ProjBudgetWFStatus,projBudgetRec;
        ProjBudgetLine              projBudgetLine,PBL;
        projBudgetRevision          projBudgetRevision;
        projBudgetRevisionLine      projBudgetRevisionLine;
        ProjBudgetAllocationLine    projBudgetAllocationLine;
        ForecastModelId             defaultModelIdForForecastImport;
        FormDataSource              projBudget_ds,ProjBudgetLineRevenue_ds,ProjBudgetLineCost_ds;
        ProjBudgetImportBatch       projBudgetImportBatch = new ProjBudgetImportBatch();

       
        args = new Args();
        actionName = menuitemActionStr(projBudgetImportBatch);
   
        ttsbegin;
           
        projbudget::findOrCreateProjectBudget("HH");
        Select forupdate projBudget where projBudget.RootProjId == "HH";
        if(projBudget.BudgetWorkflowStatus == ProjBudgetWorkflowStatus::Created)
        {
           
            projBudgetImportBatch.parmProjId("HH");
            projBudgetImportBatch.parmProjBudgetRecId(projBudget.RecId);
            projBudgetImportBatch.parmSource("CurrentF");
            projBudgetImportBatch.parmMarkValue(0);
            projBudgetImportBatch.parmProjBudgetImportOptions(ProjBudgetImportOptions::Units);
            projBudgetImportBatch.parmProjBaseBudgetOn(projbaseBudgetOn::Forecast);
            projBudgetImportBatch.parmSummarizeByCategory(noyes::Yes);
            projBudgetImportBatch.parmInBatch(noyes::No);
            args.parmObject(projBudgetImportBatch);
            menuFunction = new MenuFunction(actionName, MenuItemType::Action);
            menuFunction.run(args);
            projbudget::updateProjBudgetStatus(projBudget.RecId,ProjBudgetWorkflowStatus::Submitted);
            projbudget::updateProjBudgetStatus(projBudget.RecId,ProjBudgetWorkflowStatus::Approved);
            args.record(projbudget);
            args.parmEnumType(enumnum(ProjBudgetManagerActionType));
            args.parmEnum(ProjBudgetManagerActionType::Commit);
            ProjBudgetManager::main(args);
           
        }
        projBudgetLine.clear();
        projBudgetRec.clear();
       

        // Budget revision
        if(projBudget.BudgetWorkflowStatus == ProjBudgetWorkflowStatus::Approved)
        {
            projBudgetRevision::createRevision(projBudget.RecId);
            select ProjBudgetRevision  where ProjBudgetRevision.ProjBudget == projBudget.RecId;
            projBudgetImportBatch.parmProjId("HH");
            projBudgetImportBatch.parmProjBudgetRecId(projBudget.RecId);
            projBudgetImportBatch.parmProjBudgetRevisionRecId(projBudgetRevision.RecId);
            projBudgetImportBatch.parmSource("CurrentF");
            projBudgetImportBatch.parmMarkValue(0);
            projBudgetImportBatch.parmProjBudgetImportOptions(ProjBudgetImportOptions::Units);
            projBudgetImportBatch.parmProjBaseBudgetOn(projbaseBudgetOn::Forecast);
            projBudgetImportBatch.parmSummarizeByCategory(noyes::Yes);
            projBudgetImportBatch.parmInBatch(noyes::No);
            args.parmObject(projBudgetImportBatch);
            menuFunction = new MenuFunction(actionName, MenuItemType::Action);
            menuFunction.run(args);
            ProjBudgetRevision::updateProjBudgetRevisionStatus(ProjBudgetRevision.RecId,ProjBudgetRevisionWFStatus::Submitted);
            ProjBudgetRevision::updateProjBudgetRevisionStatus(ProjBudgetRevision.RecId,ProjBudgetRevisionWFStatus::Approved);
            args.record(ProjBudgetRevision);
            ProjBudgetRevisionManager::main(args);
            while select forupdate PBL where PBL.ProjId == "HH"
            {
                PBL.UncommittedRevisions =0;
                PBL.update();
           
                while select projBudgetRevisionLine where projBudgetRevisionLine.ProjBudgetRevision == ProjBudgetRevision.RecId
                {
                    select forupdate projBudgetAllocationLine where projBudgetAllocationLine.ProjBudgetLine == PBL.RecId;
                    projBudgetAllocationLine.CommittedRevisions = projBudgetRevisionLine.RevisionAmount;
                    projBudgetAllocationLine.TotalAllocationAmount = projBudgetRevisionLine.NewTotalBudget;
                    projBudgetAllocationLine.update();
                }
            }
        }

Tuesday, June 19, 2018

How to get the TableBuffer from a class of a form in Pre/Post Event handler with chain of command

1. Create an extension class of the parent class

[ExtensionOf(classStr(AMRentTablePost))]
final public class AMRenatTablePost_Extension
{
    public List                RentList;
    public void initFromArgs(Args args)
    {
           
        ;
        Next initFromArgs(args);
        this.parmRentalList(rentalList);
    }

    public List parmRentalList(List _RentalList = RentList)
    {
        RentList = _RentalList;
        return RentList;
    }

}

2. Add that method name where they are getting the table buffer with Next command.
3. Create a parm method in that extension class to get the buffer.
4. In this example we are getting the table buffer in list, else it may be in any format..
5. Create a Pre/Post eventhandler  class to a method of parent table.
6. Get the class buffer like AMRentTablePost                 AMRentTablePost = args.getThis(); 
7. AMRentTable                 localRentTable,amRentTable;
     rentalList                         = AMRentTablePost.parmRentalList();//Add the new parm method of that extensions class
     ListEnumerator               rentals = rentalList.getEnumerator();     
     
     while (rentals.moveNext())
     {
            amRentTable = rentals.current();// Table buffer from the list
         
      }



Sunday, June 17, 2018

How to get the arguments value of method in Pre/Post EventHandler extensions

Here we have taken the Project Quotation Approval Workflow EventHandler class.

Class Name: PSAProjQuotationApprovalEventHandler
Method Name: Completed

 [PostHandlerFor(classStr(PSAProjQuotationApprovalEventHandler), methodStr(PSAProjQuotationApprovalEventHandler, completed))]
    public static void PSAProjQuotationApprovalEventHandler_Post_completed(XppPrePostArgs args)
    {
        WorkflowContext workflowContext;
        PSAProjQuotationApprovalEventHandler            PSAProjQuotationApprovalEventHandler = args.getThis();
        WorkflowElementEventArgs                        _workflowElementeventArgs = args.getArgNum(1);
        workflowContext                                 = _workflowElementEventArgs.parmWorkflowContext();
       
    }

From workflowContext we can write our business logic as we need...