Tuesday 17 November 2015

Testing Read service in X++

static void TestingService(Args _args)
{
    PurchPurchReqService     customerService;            // Customer Service class
    CustCustomer            customer;                   // Customer Document object
    PurchPurchReq            customerRead;               // Customer Document object
    CustCustomer_CustTable  custTable;                  // CustTable data object
    CustCustomer_CustTable  custTableRead;              // CustTable data object
    AifEntityKeyList        entityKeyList;              // Entity key list
    AifEntityKeyList        entityKeyListFind;          // Entity key list
    AifQueryCriteria        queryCriteria;
    AifCriteriaElement      criteriaElement;
    AccountNum              accountNum;




    // Create the service instance
    customerService =  PurchPurchReqService::construct();


    queryCriteria = new AifQueryCriteria();
    criteriaElement = AifCriteriaElement::newCriteriaElement('VendPurchOrderJour', 'PurchId', AifCriteriaOperator::Equal, 'US1PO0000440');
    queryCriteria.addCriteriaElement(criteriaElement);
    entityKeyListFind = customerService.findKeys(queryCriteria);


    // Read Customer using returned entity key
    customerRead = customerService.read(entityKeyListFind);
    info(customerRead.serialize());
    //custTableRead = customerRead.parmCustTable().get_Item(0);
    //info(strfmt("Read customer: Account Number: %1, Name: %2.", custTableRead.parmAccountNum(), custTableRead.parmName()));





}

Debugging Services in AX 2012

Regardless of the adapter type you choose, services in AX 2012 run as IL code - even if you are using the file adapter.  To debug IL code
you must use Visual Studio.
The steps for debugging a service are:
  1. On the AOS, install the AX Application Explorer from the Microsoft Dynamics AX CD by choosing the
    component Developer Tools, and then click Visual Studio Tools.  If the AX debugger is not installed,
    you will also need to install it.
  2. Open Visual Studio, create a new project, and in the AX Application Explorer find the methods in the
    service classes that are of interest.
  3. In Visual Studio add breakpoints where needed.
  4. Click Debug, and then click Attach to Process.
  5. Mark the checkboxes for Show processes from all users and Show processes in all sessions.
  6. Select AX32Serv.exe and click Attach.The Ax32Serv.exe won't be able available if you are not debugging on the AOS, so
    you need to be on the AOS.
  7. In a separate instance of VS (so have two different projects/solutions open) run the code that calls
    the service.One instance to run the code that
    calls the service and another instance of VS to do the debugging.  By
    having two instances open, you can leave the debugging instance attached to the
    AOS while you run the service code over and over again.

Friday 13 November 2015

AIF Document services operations

static void AifSample_CustomerService(Args _args)
{

CustCustomerService     customerService;            // Customer Service class
    CustCustomer            customer;                   // Customer Document object
    CustCustomer            customerRead;               // Customer Document object
    CustCustomer_CustTable  custTable;                  // CustTable data object
    CustCustomer_CustTable  custTableRead;              // CustTable data object
    AifEntityKeyList        entityKeyList;              // Entity key list
    AifEntityKeyList        entityKeyListFind;          // Entity key list
    AifQueryCriteria        queryCriteria;
    AifCriteriaElement      criteriaElement;
    AccountNum              accountNum;
    Name                    name;
    ;

    // Create the service instance
    customerService =  CustCustomerService::construct();

    // Create the Customer document object
    customer = new CustCustomer();
    customer.createCustTable();                            // Create the CustTable list
    custTable = customer.parmCustTable().addNew();         // Add CustTable instance to CustTable list

    // Initialize the CustTable instance
   // custTable.parmName("Cust01");
    custTable.parmAccountNum("22220002");
    custTable.parmCustGroup("10");
    custTable.parmCurrency("USD");
    custTable.parmPartyType(DirPartyType::Organization);

    // Create Customer
    entityKeyList = customerService.create(customer);
    accountNum = entityKeyList.getEntityKey(1).parmKeyDataMap().lookup(fieldnum(CustTable, accountnum));
    info(strfmt("Created customer:  Account Number: %1.", accountNum));

    //Creating another Customer
    entityKeyList = customerService.create(customer);
    accountNum = entityKeyList.getEntityKey(2).parmKeyDataMap().lookup(FieldNum(Custtable,accountNum));
    info(strfmt("Created another Customer :  Account Number : %1. ",accountNum));


    // Read Customer using returned entity key
    customerRead = customerService.read(entityKeyList);
    custTableRead = customerRead.parmCustTable().get_Item(0);
    info(strfmt("Read customer: Account Number: %1, Name: %2.", custTableRead.parmAccountNum(), custTableRead.parmName()));

    // Update Customer
    custTableRead.parmName(custTableRead.parmName() + ": Updated Name");
    customerService.update(entityKeyList, customerRead);
    info (strfmt("Updated Customer: Account Number: %1.", custTableRead.parmAccountNum()));

    // Call Read to check update
    customer = customerService.read(entityKeyList);
    custTable = customerRead.parmCustTable().get_Item(0);
    info(strfmt("Read updated customer: Account Number: %1, Name: %2.", custTable.parmAccountNum(), custTable.parmName()));

    // Call FindKeys to find entity keys of matching customer entity keys
    queryCriteria = new AifQueryCriteria();
    criteriaElement = AifCriteriaElement::newCriteriaElement('CustTable', 'CustGroup', AifCriteriaOperator::Equal, '10');
    queryCriteria.addCriteriaElement(criteriaElement);
    entityKeyListFind = customerService.findKeys(queryCriteria);
    info(strfmt("Find customer keys: Result count: %1", entityKeyListFind.getEntityKeyCount()));

    // Call Find to find matching customers
    queryCriteria = new AifQueryCriteria();
    criteriaElement = AifCriteriaElement::newCriteriaElement('CustTable', 'CustGroup', AifCriteriaOperator::Equal, '10');
    queryCriteria.addCriteriaElement(criteriaElement);
    customerRead = customerService.find(queryCriteria);
    info(strfmt("Find customer: Result count: %1", customerRead.existsCustTable()?customerRead.parmCustTable().get_Count():0));

    info("TO DO: To test delete, uncomment delete code in the job.");
    // TODO: UNCOMMENT TO DELETE CUSTOMER
    /*
    // calling deleting customer
    customerService.delete(entityKeyList);
    info(strfmt("Deleted customer:  Account Number: %1.", accountNum));
    */
    pause;

}