Sunday, June 26, 2011

Call webservice using SQL Server

Query:
How can I call an AIF service using SQL Server 2008?

Answer:
You can use http://technet.microsoft.com/en-us/sqlserver/ff686773.aspx to see how it is done.

Wednesday, June 22, 2011

Dynamics AX Run Time Dialog

Query:
Can I create a dialog on run time?

Answer:
Use the following class
///

///  This is a class to create dynamic dialog on the fly
///

///
/// Usage
/*
static void testDynamicDialog(Args _args)
{
  CustomDialog   diag;
  List           types = new List(Types::Integer); // typeId is an integer
  List           fields;
  ListEnumerator enum;
  DialogField    field;
  ;

  // the dialog shall contain fields for the following types
  types.addEnd(typeId(EmplId));
  types.addEnd(typeId(Itemid));
  types.addEnd(typeId(ProdId));
  types.addEnd(typeId(CustAccount));

  // crate and show
  diag = new CustomDialog(types,"Dynamic Dialog");
  diag.run();

  // show values in the Infolog
  fields = diag.parmFieldList();
  enum   = fields.getEnumerator();

  while(enum.moveNext())
  {
    field = enum.current();
    info(field.value());
  }
} */
///


class MSTACustomDialog extends Dialog
{
    List fieldList;
}


public List parmFieldList()
{
    return fieldList;
}

void new(List typeList, Caption _caption = '', Object _caller = null, str _parmstr = '', Form _form = new Form(formStr(Dialog)))
{
    ListEnumerator enum;
    ;

    if(typeList == null)
      error("@BCS3969");

    super(_caption, _caller, _parmStr, _form);

    fieldList = new List(Types::Class);
    enum      = typeList.getEnumerator();

    while(enum.moveNext())
    {
      fieldList.addEnd(this.addField(enum.current()));
    }
}

Monday, June 13, 2011

Reading CSV File in AX

Query:
How can I read a csv file in AX?

Answer:
use the following code,

public void run()
{
    #File
    CommaTextIo commaTextIo;
    FileIOPermission permission;
    container containFromRead;
    int x;
    int cols;
    PurchIdBase purchId;
    LineNum lineNum;
    ;
    super();
    if(!this.validate())
    {
        return;
    }


    try
    {
       
        permission = new FileIOPermission(filePath, #io_read);
        permission.assert();


        commaTextIo = new CommaTextIO(filePath, #io_read);
        containFromRead = commaTextIo.read();


        while(containFromRead)
        {
            cols = conLen(containFromRead);
            for(x=1; x <= cols; x++)
            {
                info(any2str(conpeek(containFromRead, x)));
            }
            containFromRead = commaTextIo.read();
        }
        commaTextIo = null;
    }
    catch(Exception::Error)
    {


    }
}

CommaTextIO not initialized error

Query:

I am getting commatextio not initialized error and it is not going away. I am sure my code is right.

Answer:

Make sure you are on the correct tier (client or server) and make sure you have permission on that file for the specific tier.

Hint: see the main and construct methods make sure they don't have only server keyword.

Wednesday, June 8, 2011

Passing arguments between two forms

Query:
How can I pass information from 1 form to another?

Answer:
IN the clicked button of form A write following code,
void clicked()
{
    Args args;
    FormRun formRun;
    ;
    super();

    args = new Args(formstr(FormName));
    args.parm(parameters);
    formRun = classFactory.FormRunClass(args);
    formRun.init();
    formRun.run();
    formRun.wait();
    formRun.detach();
    parenttable_ds.refresh(); // Refreshing parent table DataSourceTable
    parenttable_ds.executeQuery(); // Refreshing Parent DataSourceTable Query
}

IN form B init method write following code
args = element.args();
args.parm()

Sunday, June 5, 2011

Converting Real Number to TIme

public static str 5 num2Time(real _hours)
{

    int realPart;
    int decimalPart;
    real hours;
    str hoursStr;
    str minStr;
    int decimalLocation;
    str finalMinutes;
    ;
    try
    {
        hours = _hours;
        hoursStr = num2str(hours, 0, 2, 1, 0);
        decimalLocation = strfind(hoursStr, ".", 0, strlen(hoursStr));
        hoursStr = substr(hoursStr, 0, decimalLocation);
        hoursStr = trimString(hoursStr, ["."]);
        minStr = num2str(hours, 0, 2, 1, 0);
        minStr = substr(minStr, decimalLocation, strlen(minStr) - strlen(hoursStr));
        minStr = trimString(minStr, ["."]);
        realPart = str2int(hoursStr);
        decimalPart = str2int(minStr);
        finalMinutes = hoursStr + ":" + (int2str(60 * decimalPart / 100) == "0" ? "00" : int2str(60 * decimalPart / 100));
        return finalMinutes;
    }
    catch
    {
        return num2str(_hours, 0, 2, 1, 0);
    }
}

Wednesday, June 1, 2011

CLR Exception Catch

Query: How to catch CLR Exception?
Answer:  
catch (Exception::CLRError)
{
        info("Caught 'Exception::CLRError'.");
        netExcepn = CLRInterop::getLastException();
        info(netExcepn.ToString());
}