Pages

Saturday, March 4, 2017

Custom AIF operation in Microsoft Dynamics AX 2009

Today let's try to create custom operation on generated by AX AIF Wizard service code.

1. Create new method and write code. 




2. Find you service object in AOT and Add service operation. Select checkbox and press OK.


3. In Visual Studio  add reference to your service.


4. Now can call your custom AX method  in C# code.

private int GetEmplVacationDays(string _emplID, DateTime _from, DateTime _to)
{
    var emplServiceClient = new goEmplTableServiceClient();
    int ret;
    
    try
    {
        ret = emplServiceClient.calcDays(_emplID, _from, _to);
    }
    catch (Exception xException)
    {
        ret = 0;
    }

    return ret;
}

Exposing display methods by AIF in Microsoft Dynamics AX 2009

Sometimes you need to send by AIF data display methods. In my case I have created AIF for EmplTable and additionally I would like to send full name.

1. Find in generated by AIF Wizard classes, class starts with Ax*. In this case it is AxEmplTable.
Write additional parm method returning full name.


2. Save class, Refresh Services, press Generate and do iisreset on IIS Server from command prompt.


3. In Visual Studio Update Service References.



4. Now I can see FullName on my fields list.



Sunday, February 19, 2017

Class extensions in X++

Suggested way to develop Dynamics 365 is not touching base code. Avoid overlayering is good practice especially in Azure environment. If you would like to add code for example to CustTable you can use extension methods. Below code is showing how to do it.

Note to ExtensionOf attribute, final keyword and _Extension suffix to the name of class.

[ExtensionOf(tableStr(CustTable))]
final class CustTable_Extension
{
    public str formatedName()
    {
        return strFmt('[%1] %2', this.AccountNum, this.name());
    }
}

And here example how to call extension class.

public static void main(Args _args)
{   
    CustTable       custTable;
    select firstonly custTable;
    info(custTable.formatedName());
}

And here example how to call extension class. The same we can do to class. Just change ExtensionOf attribute. Here we are extending MainClass.

[ExtensionOf(classStr(MainClass))]
final class MainClass_Extension
{
    public void sayHello()
    {
        info("Say hello from extension class");
    }
}

Here you can read more details.

Sunday, January 1, 2017

Batch jobs in Dynamics 365

My intention was to test how batch jobs are working in new Dynamics 365.

At first configuration. Configure batch group. Please check mentioned blog post and see if your configuration is correct.

Additional check if Microsoft Dynamics AX Batch Management Service is Running.






Basic code for batch job.

class goBatchTest extends RunBaseBatch
{
    #define.CurrentVersion(1)
    #define.Version1(1)

    public boolean canGoBatchJournal()
    {
        return true;
    }

    public boolean init()
    {
        return true;
    }

    protected void new()
    {
        super();
    }

    public container pack()
    {
        return [#CurrentVersion];
    }

    public void Update()
    {
        CustTable custTable;

        while select firstonly10 custTable
        {
    
            info(strFmt("[%1] %2", custTable.AccountNum, custTable.name()));
        }
    }

    public void run()
    {
        #OCCRetryCount
        if (! this.validate())
            throw error("");

        try
        {
            ttsbegin;

            this.Update();

            ttscommit;
        }
        catch (Exception::Deadlock)
        {
            retry;
        }
        catch (Exception::UpdateConflict)
        {
            if (appl.ttsLevel() == 0)
            {
                if (xSession::currentRetryCount() >= #RetryNum)
                {
                    throw Exception::UpdateConflictNotRecovered;
                }
                else
                {
                    retry;
                }
            }
            else
            {
                throw Exception::UpdateConflict;
            }
        }

    }

    public boolean runsImpersonated()
    {
        return true;
    }

    public boolean unpack(container packedClass)
    {
        Version version = RunBase::getVersion(packedClass);
    ;
        switch (version)
        {
            case #CurrentVersion:
                [version] = packedClass;
                break;
            default:
                return false;
        }

        return true;
    }

    public boolean validate(Object _calledFrom = null)
    {
        if (false)
            return checkFailed("");

        return true;
    }

    server static goBatchTest construct()
    {
        return new goBatchTest();
    }

    static ClassDescription description()
    {
        return "Test batch job";
} static void main(Args args) { goBatchTest goBatchTest; goBatchTest = goBatchTest::construct(); if (goBatchTest.prompt()) goBatchTest.runOperation(); } protected boolean canRunInNewSession() { return false; } }

When you are coding and extending from classes, below screen shows how and what methods you can override.




Next thing is right click on batch class object and select Set as Startup Object. After that Ctrl+F5.

You should see this windows. Configure batch job as usually in older versions of Dynamics AX.


Check results in System Administration. Working as expected.



As I see in this very basic actions, I haven't found no changes to previous versions of Dynamics AX batch jobs. Everything seems to be pretty much the same, at least till that moment.