web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Apply product template from code in Ax2012

(0) ShareShare
ReportReport
Posted on by 125

Hi,

I'm implementing a product management in Ax2012 through AIF services. I'm able to create and release products successfully. However, I also need to be able to apply a product template to a released product from the code. I have implemented the template related logic in the server side using x++. The logic is based on the below examples I found ( http://krishhdax.blogspot.fi/2012/06/ax2012-apply-template-for-product-using.html and https://community.dynamics.com/ax/f/33/p/43584/79151.aspx ). In the first example the template is applied to an InventTable using EcoResProductTemplateApplyManagerUtils. However the problem I'm facing right now is that if I try to create an instance of the manager by calling theEcoResProductTemplateApplyManagerUtils::newDefault() the call fails and I get an error: "Exception of type 'Microsoft.Dynamics.Ax.Xpp.InvalidRemoteCallException' was thrown.". The creation fails even if it's the only line of code I try to execute. Is this some known issue and is there a way to get around this somehow, or is there some other - perhaps completely different - way of applying a template to a released product?

Below is the code I have so far:

public void ApplyTemplate( System.String _productnumber, System.String _templatename )
{
  InventTable         inventTable;
  container           recordTmpValues;
  EcoResProductTemplateApplyManagerUtils utils;

  inventTable = InventTable::Find( _productnumber );
  recordTmpValues = this.getTemplateData( _templatename );
  utils = EcoResProductTemplateApplyManagerUtils::newDefault(); // --> this call fails!                    
  utils.applyTemplate( recordTmpValues, inventTable.ItemId );
}


// private method for retrieving the template data container
private container getTemplateData(System.String _templatename)
{
    SysRecordTemplateTable  templateTable;
    SysRecordTmpTemplate  tmp;
    SysRecordTemplateData tmpdata;
    container  dataContainer,RecordTmplateData;
    int i;
    boolean DefaultRecord;
            
    templateTable = SysRecordTemplateTable::find( 175 ); // pass invent table id
    dataContainer = templateTable.Data;
    
    for(i=2; i<conlen(dataContainer); i++) // 1st element of template data is version, so start from 2
    {
       [tmp.Description, DefaultRecord, tmp.Data, tmp.Details] = conpeek(dataContainer, i);

       if(tmp.Description == _templatename )
       {
         tmp.OrgDescription=tmp.Description;
         tmp.insert();
         RecordTmplateData=tmp.data;
       }
    }

    return RecordTmplateData;
}


Any help is appreciated, thanks a lot!


Kind regards,

Kaj Gustafsson

*This post is locked for comments

I have the same question (1)
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    What exactly is the failing call? I guess it's EcoResProductTemplateManager::construct(), because EcoResProductTemplateManager class is bound to client and AIF services don't have any AX client; they run directly on AOS.

    You will have to change the code not to do any client calls.

  • Kaj Gustafsson Profile Picture
    125 on at

    Hi Martin,

    thanks for the reply. Ok, I understand, so I cannot call the EcoResProductTemplateApplyManagerUtils since it's designed for client side use, but is there an alternative I could use in this case? I basically have the record template data container that I need to pass to an released item, but what api call could I use for that? Could you provide some small example or even some keywords i could look into so I could get forward with this. I'm totally new to Ax programming, and it seems quite hard to find coding related documentation especially regarding the templates. Thanks a lot!

    BR,

    Kaj

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    You'll find that the logic you're trying to use is tightly coupled with GUI. For instance, applyTemplate() iterates through datasources of EcoResProductDetailsExtended form (as returned from EcoResProductTemplateManager.dataSourcesNameList()).

    If EcoResProductTemplate* classes contain a lot of logic you want to reuse, you'll probably have to carefully call only what you need and factor business logic out of client-bound classes. If there is not much logic to reuse, call SysRecordTemplate class directly. Something like this:

    template = SysRecordTemplateFactory::construct().newRecordTemplateFromCommon(inventTable);
    template.parmRecordTemplateValues(…);
    template.createRecord();
  • Kaj Gustafsson Profile Picture
    125 on at

    Hi Martin,

    thanks, I was actually able to apply the template successfully using the direct method you described above. However, I did a bit of investigation and I noticed that the only way I was able to make it work was to run the code in the client side, by creating a test main x++ method and then debugging the logic through from there. But still if I make a separate method that contains the exact same logic, and then try to call it from C# through the AIF service, the method seems to run successfully (by validating all the calls), but in the end the template data doesn't appear to the product in Ax.

    So could it be that even directly accessing the SysRecordTemplate there's still some logic used underneath that is not supported by AOS. I wonder is it even possible to go any deeper than using the SysRecordTemplate class in this case? It just doesn't sound believable that it wouldn't be possible to apply a template through a service call.

    BR,

    Kaj

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    I don't believe that there is anything "not supported by AOS" - either the logic you want is not called or the result is later overwritten by some other logic. Use the debugger to see what's happening there.

  • Ashish langhnoja Profile Picture
    80 on at

    Hi Kaj

    Can you please provide code example what you have done to solve this.

    It will be a great help

  • Kaj Gustafsson Profile Picture
    125 on at

    Hi Ashish

    so the final issue in my case was that after applying the template the data didn't seem to appear in the target product record. So what I had to do - that also seemed to solve the issue - was to select the target product and corresponding inventTable record for update state and then make the call for setting the template using the "parmForceCompanyTemplate", and right after that commit the changes. At least this way it worked for me. Code example as follows:

    EcoResProduct ecoResProduct;

    EcoResProduct ecoResProductTmp;

    SysRecordTemplate template;

    InventTable         invent;

    boolean    ret;

    str templatename = _templatename;

    str productname = _productname;

    ;

    try

    {

           ecoResProductTmp = ecoResProduct::findByDisplayProductNumber( productname );

           ttsBegin;

           select firstOnly forUpdate ecoResProduct where ecoResProduct.RecId == ecoResProductTmp.RecId;

           select firstOnly forUpdate invent where invent.Product == ecoResProduct.RecId;

           template = SysRecordTemplateFactory::construct().newRecordTemplateFromCommon(invent);

           template.parmForceCompanyTemplate(templatename);

           ret = template.createRecord();

           invent.write();

           ecoResProduct.write();

           ttsCommit;

    }

    catch

    {

           Global::exceptionTextFallThrough();

           return false;

    }

    BR

    Kaj

  • kartikkp7 Profile Picture
    365 on at

    Hi,

    How can i create a product from template using x++...?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans