Dynamics Search Engine

Showing posts with label AIF. Show all posts
Showing posts with label AIF. Show all posts

Monday, June 20, 2016

How to export data in a csv or comma separated file by X++ code in Microsoft Dynamics AX 2012


In Microsoft Dynamics AX 2012 how to export data in a csv or comma separated file by X++ code.

This article explains how to generate a csv or comma separated file and export data from Microsoft Dynamics AX 2012 by X++ code.



Applied on: Dynamics AX 2012 R3 CU9.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience: AX programmers.
Assumption: You are familiar with Dynamics AX 2012.



I prepared a x++ job here which will guide you how to generate a csv file with a header and one sample line. If you have one header and multi line, you may keep line in a while.



static void exportDataInCSV(Args _args)

{

    Dialog          dialog  = new Dialog();

    DialogField     dialogFieldFileSave;

    Commaio         file;

    container       line;

    FilePath        TempPath;



    dialogFieldFileSave     = dialog.addField(extendedTypeStr(FilenameSave),"File path","Help text goes here");

    dialog.caption("Caption goes here");

    dialog.filenameLookupFilter(['csv','*.csv']);



    if(!dialog.run())

        return;



    if (dialogFieldFileSave.value() == '')

    {

        error("Please set up the location for export file.");

        return;

    }



    TempPath = dialogFieldFileSave.value() + '.csv';



    #define.filename(TempPath)

    #File





    file = new Commaio(#filename , #io_write);

    if( !file || file.status() != IO_Status::Ok)

    {

        throw error("File Cannot be opened");

    }



    //Put headers

    line = ["CardNum","Amount","IssueDate","ExpiryDate","IssueBy","CustAccount","Description"];

    file.writeExp(line);



    //Put a sample line

    line = ["C_476786","100.00","05/26/2015","03/25/2016","John Smith S","Cust_0000034","Description"];

    file.writeExp(line);



    info(strFmt("File exported successfully. Please find it at %1",TempPath));

}





Hope this was useful. You may leave your comment below.








Wednesday, February 19, 2014

How to call an AIF Service operation and send data outside Microsoft Dynamics AX 2012 using X++ code


Sometimes it happens like you developed an AIF Service in Microsoft Dynamics AX and not able to test. Reason can be anything. Consider a scenario where you need to send AX data outside using standard AIF services but don’t know how to send.
This article explains you how to run a read operation for an AIF Service to send data from Microsoft Dynamics AX to your destination folder using File System Adapter. This article may answer below questions:

Using X++ codes how to call a service operation for Microsoft Dynamics AX 2012 AIF?
How to call or execute read operation in Microsoft Dynamics AX 2012?
How to send data outside Microsoft Dynamics AX 2012 using Outbound?

How to use standard Microsoft Dynamics AX 2012 AIF Services?

Applied on: Dynamics AX 2012 R2
Prerequisite: X++; MorphX; Basic programming knowledge, Basic AIF
Target audience: AX programmers
Assumption: Outbound port is configured properly. The person has the basic idea about AIF.

Here I used a standard service call VendVendTableService and read operation of it to send Vendor data outside.

Below job is to select a vendor record and send to Queue Manager.

 
static void aifOutBoundServiceCall(Args _args)
{
    AxdSendContext      axdSendContext  = AxdSendContext::construct();
    AifEntityKey        aifEntityKey    = AifEntityKey::construct();
    Map                 keyData;
    AifConstraintList   aifConstraintList   = new AifConstraintList();
    AifConstraint       aifConstraint       = new AifConstraint();
    VendTable           vendTable;

    select firstOnly vendTable
        where vendTable.AccountNum == "‪‪V-1001";

    keyData = SysDictTable::getKeyData(vendTable);
    aifEntityKey.parmTableId(vendTable.TableId);
    aifEntityKey.parmRecId(vendTable.RecId);
    aifEntityKey.parmKeyDataMap(keyData);

    axdSendContext.parmXMLDocPurpose(XMLDocPurpose::Original);
    axdSendContext.parmSecurity(false);
    aifConstraint.parmType(AifConstraintType::NoConstraint) ;
    aifConstraintList.addConstraint(aifConstraint) ;


//To handle an error it can be checked if outbound port is configured. This is optional.
    AifSendService::submitDefault(  classnum(VendVendTableService),
                                    aifEntityKey,
                                    aifConstraintList,
                                    AifSendMode::Async,
                                    axdSendContext.pack());

}
 

Below logic can be used to check whether Outbound port is configure or not properly. It’s not mandatory but nice to have.
boolean canSendXML()
{
    boolean             ret;
    AifActionId         actionId;
    AifEndpointList     endpointList;
    AifConstraint       aifConstraint = new AifConstraint();
    AifConstraintList   aifConstraintList = new AifConstraintList();

    actionId = AifSendService::getDefaultSendAction(classnum(VendVendTableService), AifSendActionType::SendByKey);

 

    if (actionId)
    {
        aifConstraint.parmType(AifConstraintType::NoConstraint);
        aifConstraintList.addConstraint(aifConstraint);
 
        endpointList = AifSendService::getEligibleEndpoints(actionId, aifConstraintList);
 
        if (endpointList.getEndpointCount()>0)
        {
            ret = true;
        }
    }

    return ret;
}

 
To release data from Queue Manager below job can be executed.

static void aifOutboundExecutoinJob()
{
    new AifOutboundProcessingService().run();
    new AifGatewaySendService().run();
}

 
 
After execution of above job below is the output.