Friday, 16 February 2018

How to create sales order using x++ code in Ax 2012

Creating sales order using x++ code 


List of tables and classes affected during sales order

    SalesTable salesTable;
    NumberSeq NumberSeq;
    SalesId sid;
    SalesLine sl;
    SalesFormLetter salesFormLetter;

Creating header details during sales order creation

    NumberSeq =NumberSeq::newGetNum(SalesParameters::numRefSalesId() , true);
    sid=NumberSeq.num();
    salesTable.SalesId = sid;
    salesTable.initValue();
    salesTable.CustAccount = "US-006";
    salesTable.CurrencyCode = "USD";
    salesTable.SalesGroup = "1";
    salesTable.initFromCustTable();
    salesTable.insert();

Adding line item in Sales line 

    sl.SalesId=sid;
    sl.ItemId="1000";
    sl.InventDimId="000458";
    sl.PriceUnit=100.00;
    sl.SalesQty=1;
    sl.CreateLine(NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes);

    info("Sales Order Created with Line");

While posting sales order 

    salesFormLetter=SalesFormLetter::construct(DocumentStatus::PackingSlip);
    salesFormLetter.update(SalesTable::find(sid));
    salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::All,AccountOrder::None,                NoYes::No,NoYes::Yes);
    info("Sales Order Status is Delivered");

While creating sales order invoice

    salesFormLetter=SalesFormLetter::construct(DocumentStatus::Invoice);
    salesFormLetter.update(SalesTable::find(sid));
    info(strFmt("%1 Sales Order Posted and Final Status is Invoiced",salesTable.SalesId));


Code: 

static void CreateSalesOrder(Args _args)

{

    //Developer : Rajaram E

    SalesTable salesTable;
    NumberSeq NumberSeq;
    SalesId sid;
    SalesLine sl;
    SalesFormLetter salesFormLetter;
    ;
    NumberSeq =NumberSeq::newGetNum(SalesParameters::numRefSalesId() , true);
    sid=NumberSeq.num();
    salesTable.SalesId = sid;
    salesTable.initValue();
    salesTable.CustAccount = "US-006";
    salesTable.CurrencyCode = "USD";
    salesTable.SalesGroup = "1";
    salesTable.initFromCustTable();
    salesTable.insert();

    //Create the Sales Line with the created Sales Order
    sl.SalesId=sid;
    sl.ItemId="1000";
    sl.InventDimId="000458";
    sl.PriceUnit=100.00;
    sl.SalesQty=1;
    sl.CreateLine(NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes,NoYes::Yes);

    info("Sales Order Created with Line");

    //Posting Sales Order
    salesFormLetter=SalesFormLetter::construct(DocumentStatus::PackingSlip);
    salesFormLetter.update(SalesTable::find(sid));
    salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::All,AccountOrder::None, NoYes::No,NoYes::Yes);
    info("Sales Order Status is Delivered");

     //Post the Sales Order Invoice
    salesFormLetter=SalesFormLetter::construct(DocumentStatus::Invoice);
    salesFormLetter.update(SalesTable::find(sid));
    info(strFmt("%1 Sales Order Posted and Final Status is Invoiced",salesTable.SalesId));

}

output:


Thursday, 15 February 2018

Create purchase order using x++ code in Ax 2012

Purchase order creation in Ax 2012 using x++

Declaration

    NumberSeq   numberSeq;
    PurchTable  purchTable;
    PurchLine   purchLine;

Getting Number sequence

    numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());
    numberSeq.used();
    purchTable.PurchId = numberSeq.num();


Find the vendor account

purchTable.initFromVendTable(VendTable::find('US-101'));

Validation 

    if (!purchTable.validateWrite())
    {
        throw Exception::Error;
    }

Adding line item and details

    purchLine.PurchId   = purchTable.PurchId;
    purchLine.ItemId    = '1000';

Create line details and checking validation  

purchLine.createLine(true, true, true, true, true, true);


Code

Creating open purchase order 

static void PurchaseOrderCreation(Args _args)
{
    NumberSeq   numberSeq;
    PurchTable  purchTable;
    PurchLine   purchLine;
    ;
    
    ttsBegin;
    // geting number sequence
    numberSeq = NumberSeq::newGetNum(PurchParameters::numRefPurchId());
    numberSeq.used();
    // Asign purch id
    purchTable.PurchId = numberSeq.num();
    // calling table initvalue method
    purchTable.initValue();
    // find the vendor account 
    purchTable.initFromVendTable(VendTable::find('US-101'));
    // validation
    if (!purchTable.validateWrite())
    {
        throw Exception::Error;
    }
    // insert data
    purchTable.insert();
    purchLine.PurchId   = purchTable.PurchId;
    purchLine.ItemId    = '1000';
    // Purch line creation
    purchLine.createLine(true, true, true, true, true, true);
    ttsCommit;
    info(strFmt("Purchase order '%1' has been created",purchTable.PurchId));
    
}

Tuesday, 13 February 2018

Simple dialog creation in Ax 2012

Simple dialog creating in Ax2012

Description: Creating simple dialog box in Ax2012. How to create dialog and get user input in the dialog

Code:

static void TestDailog(Args _args)
{
    // Author : Rajaram.E
    // Title  : Simple dialog creation  
    Dialog      dialog;
    DialogField dialogField;
    DialogGroup dialogGroup;
    ;
    // creating new dailog instance and  title
    dialog = new Dialog("Test dialog");
    // adding group title
    dialogGroup = dialog.addGroup("Test dialog group");
    // adding field it's must be EDT, if relations true means enable lookup
    dialogField = dialog.addField(extendedTypeStr(Name));
    // calling run method for build a dialog
    dialog.run();
    // option Ok or cancel
    if(dialog.closedOk())
    {
        // if click ok button they will get and display user value
        info(strFmt("%1",dialogField.value()));
    }   
}

Output:

Value get from user:


Lookup in dialog box :

Description: Add must EDT relation and set label, help text

Code:


static void TestDailog(Args _args)
{
    // Author : Rajaram.E
    // Title  : Simple dialog creation  
    Dialog      dialog;
    DialogField dialogField;
    DialogGroup dialogGroup;
    ;
    // creating new dailog instance and  title
    dialog = new Dialog("Customer info");
    // adding group title
    dialogGroup = dialog.addGroup("Customer");
    // adding field it's must be EDT and set label and help text
    dialogField = dialog.addField(extendedTypeStr(CustAccount),"Customer id","Customer account");
    // calling run method for build a dialog
    dialog.run();
    // option Ok or cancel
    if(dialog.closedOk())
    {
        // if click ok button they will get and display user value
        info(strFmt("%1",dialogField.value()));
    }   
}

Output:

Adding tabpage in the dialog :

Description: Adding multiple tab page in the dialog box 


Code:

static void TestDailog(Args _args)
{
    // Author : Rajaram.E
    // Title  : Simple dialog creation  
    Dialog      dialog;
    DialogField dialogField;
    DialogGroup dialogGroup;
    ;
    // creating new dailog instance and  title
    dialog = new Dialog("Customer info");
    // adding group title
    dialogGroup = dialog.addGroup("Customer");
    // adding field it's must be EDT and set label and help text
    dialogField = dialog.addField(extendedTypeStr(CustAccount),"Customer id","Customer account");
    
    dialog.addTabPage("Detail");
    dialogField = dialog.addField(extendedTypeStr(CurrencyCode));
    dialogField = dialog.addField(extendedTypeStr(CustPaymMode),"Paym mode");
    
    // calling run method for build a dialog
    dialog.run();
    // option Ok or cancel
    if(dialog.closedOk())
    {
        // if click ok button they will get and display user value
        info(strFmt("%1",dialogField.value()));
    }   
}


Output:




Dynamic query creation in Ax2012 using Join table

Dynamic query creation in Ax2012 using Join table

Sub: Dynamic query creation using multiple table

static void DynamicQuery(Args _args)
{

//Author : Rajaram E
//Title : Dynamic query creation using multiple join table

    Query                   query;
    QueryBuildDataSource    queryBuildDataSource, queryBuildDataSource1;
    QueryBuildRange         queryBuildRang;
    QueryRun                queryRun;
    SalesTable              salesTable;
    CustTable               custTable;
    ;


    query                   = new Query();//create new query
    queryBuildDataSource    = query.addDataSource(tableNum(CustTable));// adding datasource - table
    queryBuildRang          = queryBuildDataSource.addRange(fieldNum(CustTable,AccountNum));// adding range
    queryBuildRang.value("Cognizant");   // filter value or assign range value
    queryBuildDataSource1   = queryBuildDataSource.addDataSource(tableNum(SalesTable));// adding second datasource inside first datasource
    queryBuildDataSource1.relations(false);// set defualt relation false if needed
    queryBuildDataSource1.addLink(fieldNum(CustTable,AccountNum),fieldNum(SalesTable,custaccount));// create own new relation
    queryBuildDataSource1.joinMode(JoinMode::InnerJoin);// adding join mode

    queryRun                = new QueryRun(query);// calling query


    while(queryRun.next())
    {
       custTable = queryRun.get(tableNum(CustTable));
       salesTable = queryRun.get(tableNum(SalesTable));
       info(strFmt("%1 %2",custTable.AccountNum, salesTable.SalesId));
    }
}

Dynamic query creation in Ax2012

Dynamic query creation in Ax2012

Sub: Creating run time query in job

static void DynamicQuery(Args _args)
{
    
    // Author : Rajaram
    // Title : simple query creation  

    Query                               query;  
    QueryBuildDataSource   queryBuildDataSource;
    QueryBuildRange            queryBuildRang;
    QueryRun                        queryRun;
    SalesTable                       salesTable;
    ;


    query                   = new Query();// creating query instance 
    queryBuildDataSource    = query.addDataSource(tableNum(salesTable));// adding datasource in the query
    queryBuildRang          = queryBuildDataSource.addRange(fieldNum(salesTable,CustAccount));// set range 
    queryBuildRang.value("DE-001");// range value
    queryRun                = new QueryRun(query);// query run


    while(queryRun.next())
    {
       salesTable = queryRun.get(tableNum(salesTable));// geting records from table using query run 
       info(strFmt("%1 %2",salesTable.SalesId, salesTable.CustAccount));
    }
}

Lookup fields filter based on the parent lookup using UiBuilder.

Lookup filter in Ax 2012 R3

sub:Lookup fields filter based on the parent lookup using UiBuilder.

UiBuilder class:

class LookUpFilterUiBuilder extends SysOperationAutomaticUIBuilder
{

//Author : Rajaram E
//Title : Lookup fields filter based on the parent lookup using UiBuilder.

    DialogField                     dlgBusinessGroup;
    DialogField                     dlgBranch;
    UiBuilderContract           contract;
}

public void build()
{
    // build ui
    Dialog      dialogLocal = this.dialog();
    contract = this.dataContractObject();

    this.addDialogField(methodStr(UiBuilderContract,parmBusinessGroup), contract);
    this.addDialogField(methodStr(UiBuilderContract,parmBranch), contract);

}

public void businessGroupLookup(FormStringControl _formStringControl)
{
    Query query = new Query();// query
    SysTableLookup sysTablelookup;// lookup

    sysTableLookup = SysTableLookup::newParameters(tableNum(OMHierarchyType),_formStringControl);
    sysTableLookup.addLookupfield(fieldNum(OMHierarchyType,Name));

    query.addDataSource(tableNum(OMHierarchyType));// adding table in query

    sysTableLookup.ParmQuery(query);  // run query
    sysTableLookup.PerformFormLookup();// enable lookup

}

public boolean businessGroupModified(FormStringControl _control)
{
    // while modified to be related tavle should be clear
    dlgBusinessGroup.value(_control.valueStr());
    dlgBranch.value('');
    return true;
}

// branch method
public void organizationvaluelookup(FormStringControl _formStringControl)
{
    Query query = new Query();
    SysTableLookup               sysTablelookup;
    QueryBuildDataSource    Qbds, Qbds1, Qbds2;
    QueryBuildRange             queryBuildRange;
    ;
    sysTableLookup = SysTableLookup::newParameters(tableNum(OMOperatingUnit),_formStringControl);// lookup table and fields
    sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit,OMOperatingUnitNumber),true);// this is the parameter field
    sysTableLookup.addLookupfield(fieldNum(OMOperatingUnit,Name));// display name

    Qbds = query.addDataSource(tableNum(OMOperatingUnit));// adding first table in the query
    Qbds1=Qbds.addDataSource(tableNum(OMHierarchyRelationship));//adding second datasource inside first datasource
    Qbds1.joinMode(JoinMode::InnerJoin);// join
    Qbds1.addLink(fieldNum(OMOperatingUnit, RecId), fieldNum(OMHierarchyRelationship, ChildOrganization));// relationship for both table

    Qbds2=Qbds1.addDataSource(tableNum(OMHierarchyType));// adding thired table
    Qbds2.joinMode(JoinMode::InnerJoin);// join mode
    Qbds2.addLink(fieldNum(OMHierarchyType,RecId), fieldNum(OMHierarchyRelationship,HierarchyType));// relationship
    Qbds2.addRange(fieldNum(OMHierarchyType,Name)).value(dlgBusinessGroup.value());//adding range value from first lookup based

    sysTableLookup.ParmQuery(query);
    sysTableLookup.PerformFormLookup();

}


public void getFromDialog()
{
    contract = this.dataContractObject();
    super();
}


public void postRun()
{
    super();
}

public void initializeFields()
{
    contract = this.dataContractObject();
}


public void postBuild()
{
    super();
    // business group
    dlgBusinessGroup = this.bindInfo().getDialogField(
                         this.dataContractObject(),
                         methodStr(UiBuilderContract, parmBusinessGroup));

    if (dlgBusinessGroup)
    {
        dlgBusinessGroup.lookupButton(2);
    }
    dlgBusinessGroup.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(LookUpFilterUiBuilder, businessGroupLookUp), this);
    dlgBusinessGroup.registerOverrideMethod(methodStr(FormStringControl, modified), methodStr(LookUpFilterUiBuilder, businessGroupModified), this);

    // branch
    dlgBranch = this.bindInfo().getDialogField(
                         this.dataContractObject(),
                         methodStr(UiBuilderContract,parmBranch));

    dlgBranch.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(LookUpFilterUiBuilder, organizationvaluelookup), this);

    if (dlgBranch)
    {
        dlgBranch.lookupButton(2);
    }


}





Step-by-Step Guide to Restore a SQL BACPAC File - Microsoft dynamics D365 Fin & Ops

 Restore steps for bacpac file in to SQL server - Microsoft dynamics D365 Fin & Ops. Log in to LCS and navigate to the asset library. On...