Thursday, 13 April 2017

Query Range more than one Value in x++ Query

static void queryMultipleValue(Args _args)
{
    Query                   query;
    QueryRun                queryrun;
    QueryBuildDataSource    qbds;
    QueryBuildRange         qbr, qbr1;
    SalesTable              salesTable;
    str                     rangevalue;
    ;

    query = new Query();

    qbds = query.addDataSource(tableNum(SalesTable));
    qbds.addSortField(fieldNum(SalesTable, CustAccount), SortOrder::Ascending);
    qbr     = qbds.addRange(fieldNum(SalesTable, CustAccount));

   
    qbr.value(strFmt("%1,%2,%3", queryValue("US-4000"), queryValue("US-027"), queryValue("US-028")));
 

    queryrun = new QueryRun(query);


    info(strFmt(queryrun.toString()));
    while(queryrun.next())
    {
        salesTable = queryrun.get(tableNum(SalesTable));

        info(strFmt("%1", salesTable.CustAccount));
    }
}

Tuesday, 11 April 2017

Simple x++ query with enum value range

static void queryEnumValue(Args _args)
{
    Query                   query;
    QueryRun                queryrun;
    QueryBuildDataSource    qbds;
    QueryBuildRange         qbr;
    PurchTable              purchTable;
    ;
   
    query = new Query();
   
    qbds = query.addDataSource(tableNum(PurchTable));
    qbds.addSortField(fieldNum(PurchTable, PurchId), SortOrder::Ascending);
    qbr     = qbds.addRange(fieldNum(PurchTable, PurchStatus));
   
    qbr.value(enum2str(PurchStatus::Received));
   
    queryrun = new QueryRun(query);
   
    while(queryrun.next())
    {
        purchTable = queryrun.get(tableNum(PurchTable));
       
        info(strFmt("%1", purchTable.PurchId));
    }
}

Date Range filter in x++ query

static void QueryDateRange(Args _args)
{
    Query                   query;
    QueryRun                queryrun;
    QueryBuildDataSource    qbds;
    QueryBuildRange         qbr;
    PurchLine               purchLine;
    TransDate               fromDate, Todate;
    ;
   
    fromDate = mkDate(19,11,2016);
    Todate   = mkDate(20,12,2016);
   
    query = new Query();
   
    qbds = query.addDataSource(tableNum(PurchLine));
   
    qbds.addSortField(fieldNum(PurchTable, PurchId), SortOrder::Ascending);
   
    qbr     = qbds.addRange(fieldNum(PurchLine, deliveryDate));
   
    qbr.value(queryRange(fromDate, Todate));
   
    queryrun = new QueryRun(query);
   
    while(queryrun.next())
    {
        purchLine = queryrun.get(tableNum(purchLine));
       
        info(strFmt("%1", purchLine.PurchId));
    }
}

Wednesday, 15 March 2017

Simple Custom Lookup Ax 2012 R3

In this post we'll see how easily we can create a custom lookup.

Suppose I have two fields 
1. Country/Region
2. StateId

Now I want to create a lookup so that If I will select any country then state lookup must show states which belongs to my selected country

for example If I have selected India in country field then it must show only indian state in state field

Lets see how can we achieve it.

Step 1: Create a table and then create two fields
           1. CountryRegionId
           2. StateId
Step 2: Create a form
Step 3: For custom lookup we will use State master table i.e. LogisticsAddressState
here we can see there is a method lookupStateId

public client static void lookupStateId(
    FormStringControl               _ctrl,
    LogisticsAddressCountryRegionId _countryRegionId)
{
    SysTableLookup       sysTableLookup = SysTableLookup::newParameters(tableNum(LogisticsAddressState), _ctrl);
    Query                query = new Query();
    QueryBuildDataSource queryBuildDataSource;

    sysTableLookup.addLookupfield(fieldNum(LogisticsAddressState, StateId));

    // <GEERU>
    if (isCountryRegionRU(_countryRegionId))
    {
        sysTableLookup.addLookupMethod(tableMethodStr(LogisticsAddressState, fullName_RU));
        sysTableLookup.addLookupMethod(tableMethodStr(LogisticsAddressState, propertiesZipCode_RU));
        sysTableLookup.addLookupMethod(tableMethodStr(LogisticsAddressState, propertiesGniCode_RU));
    }
    else
    {
    // </GEERU>
        sysTableLookup.addLookupfield(fieldNum(LogisticsAddressState, CountryRegionId));
        sysTableLookup.addLookupfield(fieldNum(LogisticsAddressState, Name));
    // <GEERU>
    }
    // </GEERU>

    queryBuildDataSource = query.addDataSource(tableNum(LogisticsAddressState));
    queryBuildDataSource.addRange(fieldNum(LogisticsAddressState, CountryRegionId)).value(queryValue(_countryRegionId));

    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}

Step 4 : Go to Design and select CountryRegionId control then go to properties and set AutoDeclaration property "YES"

Step 5:

Now override lookup method on Form > Datasource > Fields >StateId > methods > lookup

and customize lookup method

public void lookup(FormControl _formControl, str _filterStr)
{
    
    LogisticsAddressState::lookupStateId(_formControl, S3_countryStateLookup_CountryRegionId.valueStr());
}

Step 5: Now select India in country field and then you can see lookup it will show only Indian state 


Get Current Record for different data source x++

By this we can use same MenuItem at different place and for different data source.

---------------------------------------------------------------------------
Public static void main(Args _args)
{
    RetailStatementTable    retailStatementTable;
    RetailStatementLine     retailStatementLine;
    RetailStatementId         StatementID;
    RetailCountedAmountUpdate  RetailCountedAmountUpdate = new RetailCountedAmountUpdate();
    ;

     switch (_args.dataset())
    {
            case tablenum(RetailStatementTable) :
            retailStatementTable =  _args.record();
            StatementID = retailStatementTable.statementId;
            break;
           
            case tablenum(RetailStatementLine) :
            retailStatementLine =  _args.record();
            StatementID = retailStatementLine.statementId;
            break;
    }
 

    RetailCountedAmountUpdate.run(StatementID);

}