Home

Wednesday, May 21, 2014

Definition of Maps in Ax

Maps can unify the access to similar columns and methods that are present in multiple tables.

Maps are elements that wrap table objects at run time. We can associate a map field with a field in one or more tables. This association enables to use the same field name to access fields with different names in different tables.

Map methods enable us to Create or Modify methods that act on the map fields

Wednesday, May 14, 2014

Dynamically insert Received quantity by Forward quantity in new line

1- Create three Edt with real data type
     -> S3ErrorQty
     ->  S3RecivedQty
     ->  S3ForwardQty

2 Create new table with name RejectionTable
with fields   S3ErrorQty,S3RecivedQty,S3ForwardQty

3- write a static method in tables's method node
   static int64 Returns()
{
    RejectionTable rejectionTable;
   ;
    select reverse firstOnly s3ForwardQty from RejectionTable;
    return rejectionTable.S3ForwardQty;
}

4- Create Form with data source RejectionTable

5 -write methods on form data source S3ErrorQty-modified
public void modified()
{
    super();
    RejectionTable.S3ForwardQty= RejectionTable.S3RecivedQty-RejectionTable.S3ErrorQty;
}
6-write methods on form data source S3RecivedQty-modified
 public void modified()
{
    super();
    RejectionTable.S3ForwardQty= RejectionTable.S3RecivedQty-RejectionTable.S3ErrorQty;
}
7-write create method in form's datasources-RejectionTable-Create
 public void create(boolean _append = false)
{
    super(_append);
    RejectionTable.S3RecivedQty =RejectionTable::Returns();
}



   
 

Calculator class in Ax

1-Create two Edt with real data type
     value1 & value2

2- Create Base Enum S3_Calculator with elements
   Addition,Mul,Sub,Div.

3- Write following class
public class S_Calculator extends RunBaseBatch
{
    DialogField dFvalue1,dFvalue2,dialogType;
    DialogGroup  dlgGrp;
    DialogEnumComboBox  dialogEnumComboBox;
    S3_Calculator    calloc;
    real value1,value2,total;
    str v1,v2;
    int Valueloc1;
    //#define.FieldNoTmp(600)
}

public real add()
{
    ;
    total =this.parmValue1() + this.parmValue2();
    info(strFmt("Add = %1", total));

    return total;

}
 Object dialog()
{
   DialogRunbase   dialog = super();
    ;
    dFvalue1 = dialog.addField(extendedTypeStr(Value1), "value 1");
    dFvalue2 = dialog.addField(extendedTypeStr(Value2), "value 2");

    dialogType = dialog.addField(enumStr(S3_Calculator), "Select the operation");

    return dialog;

}

public real divide()
{
    ;
    total =this.parmValue1() /  this.parmValue2();
    info(strFmt("Division : %1", total));
    return total;
}

public boolean getfromdialog()
{
    boolean ret;

    value1 = dFvalue1.value();
    value2 = dFvalue2.value();
    calloc = dialogType.value();
   /* if (dialogEnumComboBox)
    {
        S3_Calculator = dialogEnumComboBox.selection();
    }*/

    return super();
}

public real multiple()
{
  ;
    total =this.parmValue1() * this.parmValue2();
    info(strFmt("Multiply : %1", total));
    return total;
}

private real parmValue1()
{
    //value1 = dFvalue1.value();
    return value1;
}

private real parmValue2(real _value2 = value2)
{
    //value2 = dFvalue2.value();
    return value2;
}

public void run()
{
    //super();
    Dialog      dialog;
    DialogField  field;
   ;

    dialog = new Dialog("Calculator");
    dialog.addText("Select your values:");
    dFvalue1 = dialog.addField(extendedTypeStr(Value1), "Value 1");
    dFvalue2 = dialog.addField(extendedTypeStr(Value2),  "Value2");
    dialogType = dialog.addField(enumStr(S3_Calculator), "Select the operation");

    dialog.run();
    if (dialog.closedOk())
    {

        info(strFmt("value =  %1,value =  %2",dFvalue1.value(),dFvalue2.value()));
        value1 = dFvalue1.value();
        value2 = dFvalue2.value();
        calloc = dialogType.value();
        if(value1 == 0 && value2 == 0)
        {
            Error ("Please fill the values other than zero");
            break;
        }

        if(calloc == S3_Calculator::Addition)
        {
            this.add();
        }
        if(calloc == S3_Calculator::Mul)
        {
            this.multiple();
        }
        if(calloc == S3_Calculator::Sub)
        {
            this.subtract();
        }
        if(calloc == S3_Calculator::Div)
        {
            if( value2 != 0)
            {
                this.divide();
            }
            else
            {
                info(strFmt("You can not divide with %1",value2));
            }
        }
    }
    else
    {
        dialog.close();
    }

}

Private real subtract()
{
     ;
    total =this.parmValue1() - this.parmValue2();
    info(strFmt("subtract = %1", total));
    return total;

}

public static void main(Args args)
 {
    S_Calculator calc   = new S_Calculator();
     ;

    calc.run();

 }

Tuesday, May 6, 2014

Copy data in text file in ax

static void DataToTxtFile(Args _args)
{
    TextIO file;
    container line;
    InventTable invenTable;
    #define.filename('d:\\items.txt')
    #File
    ;
    file = new TextIO(#filename, #io_write);
    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
    file.outFieldDelimiter(';');// for semicolon seperator
    while select invenTable
    {
        line = [invenTable.ItemId,invenTable.ItemName,invenTable.ItemGroupId];
        file.writeExp(line);
    }
}

Thursday, April 3, 2014

Manipulating field properties on a form using X++

There are times when a developer wants to manipulate properties of fields on a form using X++; specifically properties such enabling or disabling visibility, editing, view, mandatory settings etc.  In my opinion you should do this if possible on the form data source using the data source function to control field and datasource controls
Below are examples of how to acheive this. I place a lot of my toggling code on buttons and the active method(s) of forms. In standard AX there are many examples of how to achieve this functionality so I will show standard and some of my own.
Standard – Standard using boolean method from the salesTableType class to determine visiblity
salesTable_ds.object(fieldnum(SalesTable, CustAccount) ).allowEdit(editSalesTableType.editCustAccount());
salesTable_ds.object(fieldnum(SalesTable, InvoiceAccount)).allowEdit(editSalesTableType.editInvoiceAccount());
salesTable_ds.object(fieldnum(SalesTable, vatNum) ).allowEdit(editSalesTableType.editVATNum());
salesTable_ds.object(fieldnum(SalesTable, InclTax) ).allowEdit(editSalesTableType.editInclTax());
 
My examples – I am basing access to buttons and fields on whether a sales order is invoiced and if the invoice has been paid for
 select firstonly custInvoiceJour where custInvoiceJour.SalesId == salesTable.SalesId;
 custTrans = custTrans::findFromInvoice(custInvoiceJour.InvoiceId);
 if(salesTable.SalesStatus == salesStatus::Invoiced)
 {
 CustInvoiceJournal.enabled(true);
 if(custTrans.open() == true)
 {
 wfsWalkupPayment.enabled(true);
 }
 else
 {

 wfsWalkupPayment.enabled(false);
 }

 wfsCSDeliveryNote.enabled(true);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryAddress)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryName)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryStreet)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryCity)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,CustAccount)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,wfsCSPhone)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,Email)).allowEdit(false);
 SalesTable_ds.object(fieldnum(SalesTable,CustomerRef)).allowEdit(false);
 }
 else
 {
 CustInvoiceJournal.enabled(false);
 wfsCSDeliveryNote.enabled(false);
 wfsWalkupPayment.enabled(true);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryAddress)).allowEdit(true);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryName)).allowEdit(true);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryStreet)).allowEdit(true);
 SalesTable_ds.object(fieldnum(SalesTable,DeliveryCity)).allowEdit(true);
 SalesTable_ds.object(fieldnum(SalesTable,CustAccount)).allowEdit(true);
 SalesTable_ds.object(fieldnum(SalesTable,Email)).allowEdit(true);
 SalesTable_ds.object(fieldnum(SalesTable,CustomerRef)).allowEdit(true);
 }

Email validation in ax

write  validateField in table methods-

NewEmail is Edt(String Type)

public boolean validateField(fieldId _fieldIdToCheck)
{
boolean ret;
str email;
;
ret = super(_fieldIdToCheck);

if (_fieldIdToCheck == fieldnum(TestQty, NewEmail))
{
email = this.NewEmail;

if ( strfind(email, "@", 1, strlen(email)) == 0
|| strfind(email, ".", 1, strlen(email)) == 0)
{
error("Must contain a '@' character and at least one '.'");
ret = false;
}

if ( strscan(email, ".@", 1,strlen(email)) > 0
|| strscan(email, "@.", 1,strlen(email)) > 0)
{
error("Cannot have the '@' and the '.' one after the other");
ret = false;
}
}

return ret;
}

Wednesday, April 2, 2014

Validate an enum in Dynamic AX

Validate an enum

 if(NetOccupied.valueStr() == enum2str(NetOccupied::Vacant))
        CreateReservation.enabled(true);
    else
        CreateReservation.enabled(false);