Wednesday, April 24, 2013

Deploying Sales order Document Service in an Outbound Exchange [AX 2012]



Walkthrough: Deploying Sales order Document Service in an Outbound Exchange [AX 2012]
This walkthrough illustrates the following tasks:

  • Set up Application Integration Framework (AIF) components for an outbound exchange.
  • Code the outbound exchange.
  • Test the outbound exchange

You must first create an outbound directory. Microsoft Dynamics AX puts the XML files that it creates in the outbound directory.

To create and configure a directory


  1. In Windows Explorer, create an outbound directory.
Important: The file to the inbound/outbound folder should have the user executing the AIF operation added to security and the owner should be set the same user in the Security / Advanced tab.
Use the takeown-contextmenu (tool which should be downloaded & installed) which adds to registry entry which can be used to change the user using Shift + Right Click -> “Take Ownership”

To create the outbound port

  1. Open the Outbound ports form. Click System administration > Setup > Services and Application Integration Framework > Outbound ports.
  2. Click New.
  3. Name the new integration port HKSSalesOrderRead.

To configure the outbound port to use the file adapter

  1. In the Adapter list in the Address group, select File system adapter.
  2. In the URI list, click the arrow to browse to the folder that you created that is named AIFOut.
  3. On the Service contract customizations FastTab, click Service operations.
  4. In the Select service operations form, select SalesSalesOrderService.read in the Remaining service operations list. Click the left arrow to move the service operation to the Selected service operations list.
  5. Close the form.
  6. On the Service contract customizations FastTab, select Customize documents and then click Data policies.
  7. Close the Document data policies form.
  8. Expand the Troubleshooting FastTab, select All document versions for Logging mode.
  9. In the Outbound ports form, click Activate to activate the port.

Coding for outbound exchange:

Refer the below link to add code the Sales form

Alternatively use the below code in a job  and run the job.
This will create a record in the Queue Manager.  
System administration/Periodic/Services and Application Integration Framework/Queue Manager
static void sendElectronicallySalesOrder(Args _args)
{
   AxdSendContext axdSendContext = AxdSendContext::construct();
   AifEntityKey aifEntityKey = AifEntityKey::construct();
   Map keyData;
   AifConstraintList aifConstraintList = new AifConstraintList();
   AifConstraint aifConstraint = new AifConstraint();
   SalesTable  salesTable;
   ;
   salesTable = salesTable::find("SO-101282");
  info(salesTable.SalesId);
   keyData = SysDictTable::getKeyData(salesTable);

   aifEntityKey.parmTableId(salesTable.TableId);
   aifEntityKey.parmRecId(salesTable.RecId);
   aifEntityKey.parmKeyDataMap(keyData);

   //axdSendContext.parmXMLDocPurpose(_xMLDocPurpose);
   axdSendContext.parmSecurity(false);

   aifConstraint.parmType(AifConstraintType::NoConstraint) ;
   aifConstraintList.addConstraint(aifConstraint) ;

   AifSendService::submitDefault(
       classnum(SalesSalesOrderService),
       aifEntityKey,
       aifConstraintList,
       AifSendMode::Async,
       axdSendContext.pack());

}

Test the OutBound service.

For AIF to begin sending and receiving documents for adapter-based exchanges, the services that move documents through the queues must be running as batch jobs within Microsoft Dynamics AX.

To create the batch job

  1. Open the Batch job form. Click System administration > Inquiries > Batch jobs > Batch jobs.
  2. Create a new batch job and enter a name. For more information, see Create a batch job.
  3. Click View tasks. On the Batch tasks form, press CTRL+N to add a row, and then enter a Task description and select the Company account.
  4. Add four tasks that use the following classes to the batch job: AifGatewayReceiveService, AifGatewaySendService, AifInboundProcessingService, and AifOutboundProcessingService.
  5. On the Batch job form, set a recurrence interval for the batch job. For testing, it is convenient to set the interval to one minute.
  6. To start the batch job, change its status to Waiting.

To test sending the document

  1. Open the Sales order form.
  2. Click the Send electronically button. Click Original to send the original document.
The selected sales order and its lines are serialized into XML with header information and sent to the gateway queue. When the AIF batch services run, the message will be selected from the gateway queue and sent to the AIFout directory that you created earlier.
Alternatively used the below code if the document was created using job sendElectronicallySalesOrder
static void AifGateWayJob(Args _args)
{
   AifGatewayReceiveService        AifGatewayReceiveService;
   AifInboundProcessingService     AifInboundProcessingService;
   AifOutboundProcessingService    AifOutboundProcessingService;
   AifGatewaySendService           AifGatewaySendService;
   ;
   AifGatewayReceiveService =  new AifGatewayReceiveService();
   AifGatewayReceiveService.run();

   AifInboundProcessingService = new AifInboundProcessingService();
   AifInboundProcessingService.run();

   AifOutboundProcessingService = new AifOutboundProcessingService();
   AifOutboundProcessingService.run();

   AifGatewaySendService =  new AifGatewaySendService();
   AifGatewaySendService.run();


}
Results:
Check the output folder to see if the xml is generated.
Ex:




If file is not generated, check for the errors:
Batch job was used for testing:
System administration/Inquires/Batch Jobs
Select the batch job and open Batch job history and view the logs.


System administration/Periodic/Services and Application Integration Framework/Queue Manager to check the status of the queues and Exceptions forms to view the corresponding error messages.