Thursday, August 25, 2016

Catch exact error message from AX AIF service


when interacting with AX AIF services to catch the exact Error message this is what you would do in your C# Catch block


catch(FaultException<AifFault> ex)
            {
                InfologMessage[] infoLogList = ex.Detail.InfologMessageList;
                foreach (var info in infoLogList)
                {
                    Console.WriteLine(info.Message);
                }

                proxy.Abort();
                Console.ReadLine();
            }

Wednesday, July 27, 2016

Using Customer AIF Service in AX 2012

In this post i would be demonstrating how to use out of box Customer Service (CustCustomerService) for the following operations:

  1. Read a Customer
  2. Create a Customer
  3. Update a Customer
  4. Delete a Customer
  5. Find Customers
  6. FindKeys for Customers
CustService is the alias name i used in my code to refer Customer Service;

using CustService = ConsumeAXServices.CustomerServiceRef;

Here is the C# method to Read a Customer:

public  CustService.AxdCustomer  CustomerRead(ref CustService.EntityKey[] entityKeyList, string accNum =  "US-003" , Boolean halt = true)
        {
            CustService.CustomerServiceClient proxy = null;
            CustService.AxdCustomer custDoc = null;
            CustService.CallContext callContext = null;

            try
            {
                custDoc = new CustService.AxdCustomer();                

                CustService.EntityKey[] custKeyList = new CustService.EntityKey[1];
                CustService.EntityKey custKey = new CustService.EntityKey();
                custKey.KeyData = new CustService.KeyField[1];

                CustService.KeyField custAccountNum = new CustService.KeyField();
                custAccountNum.Field = "AccountNum";
                custAccountNum.Value = accNum;

                custKey.KeyData[0] = custAccountNum;
                custKeyList[0] = custKey;

                callContext = new CustService.CallContext() { Company = "usmf" };
                proxy = new CustService.CustomerServiceClient();             

                custDoc = proxy.read(callContext, custKeyList);
                Console.WriteLine("Cust Num - " + custDoc.CustTable[0].AccountNum);
                Console.WriteLine("Cust CreditStatus - " + custDoc.CustTable[0].CreditRating);
                
                if (halt)
                    Console.ReadLine();

                entityKeyList = custKeyList;
                
            }

            catch (Exception ex)
            {
                Console.WriteLine("Error - " + ex.Message);
                proxy.Abort();
                Console.ReadLine();

            }
            finally
            {
                if (proxy != null)
                {
                    proxy.Close();
                    proxy = null;
                }
            }

            return custDoc;
        }


Here is the C# method to Create a Customer:


public void CreateCustomer(string accNum = "SanCus003", string custName = "Santosh Cust 003")
        {
            CustService.CustomerServiceClient proxy = null;
            CustService.AxdCustomer custDoc;
            CustService.CallContext callContext = null;
            CustService.AxdEntity_CustTable custTable = null;
            CustService.AxdEntity_DirParty_DirOrganization dirPartyOrg = null;
            CustService.AxdEntity_OrganizationName orgName = null;           


            try 
            {
                custDoc = new CustService.AxdCustomer();

                custTable = new CustService.AxdEntity_CustTable() { AccountNum = accNum, CustGroup = "20" };
                
                dirPartyOrg = new CustService.AxdEntity_DirParty_DirOrganization() { NumberOfEmployees = 11, NumberOfEmployeesSpecified = true };
                orgName = new CustService.AxdEntity_OrganizationName { Name = custName };
                dirPartyOrg.OrganizationName = new CustService.AxdEntity_OrganizationName[] { orgName };


                custDoc.CustTable = new CustService.AxdEntity_CustTable[] { custTable};
                custTable.DirParty = new CustService.AxdEntity_DirParty_DirPartyTable[] { dirPartyOrg };

                callContext = new CustService.CallContext() { Company = "usmf" };
                proxy = new CustService.CustomerServiceClient();
                var entityKeyList =  proxy.create(callContext, custDoc);

                var keyField = entityKeyList.FirstOrDefault().KeyData.FirstOrDefault();
                Console.WriteLine("Key Field - " + keyField.Field + "Key Value - " + keyField.Value);
                Console.ReadLine();

            }

// removed Catch and finally block to save the space



Here is the C# method to Update a Customer:

public void UpdateCustomer(string accNum = "SanCus002", string CustName = "Santosh Cust 002 - Update3")
        {
            CustService.CustomerServiceClient proxy = null;
            CustService.AxdCustomer custDoc = null;
            CustService.CallContext callContext = null;
            CustService.AxdEntity_CustTable custTable = null;
            CustService.AxdEntity_DirParty_DirOrganization dirPartyOrg = null;
            CustService.AxdEntity_OrganizationName orgName = null;

            CustService.EntityKey[] entityKeyList = null; 


            try
            {
               custDoc = this.CustomerRead(ref entityKeyList, accNum, false);

               custTable = custDoc.CustTable[0];
               custTable.CreditMax = 10000;
               custTable.action = CustService.AxdEnum_AxdEntityAction.update;
               custTable.actionSpecified = true;               
                
               dirPartyOrg = custDoc.CustTable[0].DirParty[0] as CustService.AxdEntity_DirParty_DirOrganization;                               
               dirPartyOrg.NumberOfEmployees = 12;
               dirPartyOrg.NumberOfEmployeesSpecified = true;
               dirPartyOrg.action = CustService.AxdEnum_AxdEntityAction.update;
               dirPartyOrg.actionSpecified = true;                         
               
               orgName =  dirPartyOrg.OrganizationName[0];
               orgName.updateMode = CustService.AxdEnum_ValidTimeStateUpdate.Correction;
               orgName.updateModeSpecified = true;
               orgName.Name = CustName;
               orgName.action = CustService.AxdEnum_AxdEntityAction.update;
               orgName.actionSpecified = true;
                

               callContext = new CustService.CallContext() { Company = "usmf" };
               proxy = new CustService.CustomerServiceClient();
               proxy.update(callContext, entityKeyList, custDoc);
                
               Console.WriteLine("Customer Update Success");
               Console.ReadLine();

            }

// removed Catch and finally block to save the space



Here is the C# method to Delete a Customer (customer shouldn't have any transactions assoicated):

public void DeleteCustomer(string accNum = "US-003")
        {
            CustService.CustomerServiceClient proxy = null;
            CustService.CallContext callContext = null;
            
            try
            {
                CustService.EntityKey[] custKeyList = new CustService.EntityKey[1];
                CustService.EntityKey custKey = new CustService.EntityKey();
                custKey.KeyData = new CustService.KeyField[1];

                CustService.KeyField custAccountNum = new CustService.KeyField();
                custAccountNum.Field = "AccountNum";
                custAccountNum.Value = accNum;

                custKey.KeyData[0] = custAccountNum;
                custKeyList[0] = custKey;

                callContext = new CustService.CallContext() { Company = "usmf" };
                proxy = new CustService.CustomerServiceClient();
                proxy.delete(callContext, custKeyList);

                Console.WriteLine("Customer Delete Success - " + accNum);
                Console.ReadLine();
            }
// removed Catch and finally block to save the space

     


Here is the C# method to Find Customers based on a Criteria

        public void FindCustomers(string custGrp = "10")
        {
            CustService.CustomerServiceClient proxy = null;
            CustService.CallContext callContext = null;
            CustService.QueryCriteria qc = null;
            CustService.AxdCustomer custDoc = null;
            CustService.AxdEntity_CustTable[] custTableList = null;

            try 
            {
                CustService.CriteriaElement criteria = new CustService.CriteriaElement() 
                                                    { DataSourceName = "CustTable", FieldName = "CustGroup", Value1 = custGrp };

                CustService.CriteriaElement[] criteriaList = new CustService.CriteriaElement[] { criteria };
                qc = new CustService.QueryCriteria();
                qc.CriteriaElement = criteriaList;

                callContext = new CustService.CallContext() { Company = "usmf" };
                proxy = new CustService.CustomerServiceClient();
                custDoc = proxy.find(callContext, qc);

                custTableList = custDoc.CustTable;
                foreach (var custTable in custTableList)
                {
                    Console.WriteLine("Customer - {0} , Group - {1}" , custTable.Name, custTable.CustGroup);                    
                }

                Console.ReadLine();                
            }
// removed Catch and finally block to save the space





Here is the C# method to FindKeys of Customers based on a Criteria:

        public void FindKeysCustomer(string custGrp = "10")
        {
            CustService.CustomerServiceClient proxy = null;
            CustService.CallContext callContext = null;
            CustService.QueryCriteria qc = null;
            CustService.EntityKey[] custKeyList = null;            

            try
            {
                CustService.CriteriaElement criteria = new CustService.CriteriaElement() { DataSourceName = "CustTable", FieldName = "CustGroup", Value1 = custGrp };

                CustService.CriteriaElement[] criteriaList = new CustService.CriteriaElement[] { criteria };
                qc = new CustService.QueryCriteria();
                qc.CriteriaElement = criteriaList;

                callContext = new CustService.CallContext() { Company = "usmf" };
                proxy = new CustService.CustomerServiceClient();
                custKeyList  = proxy.findKeys(callContext, qc);

                foreach (var custKey in custKeyList)
                {
                    CustService.KeyField keyField = custKey.KeyData.FirstOrDefault();
                    Console.WriteLine("Key Field - {0} , Key Value - {1}", keyField.Field, keyField.Value);                    
                }

                Console.ReadLine();

            }
// removed Catch and finally block to save the space








Saturday, July 9, 2016

AX 7 Tips

Tip1: How to run a Runnable class directly from Web browser:
syntax: https://<aosUrl>/?mi=SysClassRunner&cls=<RunnableClassName>

Ex:
https://usnconeboxax1aos.cloud.onebox.dynamics.com/?mi=SysClassRunner&cls=WipRunClass1

an FYI...Runnable is just a regular class with Main method (main method is the starting point)



Tip2: AX dlls needed in a C# Project:



Tip3: Not able to break into X++ code or C# code, then here is your solution:
You may need to manually load the debug Info files(.dll, .pdb, .netModule)  through the VS -> Debug -> Modules window -> Search for C# namespace or X++ class name) -> Load Symbols




Tip4:   you can manually attach the VS debugger to worker process : VS -> Debug -> Process -> W3Wp.exe . In the case of AX 2012 you would attach the VS debugger to "AX32Serv.exe" process to debug anything that always runs in CLR, Example: Services.
(Also you don't need to set an aot element as "Start Up Object" in VS every time, you just need to know how run your object directly from the web browser)





Wednesday, June 29, 2016

AX 2012 known issues

Error1:
Team Foundation services are not available from server

you may get this error  for multiple reasons and one of the reason could be that your TFS may have multiple Project Collections and your AX Team Project is not in the Default Project collection.

Solution:  make sure to include TFS Project Collection name in the TFS URL : 
http://tfs:8080/tfs/<ProjectCollectionName>




Where is Tile Button control in AX 7

Tile Button control is part of  "Application Foundation" Model. This means unless your project has a reference to this model you wouldn't be able to add this Tile button control on any form.

If you need to refer more models in your project then go through Update Model Parameters (Dynamics AX Menu)  wizard and select the models you need.

Thursday, April 14, 2016

Item Service in AX 2012- This document does not support the AxEcoResProductIdentifier class or AxEcoResProductCategory class

In AX 2012's Item service as part of creating an Item/Product through AIF Service if you try to populate entities EcoResProductIdentifier, EcoResProductCategory you may receive these errors:

This document does not support the AxEcoResProductIdentifier class
or
This document does not support the AxEcoResProductCategory class

You may very well receive these errors after going through the AIF wizard and regenerating the Item Service (InventTable and EcoResProduct), the reason being wizard doesn't generate some code related to all the Tables involved in the AxdQuery.

To fix this, I had to add following code in class -> AxdEcoResProduct-> prepareForSave()

       case classNum(AXEcoResProductIdentifier):
            axecoResProductIdentifier   = _axdStack.top() as AxEcoResProductIdentifier;
            axEcoResProduct             = axecoResProductIdentifier.parentAxBC() as AxEcoResProduct;
            axecoResProductIdentifier.parmProduct(axEcoResProduct.parmRecId());
            return true;       
        
        case classNum(AxEcoResProductCategory):
            axEcoResProductCategory     = _axdStack.top() as AxEcoResProductCategory;
            axEcoResProduct             = axEcoResProductCategory.parentAxBC() as AxEcoResProduct;
            axEcoResProductCategory.parmProduct(axEcoResProduct.parmRecId());
            return true;       


Ofcourse I had declare the corresponding variables.
Happy Daxing.....