Friday, May 16, 2014

Basic lookups and referenced field lookups

In the dynamics ax 2012, there are different ways to fill the combo box/drop down list


How to create a simple lookup


The SysTableLookup class is provided by the standard application to allow programmers to easily create their own lookup forms, in code.
The basic steps to using this class are as follows:

  1. Create the sysTableLookup object
  2. Create the query to select the lookup data
  3. Add  the fields shown on the lookup
  4. Performs the lookup


client static void lookup<TableName> (FormStringControl _ctrl)
{
    SysTableLookup          sysTableLookup       =  SysTableLookup::newParameters(tableNum(<tableName>),_ctrl);
    Query                   query                = new Query();

    // create the query for the lookup
    QueryBuildDataSource    queryBuildDataSource = query.addDataSource(tableNum(<tableName>));

    // Add fields that will be shown in the lookup as columns        
    sysTableLookup.addLookupfield(fieldNum(<tableName>,<FeildName1>));
    sysTableLookup.addLookupfield(fieldNum(<tableName>,<FeildName2>));

    //Add the query to the lookup form
    sysTableLookup.parmQuery(query);

    // Perform the lookup
    sysTableLookup.performFormLookup();
}




How to create a simple lookup Reference
 

 The SysReferenceTableLookup class is used to construct lookup forms for reference controls.

  1. Create the SysReferenceTableLookup object
  2. Create the query which will be used to select the lookup data
  3. Add the fields which will be shown on the lookup
  4. Perform the lookup 
This method is now the standard method used to lookup the data for drop down when there is any modification needed to override the behavior of the functionality provided by the automatic lookup




public static client <tableName> lookup<tableName>(
    FormReferenceControl        _formReferenceControl)
{
    Query                   query;
    SysReferenceTableLookup referenceLookup;

    if (_formReferenceControl == null)
    {
        throw error(Error::missingParameter(null));
    }

    referenceLookup = SysReferenceTableLookup::newParameters(
        tableNum(<tableName>),
        _formReferenceControl,
        true);

    // create the query for the lookup form
     query.addDataSource(tableNum(<tableName>));

    // Add fields that will be shown in the lookup form as columns
    referenceLookup.addLookupfield(fieldNum(<tableName>,<FeildName1>));
    referenceLookup.addLookupfield(fieldNum(<tableName>,<FeildName2>));


    // Add the query to the lookup form
    referenceLookup.parmQuery(query);

    // Perform the lookup and return the selected record
    return referenceLookup.performFormLookup() as <tableName>;
}

1 comment:

  1. how to write code for multi selection for reference control

    ReplyDelete