Home > Ax Technical > Using record templates in code for Dynamics Ax 4.0.

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('Feed'); //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 form
SysRecordTmpTemplate 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 method
public 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.
 
———– any template for codes where i can just get all the code that i want Wink

Advertisements
  1. Ashlesh Patel
    November 9, 2010 at 6:25 am

    Hi,

    I wanted to set the Default Template (i.e. Check the Default Checkbox on “sysRecordTemplateTable” form) from coding.

    Any advice?

    Thanks & Regards,
    /Ashlesh

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: