Tuesday, September 29, 2020

Get selected records from grid in AX2012 using X++

 Hi guys, Today we see how to get selected records from the grid in Ax 2012 using X++.

write below code in the clicked method.





void clicked()

{

    CustomerTable  customerTableLoc;


    CustomerTable_DS.Anymarked();// check user selected the record or not.

    for (customerTableLoc= getFirstSelection(CustomerTable_DS);customerTableLoc;customerTableLoc=CustomerTable_DS.getNext())

    {

    if (customerTableLoc.recid)

    {

        info(customerTableLoc.CustAccount);

    }

  }

}

-------------------------------------------Or-------------------------------------------------

CustomerTable  customerTableLoc;

MultiSelectionHelper helper = MultiSelectionHelper::construct();


helper.parmDatasource(CustomerTable_DS);


customerTableLoc = helper.getFirst();


while (customerTableLoc.RecId)

{

    info(customerTableLoc.CustAccount);

    customerTableLoc = helper.getNext();

}


Result:


Keep Daxing!!

Post Button enabling or disabling for Journals in ax 2012 using X++

 Hi guys, Today we see how to disable post button in journals form in Ax 2012 using X++.


            In LedgerJournalTable form i want to disable the post button for particular user group persons.



  Go to LedgerJournalFormTable class in enableButtonsActive method write following code.

           while select userGroupList
                     where userGroupList.groupId == "Test Group"
                             && userGroupList.userId == curUserId()
                          {

                              ctrlPostJournalMenu.enabled(false);
                           }




Keep Daxing!!

Copying or Duplicating a record in ax 2012 using X++

 Hi guys, Today we see how to copy or duplicate a record in same table in ax 2012 using X++.

We can use Table standard "DATA" method. Check below code. We can copy one buffer data to other buffer data.

we can use buf2Buf(fromTableName , toTableName) also.

here I am using Data method.

static void copyData(Args _args)

{

    DAXChild        daxChild,daxChild1;

;

    select daxChild where daxChild.EmpId=='555';

    daxChild1.data(daxChild);

    daxChild1.insert();

}


Before Running the above Job.


Result:



Keep Daxing!!

Monday, September 28, 2020

Import data From Excel file to Ax 2012 using X++

 Hi guys ,Today we see How to Import data from Excel file to Ax 2012 using X++.


Write below code in job and Test it.

static void Job15(Args _args)

{

    Dialog                      _dialog;

    DialogField                 _file;

    SysExcelApplication         application;

    SysExcelWorkbooks           workbooks;

    SysExcelWorkbook            workbook;

    SysExcelWorksheets          worksheets;

    SysExcelWorksheet           worksheet;

    SysExcelCells               cells;

    COMVariantType              type;

    FileName                    filename;

    CustGroup                   custgroup; //Declaring Table Name

   

    CustGroupId                 _CustGroup;

    Name                         _Name;

    str                         _clearingPeriod;

    int                         row = 1;

    

    _dialog = new Dialog("Import Data From Excel");

    _dialog.addText("Select file:");


    _file   = _dialog.addField(ExtendedTypeStr("FilenameOpen"));

    _dialog.run();


    if (_dialog.closedOK())

    {


        info(_file.value());


        application = SysExcelApplication::construct();

        workbooks   = application.workbooks();

        //specify the file path that you want to read

        filename   =_file.value(); //ExcelSheet File Name

        try

        {

            workbooks.open(filename);

        }

        

        catch (Exception::Error)

        {

             throw error('File cannot be opened');

        }


        workbook    = workbooks.item(1);

        worksheets  = workbook.worksheets();

        worksheet   = worksheets.itemFromNum(1); //Here 1 is the worksheet Number

        cells       = worksheet.cells();

        do

        {


            row++;

            _CustGroup            = cells.item(row, 1).value().bStr();

            _Name                 = cells.item(row, 2).value().bStr();

            _ClearingPeriod       = cells.item(row, 3).value().bStr();


            select forUpdate custgroup where custgroup.CustGroup==_CustGroup;

            if (custgroup)

            {

                    ttsBegin;


                custgroup.Name             = _Name  ;

                custgroup.ClearingPeriod   = _clearingPeriod;


                custgroup.update();

                ttsCommit;

            }

        

            else

            {


                custgroup.Name             = _Name  ;

                custgroup.ClearingPeriod   = _clearingPeriod;


                custgroup.insert();

            }


            type = cells.item(row+1, 1).value().variantType();

        }


        while (type != COMVariantType::VT_EMPTY);

        application.quit();

        info("Data is Imported");

    }



Keep Daxing!!

Sunday, September 27, 2020

Enable or Disable the Unbonded Fields on Form in ax 2012 using X++

 

Hi guys ,Today we see how to enable Unbonded fields on form in ax 2012 using X++.

Here i write code in active method. Just check below example.


if(Table.Name== "Test")

{

    Table_ds.object(fieldNum(Table, Field)).enabled(true);

}  

------------Or-----------

Table_ds.object(fieldNum(Table, Field)).enabled(Table.Name== "Test");


Examples:

public int active()

{

int ret;

ret = super();

Table_ds.object(fieldNum(Table, Field)).enabled(Table.Name== "Test");

return ret;

}

Below example for Vendor Bank account number and name.


public int active()

{

    int ret;

    ret = super();

    VendBankAccount_ds.object(fieldNum(VendBankAccount, AccountNum)).enabled(VendBankAccount.WorkflowStatus==WorkflowStatus::Approved || VendBankAccount.WorkflowStatus==WorkflowStatus::NotSubmitted);

    VendBankAccount_ds.object(fieldNum(VendBankAccount, Name)).enabled(VendBankAccount.WorkflowStatus==WorkflowStatus::Approved || VendBankAccount.WorkflowStatus==WorkflowStatus::NotSubmitted);

    return ret;

}

Keep Daxing!!


Monday, September 14, 2020

Filtering drop down list based on another drop down on Dialog in Ax 2012 using X++

 Hi guys, Today we see how to Filtering drop down list based on another drop down.

In SysOperation we have to write code in 4 classes.

1.Contract class.

2.Controller class.

3.Service class.

4.UI builder class.


Contract class:

Mainly Contract class is for parameter Declaration Through Parm Methods.We set any Default values we implement with SysOperationInitializable. Before that we have to use SysOperationAlwaysInitializeAttribute.

For any validations we have to implement with SysOperationValidatable.


Class Declaration:

[DataContractAttribute,SysOperationAlwaysInitializeAttribute, SysOperationContractProcessingAttribute(classStr(CustDetailsUiBuilder))] public class CustDetailsContract implements SysOperationInitializable,SysOperationValidatable { CustGroupId custGroup; AccountNum accountNum; }

Parm Method1:

[DataMemberAttribute('AccountNum'), SysOperationLabelAttribute('AccountNum'), SysOperationHelpTextAttribute('AccountNum.')] public AccountNum parmAccountNum(AccountNum _accountNum= accountNum) { accountNum = _accountNum; return accountNum; }

Parm method 2:

[DataMemberAttribute('CustGroup'), SysOperationLabelAttribute('CustGroup'), SysOperationHelpTextAttribute('CustGroup.')] public CustGroupId parmCustGroup(CustGroupId _custGroup= custGroup) { custGroup = _custGroup; return custGroup; }

initialize method:

public void initialize() { custGroup='10'; accountNum=' '; }

validate method:

public boolean validate() { CustTable custTable; if(this.parmAccountNum()) { custTable=CustTable::find(this.parmAccountNum()); if (!custTable) { return checkFailed('Enter Valid accountNum'); } } else { return checkFailed('Please Enter accountNum'); } return true; }

UiBuilder Class:

    This class is mainly used for User interface Dialog changes. If we want add any lookup,enable or disable, add fields to group.


Class Declaration:

public class CustDetailsUiBuilder extends SysOperationAutomaticUIBuilder { DialogField dialogCustGroup; DialogField dialogAccountNum; CustDetailsContract contract; }

build method:

public void build() { Dialog dialogLocal = this.dialog(); contract = this.dataContractObject(); dialogLocal.addGroup('Customer Id'); this.addDialogField(methodStr(CustDetailsContract,parmAccountNum), contract); dialogLocal.addGroup('Customer Group'); this.addDialogField(methodStr(CustDetailsContract,parmCustGroup), contract); } Modified method: public boolean custGroupModified(FormStringControl _control) { dialogCustGroup.value(_control.valueStr()); return true; }

lookup method: public void lookupAccountNum(FormStringControl _control) { Query query = new Query(); SysTableLookup sysTablelookup; sysTablelookup =SysTableLookup::newParameters(tableNum(CustTable),_control); sysTablelookup.addLookupfield(fieldNum(CustTable,AccountNum)); sysTablelookup.addLookupfield(fieldnum(CustTable,Party)); query.addDataSource(tableNum(CustTable)); query.dataSourceTable(tableNum(CustTable)).addRange(fieldNum(CustTable, CustGroup)).value(dialogCustGroup.value()); sysTablelookup.parmQuery(query); sysTablelookup.performFormLookup(); }

lookup method: public void lookupCustGroup(FormStringControl _control) { SysTableLookup sysTablelookup; sysTablelookup =SysTableLookup::newParameters(tableNum(CustGroup),_control); sysTablelookup.addLookupfield(fieldNum(CustGroup,CustGroup)); sysTablelookup.addLookupfield(fieldnum(CustGroup,Name)); sysTablelookup.performFormLookup(); }

postBuild:

public void postBuild() { super(); dialogCustGroup = this.bindInfo().getDialogField(this.dataContractObject(), methodStr(CustDetailsContract,parmCustGroup)); //if (dialogCustGroup) //{ // dialogCustGroup.lookupButton(2); //} dialogCustGroup.registerOverrideMethod(methodStr(FormStringControl, lookup),methodStr(CustDetailsUiBuilder, lookupCustGroup), this); dialogCustGroup.registerOverrideMethod(methodStr(FormStringControl, modified),methodStr(CustDetailsUiBuilder, custGroupModified), this); dialogAccountNum = this.bindInfo().getDialogField(this.dataContractObject(), methodStr(CustDetailsContract,parmAccountNum)); dialogAccountNum.registerOverrideMethod(methodStr(FormStringControl, lookup),methodStr(CustDetailsUiBuilder, lookupAccountNum), this); //if (dialogAccountNum) //{ // dialogAccountNum.lookupButton(2); //} }

Controller class:

    Main logic We have to written in Controller class.

Class Declaration:

public class CustDetailsController extends SysOperationServiceController { }

new method: public void new() { super(); this.parmClassName(classStr(CustDetailsService)); this.parmMethodName(methodStr(CustDetailsService, process)); } main method: public static void main(Args args) { CustDetailsController controller; controller = new CustDetailsController(); controller.startOperation(); }

Service method:
                    We can write our own logic in Service class.

Class Declaration:

public class CustDetailsService extends SysOperationServiceBase { } process method: public void process(CustDetailsContract _contract) { CustTable custTable; AccountNum accountNum; CustGroupId custGroup; accountNum = _contract.parmAccountNum(); custGroup = _contract.parmCustGroup(); //custTable=CustTable::find(accountNum,true); select forupdate custTable where custTable.AccountNum == accountNum; ttsbegin; custTable.CustGroup=custGroup; custTable.doupdate(); ttscommit; info(strFmt('CustGroup - %1 is updated for %2-Customer',

                custTable.CustGroup, custTable.AccountNum)); }



OutPut:



Based on CustGroup Account number is filterd.





Keep Daxing!!

Tuesday, September 8, 2020

Find Customer Name and Address through Select statement Or Dynamic Query in AX 2012 using X++

 Hi guys ,Today we see how to find customer address and customer name using Select statement or Dynamic query in Ax 2012 using X++.

Select statement 1:


  LogisticsLocationRole   locationRole;

 DirPartyLocation        partyLocation;

DirPartyLocationRole    partyLocationRole;

LogisticsLocation       location;

LogisticsPostalAddress  postalAddress;


        select firstonly postalAddress

        exists join location

            where location.RecId == postalAddress.Location

        exists join locationRole

            where locationRole.Type  == LogisticsLocationRoleType::Invoice

        exists join partyLocation

            where

                partyLocation.Location == location.RecId &&

                partyLocation.Party == CustTable::find("DE-001").Party &&

                partyLocation.IsPrimary == NoYes::Yes

        exists join partyLocationRole

            where partyLocationRole.PartyLocation == partyLocation.RecId &&

                partyLocationRole.LocationRole == locationRole.RecId;


        info(strfmt("%1",postalAddress .address));


Select statement 2:

    CustTable                  custTable;

     DirPartyTable              dirPartyTable;

     LogisticsPostalAddress     logisticsPostalAddress;

     LogisticsLocation          logisticsLocation;

     LogisticsElectronicAddress logisticsElectronicAddress;


    select AccountNum from custTable

    where custTable.AccountNum =='US-004'

    join dirPartyTable

    where dirPartyTable.RecId == custTable.Party

    join logisticsPostalAddress

    where logisticsPostalAddress.Location==dirPartyTable.PrimaryAddressLocation

    join logisticsElectronicAddress

    where logisticsElectronicAddress.RecId== dirPartyTable.PrimaryContactEmail;

    info(strFmt('%1,%2,%3,%4',custTable.AccountNum,dirPartyTable.Name,logisticsElectronicAddress.Locator,logisticsPostalAddress.Address));



Using Dynamic Query:


Query query = new Query();

    QueryBuildDataSource qbds,qbds1,qbds2,qbds3,qbds4;

    QueryRun qrn;

    QueryBuildRange qbr;

    CustTable                     custTable;

    DirPartyTable                 dirPartyTable;

    LogisticsPostalAddress         logisticsPostalAddress;

    LogisticsElectronicAddress    logisticsElectronicAddress;

    DAXCustomerTable              customerTable;


    delete_from DAXCustomerTable;


    qbds = query.addDataSource(tableNum(CustTable));

    qbr = qbds.addRange(fieldNum(CustTable,AccountNum));

    qbr.value(CustomerAccount.valueStr());

    qbds1 = qbds.addDataSource(tableNum(DirPartyTable));

    qbds1.relations(false);

    qbds1.addLink(fieldNum(CustTable,party),fieldNum(DirPartyTable,RecId));


    qbds2 = qbds1.addDataSource(tableNum(logisticsPostalAddress));

    qbds2.relations(false);

    qbds2.addLink(fieldNum(logisticsPostalAddress,Location),fieldNum(DirPartyTable,PrimaryAddressLocation));


    qbds3 = qbds1.addDataSource(tableNum(logisticsElectronicAddress));

    qbds3.relations(false);

    qbds3.addLink(fieldNum(logisticsElectronicAddress,RecId),fieldNum(DirPartyTable,PrimaryContactEmail));


    qrn = new QueryRun(query);


    while (qrn.next())

    {

        custTable =qrn.get(tableNum(custTable));

        dirPartyTable=qrn.get(tableNum(DirPartyTable));

        logisticsPostalAddress=qrn.get(tableNum(LogisticsPostalAddress));

        logisticsElectronicAddress=qrn.get(tableNum(LogisticsElectronicAddress));

        DAXCustomerTable.CustomerAccountNum =custTable.AccountNum;

        DAXCustomerTable.CustomerName =dirPartyTable.Name;

        DAXCustomerTable.CustomerAddress=logisticsPostalAddress.Address;

        DAXCustomerTable.CustomerEmail=logisticsElectronicAddress.Locator;

        DAXCustomerTable.insert();

    }


In the above dynamic query i insert the data in my Custom table.


Keep Daxing!!


Wednesday, September 2, 2020

Filtering Records in a Form in AX 2012 using X++

 Hi guys,Today we see Filtering records in a Form in AX 2012 using X++.


Based on My Combo Box selection I have to filter the record in grid

On Form ClassDeclaration:

public class FormRun extends ObjectRun

{

        QueryBuildRange qr;

}


Apply the Range on Form DataSource Init():


public void init()

{

    super();

   qr=this.query().dataSourceName('InventSum').addRange(fieldNum(InventSum,Itemid));

}

On Form DataSource executeQuery():

   public void executeQuery()

{

      qr.value(Gender.valueStr());

      super();

}


IF its Enum Type Write Code on Selection change Method.

On ComboBox SelectionChange() Method Call Data Source Execute Query():

public int selectionChange()

{

    int ret;

    ret = super();

    InventSum_ds.executeQuery();

    return ret;

}


IF its String or int Type Write Code on modified Method.


public void modified()

{

    int ret;

    ret = super();

    InventSum_ds.executeQuery();

    return ret;

}


Keep Daxing!!