Monday, February 17, 2020

How to enable the dimension fields based on the Item selected on the form.

[Form]
public class KMTShipFromWarehouses extends FormRun
{
    InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;

    /// <summary>
    /// InventDimFormDesignUpdate
    /// </summary>
    /// <returns></returns>
    public InventDimCtrl_Frm_EditDimensions inventDimSetupObject()
    {
        return inventDimFormSetup;
    }

    /// <summary>
    /// InventDimFormDesignUpdate
    /// </summary>
    public void init()
    {
        super();
        // This method will be used to show default fields at form startup
        element.updateDesign(InventDimFormDesignUpdate::Init);
    }

    /// <summary>
    /// To Display product dimensions
    /// </summary>
    /// <param name = "mode">InventDimFormDesignUpdate</param>
    void updateDesign(InventDimFormDesignUpdate mode)
    {
        InventDimParm inventDimParmVisible;

        switch (mode)
        {
            // Form Init
            case InventDimFormDesignUpdate::Init:
                if(!inventDimFormSetup)
                {
                    inventDimFormSetup  = InventDimCtrl_Frm_EditDimensions::newFromForm(element);
                    inventDimFormSetup.parmSkipOnHandLookUp( true);

                    // Use the methods on InventDimParm
                    // to set which dimensions to show when form is initialized
                    inventdimparmvisible.InventSizeIdFlag       = true;
                    inventdimparmvisible.InventColorIdFlag      = true;
                    inventdimparmvisible.InventSiteIdFlag       = true;
                    inventdimparmvisible.InventLocationIdFlag   = true;
                    inventDimFormSetup.parmDimParmVisibleGrid(inventDimParmVisible);
                }
            // Datasource Active
            case InventDimFormDesignUpdate::Active  :
                inventDimFormSetup.formActiveSetup(InventDimGroupSetup::newItemId(KMTShipFromWarehouses.ItemId)); //InventDimDisplay is the datasource name.
                inventDimFormSetup.formSetControls( true);
                break;

            // Datasource Field change
            case InventDimFormDesignUpdate::FieldChange :
                inventDimFormSetup.formActiveSetup(InventDimGroupSetup::newItemId(KMTShipFromWarehouses.ItemId)); //InventDimDisplay is the datasource name.
                InventDim.clearNotSelectedDim(inventDimFormSetup.parmDimParmEnabled()); // InventDim is referring to datasource name
                inventdimparmvisible.InventSizeIdFlag       = true;
                inventdimparmvisible.InventColorIdFlag      = true;
                inventdimparmvisible.InventSiteIdFlag       = true;
                inventdimparmvisible.InventLocationIdFlag   = true;
                inventDimFormSetup.formSetControls( true);
                break;

            default :
                throw error(strFmt ("@SYS54195", funcName()));
        }
                   
    }

    [DataSource]
    class KMTShipFromWarehouses
    {
         /// <summary>
        /// To Display product dimensions
        /// </summary>
        public int active()
        {
            int ret;
            ret = super();
            element.updateDesign(InventDimFormDesignUpdate::Active);
           
            //If record is saved then allow edit for dimension field is false.
            if (KMTShipFromWarehouses_ds.cursor())
            {
                this.formRun().design(0).controlName('InventoryDimensionsGrid_InventSizeId').allowEdit(false);
                this.formRun().design(0).controlName('InventoryDimensionsGrid_InventColorId').allowEdit(false);
            }
            return ret;
        }

        [DataField]
        class ItemId
        {
            /// <summary>
            /// To Display product dimensions
            /// </summary>
            public void modified()
            {
                super();
   
                element.updateDesign(InventDimFormDesignUpdate::FieldChange);
                InventDim.clearNotSelectedDim(element.inventDimSetupObject().parmDimParmEnabled());
            }

        }

    }

    [DataSource]
    class InventDim
    {
        [DataField]
        class InventSizeId
        {
            /// <summary>
            /// To display site and warehouse
            /// </summary>
            public void modified()
            {
                InventTable                 inventTable = inventTable::find(KMTShipFromWarehouses.ItemId);

                InventItemOrderSetupType    setupType   = InventItemOrderSetupType::Invent;
               
                super();

                inventDim.InventSiteId      = inventTable.inventItemOrderSetupMap(setupType).inventSiteId(inventDim.InventSiteId, inventTable);

                inventDim.InventLocationId  = inventTable.inventItemOrderSetupMap(setupType,InventDim::findOrCreate(inventDim).InventDimId).inventLocationId(inventDim.InventLocationId,inventTable,inventDim.InventSiteId);
               
            }

        }

    }

}


Validate Write method on table

/// <summary>
    /// Validate the required fields
    /// </summary>
    /// <returns>Boolean</returns>
     public boolean validateWrite()
    {
        boolean ret = true;
       
        str errorMessage;

        InventTable     inventTable;
        InventDimParm   inventDimParm;
       
        inventTable   = InventTable::find(this.ItemId);
        inventDimParm =  InventDimParm::activeDimFlag(InventDimGroupSetup::newInventTable(inventTable));

        InventDim kmtInventDim = InventDim::find(this.InventDimId);

        if(inventDimParm.InventSizeIdFlag && !kmtInventDim.InventSizeId)
        {
            errorMessage = strFmt("@SYS26332", Fieldid2PName(tableNum(InventDim),fieldNum(InventDim, InventSizeId)));
        }

        if(inventDimParm.InventColorIdFlag && !kmtInventDim.InventColorId)
        {
            errorMessage = errorMessage + " " + strFmt("@SYS26332", Fieldid2PName(tableNum(InventDim),fieldNum(InventDim, InventColorId)));
        }

        if(kmtInventDim.InventSiteId && !kmtInventDim.InventLocationId)
        {
            errorMessage = errorMessage + " " + strFmt("@SYS26332", Fieldid2PName(tableNum(InventDim),fieldNum(InventDim, InventLocationId)));
        }

        if(errorMessage)
        {
            ret = checkFailed(errorMessage);
        }

        else
        {
            ret = super();
        }

        return ret;
    }

How to create InventTransfer Order using X++

static void TransferOrdersImport(Args _args)

#define.WarehouseFrom("AHK")
#define.WarehouseTo("SIMON")
#define.ShipDate("1/1/2019")
#define.ReceiveDate("1/1/2019)
#define.ItemId("ICTest")
#define.Qty(1)
#define.Site("HK")
#define.Warehouse("AHK")
InventTransferTable inventTransferTable;
InventTransferLine inventTransferLine;
InventDim inventDim;

try

ttsbegin;
//Order header inventTransferTable.clear();
inventTransferTable.initValue();
inventTransferTable.TransferId = InventTransferTable::numberSeq().num();
inventTransferTable.InventLocationIdFrom = #WarehouseFrom;
inventTransferTable.InventLocationIdTo = #WarehouseTo;
  inventTransferTable.DlvModeId = CustVendTransportPointLine::defaultDeliveryMode(inventTransferTable.InventLocationIdFrom,'','','','',inventTransferTable.InventLocationIdTo);
inventTransferTable.InventLocationIdTransit = InventLocation::find(inventTransferTable.InventLocationIdFrom).InventLocationIdTransit;

inventTransferTable.initFromAddress();
inventTransferTable.initToAddress();
inventTransferTable.ShipDate = str2Date(#ShipDate, 213);

inventTransferTable.ReceiveDate = str2Date(#ReceiveDate, 213);

if (inventTransferTable.validateWrite())
{
inventTransferTable.insert();
//Order line inventDim.clear();
  inventDim.InventSiteId = "HK";
inventDim.InventLocationId = "AHK";
inventTransferLine.clear();
inventTransferLine.initValue();
inventTransferLine.ItemId = #ItemId;
inventTransferLine.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;

inventTransferLine.QtyTransfer = #Qty;

inventTransferLine.initFromInventTableModule(InventTableModule::find(inventTransferLine.ItemId,ModuleInventPurchSales::Invent));
inventTransferLine.QtyRemainReceive = inventTransferLine.QtyTransfer;
inventTransferLine.QtyRemainShip = inventTransferLine.QtyTransfer; 
inventTransferLine.ShipDate = str2Date(#ShipDate, 213);
inventTransferLine.ReceiveDate = str2Date(#ReceiveDate, 213);
inventTransferLine.initFromInventTransferTable(inventTransferTable, false);
inventTransferLine.LineNum = InventTransferLine::lastLineNum(inventTransferLine.TransferId) + 1.0;
if (inventTransferLine.validateWrite())
{
inventTransferLine.insert();
}
  else
throw error("Error in Order line");
}
  else
throw error("Error in Order header");
ttscommit;
}
  catch {
  error("Error!");
return;
}
  info("Done!");
}

Thursday, January 9, 2020

How to display warehouse based on the Site selected using X++ in D365FO

[FormControlEventHandler(formControlStr(EcoResProductParameters, KMTWarehousePlaceholder), FormControlEventType::Lookup)]
    public static void KMTWarehousePlaceholder_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControlCancelableSuperEventArgs formControlCancelSuper = e as FormControlCancelableSuperEventArgs;

        EcoResProductParameters ecoResProductParameters = sender.formRun().dataSource(formDataSourceStr(EcoResProductParameters,EcoResProductParameters)).cursor();

        if (ecoResProductParameters.KMTSitePlaceholder)
        {
            InventLocation::lookupBySiteIdAllTypes(sender, ecoResProductParameters.KMTSitePlaceholder);
        }

        formControlCancelSuper.CancelSuperCall();
    }

Thursday, November 14, 2019

How to pass the argument from One form to another Form on Button click in D365FO X++

Form 1 on Button Click :
 [FormControlEventHandler(formControlStr(InventItemPrice, KMTItemPriceBreakdownForm), FormControlEventType::Clicked)]
    public static void KMTItemPriceBreakdownForm_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        Args            args = new Args();
        MenuFunction    menuFunction;
 
        InventItemPrice inventItemPrice = sender.formRun().dataSource("ItemPriceActivated").cursor();
        args.record(inventItemPrice);
        menuFunction = new MenuFunction(menuitemDisplayStr(KMTItemPriceBreakdownForm), MenuItemType::Display);
        menuFunction.run(args);



Form 2 on Init:
 [DataSource]
    class KMTInventItemPriceBrkdwn
    {
        public void init()
        {
            super();
            if(element.args().menuItemName() == menuItemDisplayStr(KMTItemPriceBreakdownForm))
            {
                 InventItemPrice kmtinventItemPrice = element.args().record();
                KMTInventItemPriceBrkdwn_ds.query().dataSourceTable(Tablenum(KMTInventItemPriceBrkdwn)).addRange(fieldNum(KMTInventItemPriceBrkdwn,ItemId)).
                    value(SysQuery::value(kmtinventItemPrice.ItemId));
            }
        }
}

Thursday, October 24, 2019

How to trigger the Workflow and update the workflow status using X++


WorkflowVersionTable        workflowVersionTable;


//Check whether we have a Active workflow on the table.
workflowVersionTable = Workflow::findWorkflowConfigToActivateForType(workFlowTypeStr(KMTSalesAgreementLineWFType),
                                                                             agreementLine.RecId,
                                                                             agreementLine.TableId);

        //If workflow is active then submit the workflow.
        if (agreementLine.RecId && workflowVersionTable.RecId)
        {
            //submitting to workflow
            Workflow::activateFromWorkflowType( workFlowTypeStr(KMTSalesAgreementLineWFType),
                                                agreementLine.RecId,
                                                "@KMT_Label:AutoSubmitWorkflow",
                                                false,
                                                curUserid());
           
            //Update the workflow status to Submitted on the table.
            salesAgreementHeader::updateWorkflowStatus(agreementLine.RecId,
                                                       KMTSalesAgreementWFStatus::Submitted,
                                                       NoYes::No);
        }

Tuesday, July 9, 2019

How to GET and SET SID in AX?

Whenever we switch back and forth from staging to production environment, we want to get the SID for the current user setting,

here are the steps you  can perform to scussfully logon to Dynamics AX:

1. Open the command prompt and type "wmic path win32_useraccount where name="{Domain user name}" get sid"
2. Copy the SID.
3. Open the SQL server and write following command to update current admin user:
             use {AX DB}
             update USERINFO
             set SID = '{paste the SID here from step#2}', NETWORKDOMAIN='{Network domain}',         NETWORKALIAS='{User Id}' where ID='Admin'

Thursday, June 13, 2019

How to mark Vendor open settlement transaction using x++ code in Ax 2012

How to mark Vendor open settlement transaction using x++ code in Ax 2012
public void settlementnew(InvoiceId   _invoice)
{
    custvendopentransmanager            manager;
    VendTransOpen                       vendTransOpen;
    VendTrans                           vendTrans;
    ExchangeRateHelper                  exchangeRateHelper;
    AmountCur                           totalSettlement;

    //To mark particular invoice based on Acc num
    select vendtransopen where vendTransOpen.AccountNum ==AccountNumb
                join vendTrans where vendTrans.Invoice == _invoice
                     && vendTrans.RecId == vendtransopen.RefRecId
                    && vendTrans.AccountNum == vendTransOpen.AccountNum;
 
    if(vendtransopen)
    {
        //To check transaction line which we want to settle
        manager = custvendopentransmanager::construct(ledgerJournalTrans);
        manager.updateTransMarked(vendTransOpen,true);
 
        //To get total settlement
        exchangeRateHelper = ExchangeRateHelper::newCurrency(Ledger::primaryLedger(CompanyInfo::findDataArea(ledgerJournalTrans.Company).RecId),ledgerJournalTrans.CurrencyCode);
        totalSettlement    = SpecTransManager::getTotalSettleAmountForSpecReference(
                                ledgerJournalTrans.Company,
                                ledgerJournalTrans.TableId,
                                ledgerJournalTrans.RecId,
                                ledgerJournalTrans.CurrencyCode,
                                ledgerJournalTrans.Company,
                                ledgerJournalTrans.TransDate,
                                exchangeRateHelper.prepareExchangeRateForStorage(ledgerJournalTrans.crossrate()));
        //To update in ledgerJournal trans
        ttsBegin;
        ledgerJournalTrans.selectForUpdate(true);
        ledgerJournalTrans.AmountCurDebit   = abs(totalSettlement);
        ledgerJournalTrans.SettleVoucher    = SettlementType::SelectedTransact;
        ledgerJournalTrans.update();
        ttsCommit;
    }

}

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// ...