Monday, 1 October 2018

Custom service to import product master using WCF service AX 2012.

In this article, we will see how to import a product into Dynamics AX 2012 using WCF custom service. Use this approach only you need to extream customization otherwise, we can use the standard DIXF Product master for details you can check below link.

 "How to import items master data using Data Migration Framework"


Coming to Custom service to import product master. 

1. We need to create a service class which extends SysOperationServiceBase
class MK_ItemMasterService extends SysOperationServiceBase
{
}

2. Add Entry point method which takes contract class as parameter. 

[SysEntryPointAttribute(true)]
public ItemId createProduct(MK_ItemImportContract _itemImport)
{
      InventTable            inventTable,ret;
    try
    {
        ttsBegin;
        inventTable = InventTable::find(_itemImport.parmProductNumber(),true);
        if (inventTable.RecId)
        {
            //update product
            this.update(_itemImport,inventTable);
        }
        else
        {
            //create new product
            ret = this.create(_itemImport);
        }// end of else
        ttsCommit;
    }
    catch
    {
        return "";
    }

    return ret.itemid;

}

3. Now we can create a Data contract class as below.

[DataContractAttribute]
public class MK_ItemImportContract
{
    Str DisplayProductNumber; // = "Bulb60W1",
    //ProductType = AxdEnum_EcoResProductType.Item,
    str SearchName;// = "Bulb60W1"
    str Name;
    str ProductNumber;
    str StorageDimGroup;
    str ProductDimGroup;
    str TrackingDimGroup;
}

4. Add Data member attribute method for all the variables declared in the class like below.

[DataMemberAttribute]
public str parmStorageDimGroup(Str _StorageDimGroup = StorageDimGroup)
{
    StorageDimGroup = _StorageDimGroup;
    return  StorageDimGroup;
}

5. Now it is the time to create a service 

Goto AOT\Services\ and right click and add new 
Name : MK_ItemMasterService
Class: Service class name (MK_ItemMasterService)
Description: "Product Import Service".
External Name: 

6. Add the Operations to Service.
By Expanding the service and right click on Operations and add
Select the method "CreateProduct" and click ok.

7. Create Service Group 
\Service Groups\MK_ItemMasterServices
Name: MK_ItemMasterServices
AutoDeploy: Yes
Description:

add the above-created service (step 6) to this service group.

8. Deploy the service Group.
Right click on Service Group and deploy.
This will create an Inbound service. 
Copy the WSDL URL.

Now it's time to test the above service. For that, we will create a C# console application.

9. Create a C# console application 
Right click on the references and add a service reference 
Past the URL copied from step 8.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MKProductImportService.ServiceReference1;

namespace MKProductImportService
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MK_ItemMasterServiceClient client = new MK_ItemMasterServiceClient();
                CallContext context = new CallContext();
                context.Company = "CW01";
                Console.WriteLine("Object Created Success!");

                MK_ItemImportContract procuct = new MK_ItemImportContract();
                procuct.parmDisplayProductNumber = "T00003";
                procuct.parmName = "Test3 Procut WCF";
                procuct.parmProductNumber = "T00003";
                procuct.parmStorageDimGroup = "WL";
                procuct.parmTrackingDimGroup = "BS";
                procuct.parmSearchName = "Test3 WCF";

                Console.WriteLine("Lines added Success!");
                string itemid = client.createProduct(context, procuct); // call the create product method

                Console.WriteLine("Item Created:" + itemid);

                Console.WriteLine("Success! !!!");

            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.Message);
            }
            //Console.ReadLine();
        }
    }
}

10. Build and run 

........................


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...