Initialisation and validation of data contract in AX 2012

Initialisation and validation of data contract in AX 2012

Tutorial
Published on
Sep 17, 2015

If we need to initialise or validate data contract members’ values we can use two dedicated interfaces for that. They are named SysOperationInitializable and SysOperationValidatable.

In following text you will find sample implementation. Members of this simple data contract are initialised with current date and validated in some manner, and if validation is failed, feedback information is presented to user. Please notice, that spoken logic is limited to context of data contract object only. If it requires broader context we can initialise data contract in controller and validate in operation, but that goes out of scope of this blog entry.

[DataContractAttribute]

class SimpleContract implements SysOperationInitializable, SysOperationValidatable

{

   StartDate startDate;

   EndDate endDate;

   [DataMemberAttribute]

   public StartDate parmStartDate(StartDate _startDate = startDate)

   {

       startDate = _startDate;

       return startDate;

   }

   [DataMemberAttribute]

   public EndDate parmEndDate(EndDate _endDate = endDate)

   {

       endDate = _endDate;

       return endDate;

   }

   public void initialise()

   {

       startDate = systemDateGet();

       endDate = startDate;

   }

   public boolean validate()

   {

       if (endDate < startDate)

       {

           return checkFailed("End date must be greater or equal to start date.");

       }

       return true;

   }

}

class SimpleService

{

   public void run(SimpleContract _contract)

   {

       info(strFmt("Date range: %1 - %2", _contract.parmStartDate(), _contract.parmEndDate()));

   }

}

class SimpleController extends SysOperationServiceController

{

   public static void main(Args args)

   {

       SimpleController controller;

       controller = new SimpleController();

       controller.parmClassName(classStr(SimpleService));

       controller.parmMethodName(methodStr(SimpleService, run));

       controller.startOperation();

  }

}

Share it
Written by
Marek Korejwo

Microsoft Dynamics programmer, consultant and enthusiast since 2009. Experienced in discrete production and media systems.

New course!

Programming in Dynamics 365 for Operations

9-13 October 2017
Wroclaw, Poland
In-depth Dynamics 365 for Operations training course is now available in Poland!
Read more →