Tuesday, 21 April 2015

AIF Calling CustCustomerService.Update using WCF from C#

Create and configure the Inbound integration port for CustCustomerService.Update

  1. Open the Inbound ports form. Click System administration > Setup > Services and Application Integration Framework > Inbound ports.
  2. Click New.
  3. Name the new integration port CRM2AXCustCustomerService.
  4. In the Adapter list in the Address group, select NetTcp.
  5. On the Service contract customizations FastTab, click Service operations.
  6. In the Select service operations form, select CustCustomerService.create,CustCustomerService.read and CustCustomerService.update in the Remaining service operations list. Click the left arrow to move the service operation to the Selected service operations list. Then, close the form.
  7. You can modify the document data policy to match the fields that you want to return in the response. If you do not specify a data policy, then all sales order schema elements are included in the response message by AIF. If you specify a data policy but do not modify the default data policy, then only the default elements are included in the sales order schema.
  8. Activate the integration port.
  9. Make note of the address that is displayed in the WSDL URI text box.
  10. Close the Inbound ports form.
Create a C# project to consume the WCF service 
  1. Open Visual Studio and create a new console application project in Visual C#. Name the project "CRM2AXMKClient".
  2. In Solution Explorer, right-click the project name and then click Add Service Reference.
The Add Service Reference dialog box opens.
  1. In the Address text box, enter the WSDL URI from the port that you created in the previous section. For example:
4.  http:// XXXXMKAOSXX:8101/DynamicsAx/Services/CRM2AXCustCustomerService
  1. Click Go. Wait for Visual Studio to find the service.
  2. Click OK to close the dialog box.
  3. Open Program.cs. In the code editor, replace the code with the following C# code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM2AXUpdateCustCustomerService
{
    class Program
    {
        //Mallik Code to updated Customer using WCF (CustCustomerService.update()) 20/04/2015
        //  The customer service is date effective because it contains the DirPersonName, DirOrganizationName,
        //DirPartyLocation, etc. tables which have their ValidTimeStateFieldType properties set to something other than None.

        //Date effective services have properties added at the document level (the highest level in the service)
        //to indicate how you want to set the date effective entities.  
        //The properties are ValidTimeStateType, ValidAsOfDateTime, ValidFromDateTime and ValidToDateTime.
        //Either you can set those at the document level or you can set the date effective fields on each entity in the service
        //that is date effective. 

        //In the example code below, I call the read method to retrieve a customer and then I create a
        //new instance of a customer, set the date effective properties on the document according
        //to what they are on the retrieved customer and then update the CreditMax field on the customer.

        static void Main(string[] args)
        {
            string customer = "MK2";

            //ServiceReference1 is the name of ServiceReferance added to the project.
            ServiceReference1.CustomerServiceClient proxy = new ServiceReference1.CustomerServiceClient();
            var context = new ServiceReference1.CallContext() { Company = "USMF" };
            ServiceReference1.AxdCustomer foundCustomer = null;
            try
            {
                foundCustomer = proxy.read(context, readCritera(customer));
                Console.WriteLine("Read worked");
                updateCustomer(foundCustomer);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        } // end of Main


        private static ServiceReference1.EntityKey[] readCritera(string customerAccount)
        {
            ServiceReference1.AxdEntity_CustTable custTable = new ServiceReference1.AxdEntity_CustTable();
            ServiceReference1.EntityKey[] entityKeyList = new ServiceReference1.EntityKey[1];
            ServiceReference1.EntityKey key = new ServiceReference1.EntityKey();
            ServiceReference1.KeyField[] keyFields = new ServiceReference1.KeyField[1];
            ServiceReference1.KeyField keyField = new ServiceReference1.KeyField();
            keyField.Field = "AccountNum";
            keyField.Value = customerAccount;
            keyFields[0] = keyField;
            key.KeyData = keyFields;
            entityKeyList[0] = key;
            return entityKeyList;
        }

        private static void updateCustomer(ServiceReference1.AxdCustomer customer)
        {
            ServiceReference1.CustomerServiceClient proxy = new ServiceReference1.CustomerServiceClient();
            ServiceReference1.CallContext context = new ServiceReference1.CallContext();
            context.Company = "USMF";
            ServiceReference1.AxdEntity_CustTable custTable = customer.CustTable[0];

            ServiceReference1.AxdCustomer axdCustomer2 = new ServiceReference1.AxdCustomer();
            axdCustomer2.ValidTimeStateType = customer.ValidTimeStateType;
            axdCustomer2.ValidTimeStateTypeSpecified = true;
            axdCustomer2.ValidAsOfDateTime = customer.ValidAsOfDateTime;
            axdCustomer2.ValidFromDateTime = customer.ValidFromDateTime;
            axdCustomer2.ValidToDateTime = customer.ValidToDateTime;

            ServiceReference1.AxdEntity_CustTable custTableNew = new ServiceReference1.AxdEntity_CustTable();
            custTableNew._DocumentHash = custTable._DocumentHash;
            custTableNew.RecId = custTable.RecId;
            custTableNew.RecVersion = custTable.RecVersion;
            custTableNew.action = ServiceReference1.AxdEnum_AxdEntityAction.update;
            custTableNew.actionSpecified = true;
            custTableNew.CreditMax = custTable.CreditMax + 10;
            custTableNew.CreditMaxSpecified = true;
            custTableNew.CustGroup = custTable.CustGroup;
            axdCustomer2.CustTable = new ServiceReference1.AxdEntity_CustTable[1] { custTableNew };

            try
            {
                proxy.update(context, readCritera("MK2"), axdCustomer2);
                Console.Write("Worked");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed");
                Console.ReadLine();
            }
        }
    }
}

For Item /Product Refer 

https://blogs.msdn.microsoft.com/dynamicsaxscm/2011/07/06/product-item-data-management-services/



No comments:

Import and Export file from BLOB storage Account(Azure) in D365 F&O using X++

  Import and Export file from BLOB storage Account in D365 F&O using X++ Import: /// <summary> /// MKInventQualityOrderLineService...