AIF testing in AX 2012

AIF testing in AX 2012

Tutorial
Published on
Jul 14, 2015

There are a couple of ways to test AIF inbound classes in Dynamics AX:

  1. From inside Dynamics AX using jobs:
    a. Testing only classes for creating records in database.
    b. Testing service classes and classes for creating records in database.
  2. Configuring and testing AIF service.

Testing only classes for creating records in database

Below is code for creating record in ProjTable.

static void ANG_AIF_TestServiceAX_1(Args _args)

{

   AifEntityKeyList                            entityKeyList;                          // Entity key list

   ProjTableService                            projTableService;                       // Service class

   ProjProjTable                               projProjTable;                          // Document object

   ProjProjTable_ProjTable                     projProjTable_ProjTable;                // data object;

   ProjProjTable_TableDlvAddr                  projProjTable_TableDlvAddr;             // data object

   // Create the service instance

   projTableService = ProjTableService::construct();

   // Create the data document object

   projProjTable = new ProjProjTable();

   projProjTable.createProjTable();                                                    // Create the data list

   projProjTable_ProjTable = projProjTable.parmProjTable().addNew();                   // Add data object instance to data list

   // Initialize the data instance

   projProjTable_ProjTable.parmProjId("P-000003");

   projProjTable_ProjTable.parmName("Test name");

   projProjTable_ProjTable.parmProjGroupId("FixWIP");

   projProjTable_ProjTable.parmType(ProjType::FixedPrice);

   projProjTable_ProjTable.parmProjInvoiceProjId("GTC-19154");

   projProjTable_ProjTable.parmStatus(ProjStatus::InProcess);

   // Create the top level record

   projProjTable_TableDlvAddr = new ProjProjTable_TableDlvAddr();

   // Addind lower level record

   projProjTable_TableDlvAddr.parmAddress("Test Address");

   projProjTable_TableDlvAddr.parmCountryRegionId("PL");

   projProjTable_ProjTable.createTableDlvAddr().add(projProjTable_TableDlvAddr);

   // Create data

   entityKeyList = projTableService.create(projProjTable);

   info("end");

}

Testing service classes and classes for creating records in database

Here first we need to get XML file:

static void  ANG_AIF_TestSerwiceXMLGet(Args _args)

{

   AxdProjTable                                axdProjTable;

   AifEntityKey                                aifEntityKey;

   Map                                         map;

   XMLDocument                                 xmlDocument;

   XML                                         XML;

   AifPropertyBag                              aifPropertyBag;

   FileName                                    fileName;

   fileName    = @"C:\Temp\output.xml";

   map         = new Map(Types::Integer, Types::Container);

   map.insert(fieldNum(ProjTable, ProjId), ['AA-0033']);

   aifEntityKey = new AifEntityKey();

   aifEntityKey.parmTableId(tableNum(ProjTable));

   aifEntityKey.parmKeyDataMap(map);

   try

   {

       axdProjTable    = new AxdProjTable();

       xml             = axdProjTable.read(aifEntityKey, null, new AifEndPointActionPolicyInfo(), new AifConstraintList(), aifPropertyBag);

       new FileIoPermission(fileName, 'rw').assert();

       xmlDocument = XMLDocument::newXml(XML);

       xmlDocument.save(fileName);

       CodeAccessPermission::revertAssert();

   }

   catch

   {

       throw error('Error in document service outbound');

   }

   info("end");

}

After that we should modify that file and run job:

static void ANG_AIF_TestSerwiceXML(Args _args)

{

   XmlDocument                                 xmlDocument;

   AxdProjTable                                axdProjTable;

   FileName                                    fileName;

   //feed the xml that was modified after exporting through exportxml job

   fileName = @"C:\Temp\Test1.xml";

   try

   {

       new FileIoPermission(fileName, 'r').assert();

       xmlDocument     = XmlDocument::newFile(fileName);

       axdProjTable    = new AxdProjTable();

       axdProjTable.create(xmlDocument.xml(), new AifEndPointActionPolicyInfo(), new AifConstraintList());

       CodeAccessPermission::revertAssert();

   }

   catch

   {

       throw error('Error in document service');

   }

   info("end");

}

If you modified xml file correctly then you should have new record in your database.

Share it
New course!

Programming in Dynamics 365 for Operations

19-23 June 2017
Wroclaw, Poland
In-depth Dynamics 365 for Operations training course is now available in Poland!
Read more →
Written by
Michal Szemplinski

Microsoft Dynamics developer from July 2008. Comes from Tychy in Poland. Graduated at Silesian University of Technology as Computer Science Master.