Monday, October 25, 2010

Using record templates in code for Dynamics Ax 4.0.

This article deals with defaulting values from record templates through code in Dynamics Ax 4.0.
 
Whenever you creat a new record in the Item form, a small form opens up showing up the templates. You can choose one of the templates from which you want to the basic values like "Item Group", "Dimension Group" to be copied.(Provided you have setup a template for that table).
 
This comes handy to create new records further as most of the value is drawn from the template record itself. You can harness this when you do it through code also :) ....  The following lines will throw light on how to do it.
 
1. Assume that you have the template name, then all that you need is to create a new record based on the template.
These three lines will do the job...
    sysRecordTemplate = SysRecordTemplate::newCommon(inventTable); 
    sysRecordTemplate.parmForceCompanyTemplate('WMSY01'); //Template name as string
    sysRecordTemplate.createRecord();
There are two kind of templates one common for the entire company and the other for specific user. In my example i have taken the company template, for the simple reason that it is valid across all accounts.

2. The above code pressumes that you might know the template name in prior. Sometimes you may want to give user a option of telling what template he wants to use. In that case we may need to create a template for him.
Here is the code that will enable the lookup and validate the selection. Bind these methods to the control where the user selects.
 
//Call this method in the init method of formSysRecordTmpTemplate VCTInitItemTemplates() {     Container               recordValues;     ;     recordValues = SysRecordTemplateTable::find(tablenum(inventTable)).Data;     recordValues =  condel(recordValues,1,1);     //tmp1 - Global variable for lookup        SysRecordTmpTemplate::insertContainer(tablenum(inventTable), tmp1, recordValues, SysRecordTemplateType::Company, true);     //tmp2 - Global variable for validation    SysRecordTmpTemplate::insertContainer(tablenum(inventTable), tmp2, recordValues, SysRecordTemplateType::Company, true);     return tmp; }
//Override the lookup methodpublic void lookup() {     SysTableLookup          sysTableLookup;     SysRecordTmpTemplate    tmp;     Container               recordValues;     ;     super();        sysTableLookup = SysTableLookup::newParameters(tablenum(SysRecordTmpTemplate), this);     //Add the fields to be shown in the lookup form     sysTableLookup.addLookupfield(fieldnum(SysRecordTmpTemplate, Description), true);     sysTableLookup.parmUseLookupValue(false);     sysTableLookup.parmTmpBuffer(tmp2);     // Perform lookup     sysTableLookup.performFormLookup(); }      
//Override the modified method
public boolean modified()
{
    boolean                 ret;
    SysRecordTmpTemplate    tmp;
    container               recordValues;
    ;

    ret = super();

    select firstonly tmp2 where tmp2.Description == this.valueStr();

    if (!tmp2 && this.valueStr())
    {
        this.text('');
        warning ('Invalid selection.');
        return false;
    }

    return ret;
}
 
This also helps you learn usage of temporary tables for lookup's. The following line enables lookups through temporary table.
 
sysTableLookup.parmTmpBuffer(tmp2);
 
Hope now you can implement a full fledged code that will create it's record from an pre exisiting template.
 
Regards,
/Ashlesh

Friday, October 1, 2010

Error in pack/unpack method while using LocalMacro.CurrentList and CurrentVersion

Hi,

I faced the problem while using dialog field in my class. I have declared a dialogfield variable for EDT but while assigning the value from the dialog it is not able to keep the assigned value throughout the class where it is being used.

I found some blogs that, by the use of Local Macro (CurrentVersion and CurrentList) you can resolve this issue. But the concept of CurrentVersion is much complicated with it.

Key Point: You have to manually increase the value of CurrentVersion declared as and when you add the dialogfield into the CurrentList macro.
By using above i can able to use the dialog field throughout the class.

Hope this helps to someone.

Regards,
/Ashlesh