Wednesday, 19 August 2009

Item Import using code unit / xml import using Web service in Navision 2009

First, we design a xml port. Here is a sample code for xmlport.

OBJECT XMLport 50004 Items Import Port
{
OBJECT-PROPERTIES
{
Date=08/07/09;
Time=15:30:40;
Modified=Yes;
Version List=NPV1.5.2;
}
PROPERTIES
{
}
ELEMENTS
{
{ [{CEE36EFB-F847-4B7C-974F-F7B9C63E382F}]; ;Items ;Element ;Text }

{ [{B67698AD-EC88-41B0-88B3-B1DB9C0AC03D}];1 ;Item ;Element ;Table ;
VariableName=;
SourceTable=Table27 }

{ [{70719606-5836-422A-83CF-6CB40E8E8AAF}];2 ;ItemNo ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::No. }

{ [{3543F7A5-76E7-4A2C-9EEA-11FC97CCFE63}];2 ;EEStockCode ;Element ;Field ;
DataType=Code;
SourceField=::EEStockCode }

{ [{2F69AD9A-FEB9-4813-B5BD-355A2B2F7ACF}];2 ;Description ;Element ;Field ;
DataType=Text;
SourceField=::Description }

{ [{D34D9420-AAC2-4902-8AB5-C1ED51312C6F}];2 ;Description2 ;Element ;Field ;
DataType=Text;
SourceField=::Description 2 }

{ [{E8D83634-7EF3-47D9-A739-3318F6C7ABC4}];2 ;BUOM ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Base Unit of Measure }

{ [{E5A054A0-C126-43F5-A6EB-7C3691729018}];2 ;InventoryPostingGroup;Element;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Inventory Posting Group }

{ [{54A9B928-E0E1-48D6-8DE6-D8D76E375F1C}];2 ;UnitPrice ;Element ;Field ;
DataType=Decimal;
SourceField=::Unit Price }

{ [{535C3359-BD6D-492A-8454-9E3719A18186}];2 ;UnitCost ;Element ;Field ;
DataType=Decimal;
SourceField=::Unit Cost }

{ [{E5138A6C-B517-4531-AAC3-3247BE78BCF9}];2 ;VendorNo ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Vendor No. }

{ [{DFA265CE-0CF8-4EB3-9D91-283155D02DC8}];2 ;VendorItemNo ;Element ;Field ;
DataType=Text;
SourceField=::Vendor Item No. }

{ [{FACF3DD1-6275-4A32-832C-787131B433B3}];2 ;Blocked ;Element ;Field ;
DataType=Boolean;
SourceField=::Blocked }

{ [{25FE201D-995B-4889-B6D2-7835916594EC}];2 ;GenProdPostingGroup ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Gen. Prod. Posting Group }

{ [{319FCD24-1C99-49B4-A064-2061DE95DAA0}];2 ;TaxGroupCode ;Element ;Field ;
DataType=Code;
SourceField=::Tax Group Code }

{ [{8F96AA29-4D8D-43DA-872E-61ECC7B476C3}];2 ;VATProdPostingGroup ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::VAT Prod. Posting Group }

{ [{ACD9C70C-6878-41B0-9F43-08BDD8B7393E}];2 ;SalesUnitofMeasure ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Sales Unit of Measure }

{ [{07733FE9-02D4-4147-9C5B-EEA39121F283}];2 ;PurchaseUnitofMeasure;Element;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Purch. Unit of Measure }

{ [{F48F378B-F778-4BB1-B9C9-FE665367F3E7}];2 ;ManufacturerCode ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Manufacturer Code }

{ [{E5B75978-7945-4C7D-BD71-DB6B01B55743}];2 ;ItemCategoryCode ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Item Category Code }

{ [{D0EC2ADC-0A7A-4670-8248-B0D4AEF65C82}];2 ;ProductGroupCode ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Product Group Code }

{ [{0FE3334C-3E5C-4E1E-8207-349EF72B1820}];2 ;ServiceItemGroup ;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Service Item Group }

{ [{DB94D265-51C2-4586-B7C3-691956B004B9}];2 ;Critical ;Element ;Field ;
DataType=Boolean;
FieldValidate=No;
SourceField=::Critical }

{ [{A24C5A8B-9B83-441C-AC2E-E2CBA97D8B88}];2 ;GlobalDimensionCode1;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Global Dimension 1 Code }

{ [{7197C416-1209-4275-82C0-E0306239DA1E}];2 ;GlobalDimensionCode2;Element ;Field ;
DataType=Code;
FieldValidate=No;
SourceField=::Global Dimension 2 Code }

}
EVENTS
{
}
REQUESTPAGE
{
PROPERTIES
{
}
CONTROLS
{
}
}
CODE
{

BEGIN
END.
}
}

Now we design a code unit that does the job.


Documentation()
This code unit is designed to
- Import ITEMS
- Check If item exists
- Create Unit of measurement based on itemno, uom and conv factor.

Designed by :
- Kamran Shehzad
- Network Places Ltd
- 23 Feb 2009

Project Scope:
- EE Nav Interfacing

OnRun()
// to test run

Itemno:= '31635S1';
retVAL:= CreateUOM(Itemno,'PCS',1);

Itemno := '90-MIB5X0-G0EAY00Z';
status := CheckItemExists(Itemno);

MESSAGE('Does Item Exist : %1',status);
CLEAR(status);

IF NOT EXISTS('C:\XML_EE_ITEMS.XML') THEN BEGIN
ERROR('FILE NOT FOUND');
EXIT;
END;

CLEAR(DataInStream);

XmlFile.OPEN('C:\XML_EE_ITEMS.XML');
XmlFile.CREATEINSTREAM(DataInStream);
XMLPORT.IMPORT(123456792,DataInStream);
XmlFile.CLOSE;

MESSAGE('Import done Successfully.');

CheckItemExists(ItemNo : Code[50]) Status : Boolean

// CHECK WHETHER ITEM EXISTS OR NOT
Status := RecItems.GET(ItemNo);

ImportItems(XmlData : BigText) RetMsg : Text[1000]

// IF INPUT XML DOCUMENT HAS GOT ANY TEXT
IF XmlData.LENGTH > 0 THEN BEGIN
// READING INPUT TEXT INTO A STREAM AND WRITTING INTO STREAM AND THEN READING THE STREAM
TmpRecord.Blob.CREATEOUTSTREAM(XmlOutStream);
XmlData.WRITE(XmlOutStream);
TmpRecord.Blob.CREATEINSTREAM(XmlInStream);
Status := XMLPORT.IMPORT(50004, XmlInStream);

IF Status = TRUE THEN BEGIN
RetMsg := 'TRUE';
END ELSE BEGIN
// RETURN THE LAST ERROR MESSAGE
RetMsg := GETLASTERRORTEXT;
END;
END;

CreateUOM(ItemNo : Code[20];UOM : Text[30];ConversionFactor : Integer) RetVal : Text[30]

// CREATING UOM FOR ITEM IN NAVISION
ItemExists := CheckItemExists(ItemNo);

IF ItemExists = TRUE THEN BEGIN
UOMExists := RecItemUOM.GET(ItemNo,UOM);

IF UOMExists = FALSE THEN BEGIN
RecItemUOM.VALIDATE(RecItemUOM."Item No.", ItemNo);
RecItemUOM.VALIDATE(RecItemUOM.Code, UOM);
RecItemUOM.VALIDATE(RecItemUOM."Qty. per Unit of Measure", ConversionFactor);
RecItemUOM.INSERT;
COMMIT;
RetVal := 'TRUE';
END ELSE BEGIN
RetVal := 'UOM Already exists';
END;
END;

Now go to object designed and run form 810. Select object type as Code Unit, select the object and give a name to the service and check publish.


Now open IE and type http://localhost:7047/DynamicsNAV/WS/Services
This should list all enabled /published web services.

Now create a project in VS 2005/08. Add a webservice reference.

The code sample to access will be like this. ( the code is a part of application that I wrote for internal process so may not match exactly what you need). This is for illustration purpose only.

internal class ItemsDocument
{
#region Fields
NavInterface _navInterface;
EE_Items_Service.EE_Items_Service _eeService;

private const string _logApp = "Nav BL";
private int _requestStatus = 0;
#endregion

#region Internal methods
///
/// Processes the item document.
///

/// The nav interface .
/// The XML document.
///
internal int ProcessItemDocument(NavInterface navInterface, XmlDocument xmlDoc)
{
Logger.Write("Executing ItemsDocument.ProcessItemDocument()", _logApp);
_navInterface = navInterface;
XmlProcessor xmlProcessor = new XmlProcessor();
try
{
ProcessItemsInNavision(xmlProcessor.TransformXml(navInterface.TranslatorLocation, xmlDoc.InnerXml));
}
catch (Exception ex)
{
Logger.Write("EXCEPTION caught in ItemsDocument.ProcessItemDocument() exception : " + ex.Message, _logApp);
throw ex;
}
finally
{
if (_eeService != null)
_eeService = null;
}
return _requestStatus;
}
#endregion

#region Private Methods
///
/// Creates the new items.
///

/// The xml node to proces.
private void CreateNewItems(XmlNode node)
{
Logger.Write("Executing ItemsDocument.CreateNewItems()", _logApp);
try
{
string response = string.Empty;
string input = node.InnerXml.ToString();
response = _eeService.ImportItems(input);

if (response == "TRUE")
Logger.Write("Executing ItemsDocument.CreateNewItems() Item created successfully.", _logApp);
else
Logger.Write("Executing ItemsDocument.CreateNewItems() failed to create item.", _logApp);
}
catch (Exception ex)
{
Logger.Write("EXCEPTION caught in ItemsDocument.CreateNewItems() : " + ex.Message, _logApp);
Logger.Write("EXCEPTION caught in ItemsDocument.CreateNewItems() XML Data: " + node.InnerXml, _logApp);
// throw ex;
}
}

///
/// Checks the item exists.
///

/// The itemno.
///
private bool CheckItemExists(string itemno)
{
Logger.Write("Executing ItemsDocument.CheckItemExists() ItemNo:" + itemno, _logApp);
bool itemExists = false;
try
{
_eeService.CheckItemExists(itemno);
_requestStatus = itemExists ? 2 : 1;
Logger.Write("Executing ItemsDocument.CheckItemExists() Checking If Item Exists? " + itemExists, _logApp);
}
catch (Exception ex)
{
Logger.Write("EXCEPTION caught in ItemsDocument.CheckItemExists() :" + ex.Message, _logApp);
throw ex;
}

return itemExists;
}

///
/// Processes the items in navision.
///

/// The xmldocument
private void ProcessItemsInNavision(XmlDocument xd)
{
Logger.Write("Executing ItemsDocument.ProcessItemsInNavision()", _logApp);
try
{
InitializeItemsWebservice();
string xPath = string.Empty;
xPath = "Items/Item";
XmlNodeList nodeList = xd.GetElementsByTagName("Item");
string itemno = string.Empty;
string buom = string.Empty;
bool itemExists = false;
XmlProcessor xmlProcessor = new XmlProcessor();
XmlDocument xmlDocument = new XmlDocument();

if (nodeList.Count == 0)
{
Logger.Write("Executing ItemsDocument.ProcessItemsInNavision() Item node list is empty. ", _logApp);
_requestStatus = 3;
}
foreach (XmlNode node in nodeList)
{
XmlElement xmlElement = (XmlElement)node;

xPath = "ItemNo";
itemno = node.SelectSingleNode(xPath).InnerText;

xPath = "BUOM";
buom = node.SelectSingleNode(xPath).InnerText;

Logger.Write("Executing ItemsDocument.ProcessItemsInNavision() Item no : " + itemno, _logApp);
itemExists = CheckItemExists(itemno);

if (!itemExists)
{
xmlDocument = xmlProcessor.AddXmlToGivenElementTag("Item", node.InnerXml, false);
xmlDocument = xmlProcessor.AddXmlToGivenElementTag("Items", xmlDocument.InnerXml, true);
CreateNewItems(xmlDocument);
CreateUOM(itemno, buom);
_requestStatus = 1;

}
else
{
_requestStatus = 2;
}
}
}
catch (Exception ex)
{
Logger.Write("EXCEPTION caught in ItemsDocument.ProcessItemsInNavision() : " + ex.Message, _logApp);
throw ex;
}
}

///
/// Creates the UOM.
///

/// The itemno.
/// The buom.
private void CreateUOM(string itemno, string buom)
{
Logger.Write("Executing ItemsDocument.CreateUOM() For itemno : " + itemno, _logApp);
string retVal = string.Empty;
try
{
retVal = _eeService.CreateUOM(itemno, buom, 1);
if (retVal == "TRUE")
{
_requestStatus = 1;
Logger.Write("Executing ItemsDocument.CreateUOM() For itemno : " + itemno, _logApp);
}
else
{
throw new Exception(retVal);
}
}
catch (Exception ex)
{
Logger.Write("EXCEPTION caught in ItemsDocument.CreateUOM() : " + ex.Message, _logApp);
}
}

///
/// Iinitialize Items web service object.
///

private void InitializeItemsWebservice()
{
Logger.Write("Executing ItemsDocument.InitializeItemsWebservice()", _logApp);
try
{
_eeService = new EE_Items_Service.EE_Items_Service();
_eeService.UseDefaultCredentials = false;
_eeService.Credentials = NPHelper.GetCredentials();
_eeService.Url = _navInterface.WebServiceURL;
}
catch (Exception ex)
{
Logger.Write("EXCEPTION caught in ItemsDocument.InitializeItemsWebservice() : " + ex.Message, _logApp);
throw ex;
}
}
#endregion
}


This code is for illustration purpose only. Please use it to devise your logic only.

No comments:

Post a Comment