Greg On Dynamics Ax

Random thoughts about development using Dynamics AX and .Net

Posts Tagged ‘Axapta

Refactoring a long parameter list of boolean flags

with 3 comments

If you have had the need to have a method accept many boolean arguments, you might design the method to accept a number of optional boolean parameters, such as:

public void print(boolean _proforma = false, boolean _hideFooter = false, boolean _hideHeader= false, boolean _archive = false)

This is not the best design. For a start if you are happy to accept all the defaults but want to archive the printout you’ll need to call the method like this:

print(false, false, false, true);

It also suffers from what refactoring practitioners call a “bad smell” – this one being a long parameter list, which makes it harder to read and understand, and gets worse over time as new arguments are added.

A refactoring to improve the design might be to introduce a (single) settings object to replace the parameters.
If you consider that to be overkill you may want to consider this alternative: replace the parameters with a single base Enum type:

Each enumeration element then has a value that maps to the two base binary. So the values for this base enum would be:

  • Proforma – 1
  • HideFooter – 2
  • HideHeader – 4
  • Archive – 8


This enables us to declare our print method like so:

public void print(DEV_PrintOptions _opts)
{
if ((_opts & DEV_PrintOptions::Proforma) == DEV_PrintOptions::Proforma)
// Do proforma stuff

if ((_opts & DEV_PrintOptions::HideFooter) == DEV_PrintOptions::HideFooter)
// Hide the footer

if ((_opts & DEV_PrintOptions::HideHeader) == DEV_PrintOptions::HideHeader)
// Hide the header

if ((_opts & DEV_PrintOptions::Archive) == DEV_PrintOptions::Archive)
// Archive the printout
}

Which allows the method to be called in a more flexible way, specifying only the options you wish to override, for example:

// print out proforma and archive it (accepting the default to print the footer and header):
print(Dev_PrintOptions::Proforma | Dev_PrintOptions::Archive);

Written by gregondax

February 26, 2010 at 9:15 am

Solving an ‘Object object has not been initialized’ runtime error

leave a comment »

If you have received the error below, and are scratching your head over the cause:

Object object not initialised error

Then the answer is to check you have called super() in your constructor. The compilation output will also display a warning to that effect:

Compile warning for not overriding super in constructor

Related notes about extending Object and initialization

Unlike C#, Object is not implicitly extended when creating a new class. You’ll need to ensure that the ‘extends object’ class declaration is included if you want to call methods declared in Object, eg:

extendsObject

Repeating the aforementioned error (and also unlike C#) the base constructor is not implicitly called in X++:

void new()
{
;
super(); //base constructor is not called implicitly
}

Written by gregondax

November 23, 2009 at 9:30 am

AIF Pipeline Example

with 4 comments

In my last post (Top 5 AIF Development Tips and Tricks) I mentioned how you can use the AIF to transform external messages into the format that AX expects. This post elaborates upon that tip with a more detailed example.

Suppose you want to import currency exchange rates.

AX has an action for this out of the box – createListExchangeRates, and will happily accept a message similar to this:

<ExchangeRates xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/ExchangeRates">
    <DocPurpose>Original>/DocPurpose>
    <ExchRates>
        <CurrencyCode>GBP</CurrencyCode>
        <ExchRate>1.71</ExchRate>
        <FromDate>2009-09-21<FromDate>
        <ToDate>2009-09-22</ToDate>
    </ExchRates>
</ExchangeRates>

Thats all well and good if you have control over the schemas of any inbound messages.
Most likely this is not the case, say for example you want to import the exchange rates for the euro from the european central banks daily published list (http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml).

If you try and receive this it will fail on two counts:
1) It is not wrapped in an envelope (which is used by the AIF to identify the message sender and destiation) and
2) the format of the exchange rate does not match AX schema.

To solve problem 1) you can make use of the ‘Wrap XML in AIF envlope’ program available from axaptapedia here:www.axaptapedia.com/AIFEnvelopeTool

To solve 2) you can use the pipeline to specify an XSLT to transform the message, the XSLT to perform the transformation for this example is:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:ax="http://schemas.microsoft.com/dynamics/2006/02/documents/ExchangeRates" 
  xmlns:ext="http://www.ecb.int/vocabulary/2002-08-01/eurofxref" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" 
  exclude-result-prefixes="ext xs xsi xsl gesmes" 
  xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/ExchangeRates">    
  <xsl:namespace-alias stylesheet-prefix="ax" result-prefix="#default" />
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:template match="gesmes:Envelope">
      <ax:ExchangeRates>          
          <xsl:variable name="root" select="." />          
          <xsl:for-each select="$root/ext:Cube/ext:Cube">
            <xsl:variable name="xrateDate" select="$root/ext:Cube/ext:Cube/@time" />
              <xsl:for-each select="$root/ext:Cube/ext:Cube/ext:Cube">
              <ax:ExchRates>
                <xsl:attribute name="class">
                  <xsl:value-of select="'entity'" />
                </xsl:attribute>
                <ax:CurrencyCode>
                  <xsl:value-of select="@currency" />
                </ax:CurrencyCode>
                <ax:ExchRate>
                  <xsl:value-of select="@rate" />
                </ax:ExchRate>
                <ax:FromDate>
                  <xsl:value-of select="$xrateDate" />
                </ax:FromDate>
                <ax:ToDate>
                  <xsl:value-of select="$xrateDate" />
                </ax:ToDate>
              </ax:ExchRates>
            </xsl:for-each>
          </xsl:for-each>        
      </ax:ExchangeRates>         
    </xsl:template>  
</xsl:stylesheet>

After running the AIF inbound service upon last weeks exchange rates it results in the following data being created (US Dollar highlighted):

Exchange rates AX form

Exchange rates AX form

Written by gregondax

October 26, 2009 at 8:00 am

Top 5 AIF Development Tips and Tricks

with 2 comments

1. Enable the debugger

You will notice, if you put a breakpoint into your document class, that when you run the associated action, the breakpoint will get ignored – leaving you in the dark. This is because the action is called using a ‘runas’ function call  to change the user executing the code.

To workaround this replace the runas method calls with direct calls in\Classes\AifOutboundProcessingService\ and \Classes\AifInboundProcessingService, eg:

/*
runas(message.sourceEndpointAxUserId(),
classnum(AifInboundProcessingService),
staticmethodstr(AifInboundProcessingService, processAsUser),
[message.pack(), messageId]);
*/
AifInboundProcessingService::processAsUser([message.pack(), messageId]);

2. Run jobs instead of waiting for batch processing

The usual way for the AIF to run is using the batch processing framework, where you setup an interval for the inbound and outbound processing  to run.  This minute or so can feel like an age when you are in the middle of developing:

AIF batch processing

AIF batch processing

So use a custom job to have the AIF run instantly at the click of a button, here is an example of the receive job:


static void runAIFReceive(Args _args)
{
AifGatewayReceiveService aifGatewayReceiveService;
AifInboundProcessingService aifInboundProcessingService;
;

aifGatewayReceiveService = new AifGatewayReceiveService();
aifGatewayReceiveService.run();
aifInboundProcessingService = new AifInboundProcessingService();
aifInboundProcessingService.run(true);  // pass true for debug mode
}

3. Use file adapters

fileAdapter

When using the AIF, you will most likely be using a Biztalk, Web service or MSMQ adapter. Testing actions using one of these adapters can be a pain as you will most likely require another program to send or receive the message.

To get around this you can use a file adapter during testing, so that you can just write the message in plain XML and drop the file into a directory on your file system to be processed.

Then when you are finished testing / developing you can easily swap the file adapter out.

4. Compose messages with the Visual Studio XML editor

During development (when you use a file adapter), you can create the message using notepad or any other text editor. I recommend using the Visual Studio XML editor to quickly compose these to take advantage of intellisense, schema validation and other useful features (like inserting a guid):

xmlEditVS2008

5. Use the pipeline

It is unlikely (if you are integrating with a third party) that the XML schemas  of the external system match those in AX.

To transform the message into the format AX can handle you can use the pipeline to add a component to run an xslt on the inbound XML:

pipelineConfig

You can also create custom pipeline components, see this link for more information –

How Do I: Create a Custom AIF Pipeline Component?

Written by gregondax

September 24, 2009 at 1:23 pm

Spell check text box – Example of a WPF control in AX

with 2 comments

This post shows how to add ‘Microsoft Word’ like spell checking to standard AX forms, taking the label editor as an example:

Spell check control in AX

Spell check control in AX

When a word is misspelled it will underline that word red, and right-clicking will show a context menu with alternative suggestions.

The user control and xpo are both available to download, with instructions to install at axaptapedia (see link at the bottom of this post).

How it was built

The first step is to create the user control in Visual Studio 2008.

All there is to the user control is a WPF Textbox – which has spell checking built in!
All you need to do is set the SpellCheck.IsEnabled property::

<TextBox Name="textBox" SpellCheck.IsEnabled="True" Language="en-gb"></TextBox>

This user control then needs to be exposed as an ActiveX to be used in Dynamics AX. (A very helpful colleague of mine gave me some guidance on this.)

Please see this link (MSDN Tutorial: Create a Win32 Application Hosting WPF Content) for an example on how to do this.

To automatically deploy to clients

In AX, make use of the SysFileDeployer framework. To do this subclass SysFileDeploymentDLL, ensuring that you override the filename method to return the name of your library.

Also if you have developed a .Net WPF control you will need to register the library using regasm instead of regsvr32, so make sure you override (or inherit) the register and unregister methods so that regasm is used:

class SysFileDeployment_DevWpf extends SysFileDeploymentDLL
{
 #Define.regasmCommand(@'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe')
}

public Filename filename()
{
 return 'WpfTextBoxControlLibActiveX.dll';
}

protected void register(boolean atReboot = false)
{
 ;
 WinAPI::setCurrentDirectory(this.destinationPath());
 //TODO Check regasm executable exists on client?
 WinAPI::shellExecute(#regasmCommand, strfmt('"%1" %2', this.destinationPath()+this.filename(),'/codebase'));
}

protected void unRegister()
{
 ;
 WinAPI::setCurrentDirectory(this.destinationPath());
 //TODO Check regasm executable exists on client?
 WinAPI::shellExecute(#regasmCommand, this.destinationPath()+this.filename()+' /unregister');
}

Then add your class(es) to the SysFileDeployer filesToDeploy method:

private static container filesToDeploy()
{
 ;
 return [classnum(SysFileDeployment_DevWpfMainControl), classnum(SysFileDeployment_DevWpf)];
}

Then ensure that your dll’s are shared on your server (copy them to your  %AXDIR%/50/Client/Share/Include directory on your AOS server) and either update the application version of AX, or delete the usage data for the SysFileDeployer job for each client, then each client that starts up will get the option to deploy your new libraries:

clientFileDeployment

Download the control and example xpo here from axaptapedia
The steps for installing the control into your label editor can also be found here at axaptapedia

Written by gregondax

July 23, 2009 at 7:00 am

X++ and C# Compared

leave a comment »

With the advent of Dynamics AX 2009, the need for AX developers to know .Net is increasing.

I have noticed the AX developer documentation on MSDN getting better and better, here is a section comparing X++ and C# that is well worth a read, check it out!:

X++ and C# Comparisons

Dynamics AX MSDN Developer Center – X++ and C# comparisons

Written by gregondax

June 12, 2009 at 9:00 am

Storing passwords using AX

with one comment

If you need to store passwords in AX there are some application objects, classes and attributes that you can use.  This post details the steps you can take to allow entry of a password in a form, which will be stored in the database.

Password form

1.  Add the password field to your table. This field should be of type ‘CryptoBlob’ which is a container that contains binary data:

Password table field

2. Add an edit method for the password to your table:

//BP Deviation Documented
edit Password editPassword(boolean _set = false, Password _pwd = '')
{
    CryptoBlob cryptoBlob = connull();
    ;

    if (_set)
    {
        this.Password = WinapiServer::cryptProtectData(str2cryptoblob(_pwd));
    }

    return (this.Password == connull()) ? '' : 'xxxxxxxx';
}

3. Drag and drop the edit method to your form and ensure that the attribute ‘PasswordStyle’ is set to ‘Yes’:

Password form control

4. To retrieve the password you will need a method similar to the following:

static Password getPassword(UserId _userId)
{
    CryptoBlob cryptoBlob = TutorialPasswordTable::find(_userId).Password;
    ;

    return (cryptoBlob == connull()) ? '' :
                cryptoblob2str(WinapiServer::cryptUnProtectData(cryptoBlob));
}

 
Disclaimer / Notice / Yada Yada
The safest way to handle passwords is not to store them in the database. The steps described in this post are better than storing the password in the database as plain text, but far from bulletproof. Please ensure that AX security is fully considered if using this method (Table level security, access to code / development etc)

You can download the tutorial as an xpo here from axaptapedia

Written by gregondax

May 21, 2009 at 12:30 pm