In this post I demonstrate two Simple Web Service examples of updating a NAV table from an external application using SOAP Web Services.
We have table “Person Test” with “Person List” and “Person Card” pages and 3 fields which look like this:
And we will create a C# Console Application in Visual Studio that will Modify the BirthDate of Person00001 and Insert 2 new persons. The modification will be done using the Card Page and the 2 inserts using the Card Page and a Codeunit. The final result will look like this:
So, let’s see how this is done.
First, I created a simple logic that automatically generates next Person ID when a new record is inserted into table “Person Test”. (this is not production code, it’s just a quick workaround for this example about getting an idea how NAV SOAP Web Services work):
OnInsert() IF PersonTest.FINDLAST THEN "Person Id" := GetNextPersonId(PersonTest."Person Id") ELSE "Person Id" := 'Person00001' ------------------------------------------------------------ LOCAL GetNextPersonId(LastId : Code[20]) : Code[20] FOR i := 1 TO STRLEN(LastId) DO BEGIN IF EVALUATE(IntId,FORMAT(LastId[i])) THEN FirstIntPosition := i; END; EVALUATE(IntId, COPYSTR(LastId,FirstIntPosition,STRLEN(LastId)-FirstIntPosition+1)); IntId += 1; EXIT(COPYSTR(LastId,1,FirstIntPosition-1) + FORMAT(IntId));
1) Modify existing record and Insert new record using a Nav Page published as a Web Service:
–> First, publish page “Person Card” as Web Service:
–> Create a new C# Console Application and add a reference to the published Web Service:
After you created a C# Console Application, copy the SOAP url of the “Person” web service and add it as Web Service Reference.
You should now see the reference in the Web References folder in solution explorer and if you double click on it you will see all classes, properties, methods etc:
–> Create function to modify existing record:
In program.cs file create new functions:
private static void UpdatePerson(string PersonID, DateTime BirthDate) { //update a Person by Id PersonWs.Person_Service ws = new PersonWs.Person_Service(); ws.Url = "http://YOURAPPLICATIONSERVER:SOAPWSPORTNUMBER/YOURSERVICENAME/WS/CRONUS%20Nederland%20BV/Page/Person"; ws.UseDefaultCredentials = true; PersonWs.Person Person1 = ws.ReadByRecId(PersonID); DateTime oldBirthDate = Person1.Date_Of_Birth; Person1.Date_Of_Birth = BirthDate.Date; Person1.Message_From_Webservice = "BirthDate Updated from C# app. Old value: " + oldBirthDate.Date.ToString("d") + ", New Value: " + Person1.Date_Of_Birth.Date.ToString("d"); ws.Update(ref Person1); }
–> Create function to insert a new record:
private static void CreatePerson(DateTime BirthDate) { //update a Person by Id PersonWs.Person_Service ws = new PersonWs.Person_Service(); ws.Url = "http://YOURAPPLICATIONSERVER:SOAPWSPORTNUMBER/YOURSERVICENAME/WS/CRONUS%20Nederland%20BV/Page/Person"; ws.UseDefaultCredentials = true; PersonWs.Person Person1 = new PersonWs.Person(); ws.Create(ref Person1); Person1.Date_Of_Birth = BirthDate.Date; Person1.Message_From_Webservice = "New record created from C# application using Page"; ws.Update(ref Person1); }
* notice that code from OnInsert of table is triggered and “Person Id” is automatically generated with the next id.
–> Call functions from Main method:
static void Main(string[] args) { //SOAP using Page DateTime BirthDate = new DateTime(2000, 7, 23); CreatePerson(BirthDate); DateTime BirthDate2 = new DateTime(1999, 02, 01); UpdatePerson("Person Test: PERSON00001", BirthDate2); }
2) Insert a new record using a Codeunit published as a Web Service:
–> Create XML Port PersonWebservice:
–> Create Codeunit PersonWebservice with a global function ImportNewPerson which receives as a parameter the XML Port PersonWebservice by reference. In the function call the XMLPort.Import method:
Documentation() OnRun() ImportNewPerson(VAR pPersonWebservice : XMLport PersonWebservice) pPersonWebservice.IMPORT;
–> Publish Codeunit as a Webservice and copy the SOAP Url.
–> Add new Web Reference in C# Console Application (same as you did at step 1). You should now see the new Web Reference:
–> Create function to insert a new person:
private static void CreatePerson2(DateTime BirthDate) { PersonWebServiceCodeunit.PersonWebService ws = new PersonWebServiceCodeunit.PersonWebService(); ws.Url = "http://YOURAPPLICATIONSERVER:SOAPWSPORTNUMBER/YOURSERVICEINSTANCENAME/WS/CRONUS%20Nederland%20BV/Codeunit/PersonWebService"; ws.UseDefaultCredentials = true; PersonWebServiceCodeunit.Persons persons = new PersonWebServiceCodeunit.Persons(); List<PersonWebServiceCodeunit.Person> personlist = new List<PersonWebServiceCodeunit.Person>(); PersonWebServiceCodeunit.Person person = new PersonWebServiceCodeunit.Person(); person.PersonID_fromXMLPort = "TEST"; person.DateOfBirth_fromXMLPort = BirthDate.Date.ToString(System.Globalization.CultureInfo.GetCultureInfo("en-US")); person.Message_fromXMLPort = "New record created from C# application using Codeunit"; personlist.Add(person); persons.Person = personlist.ToArray(); ws.ImportNewPerson(ref persons); }
–> call function from Main method:
static void Main(string[] args) { //SOAP using Page DateTime BirthDate = new DateTime(2000, 7, 23); CreatePerson(BirthDate); DateTime BirthDate2 = new DateTime(1999, 02, 01); UpdatePerson("Person Test: PERSON00001", BirthDate2); //SOAP using Codeunit DateTime BirthDate3 = new DateTime(2007, 02, 20); CreatePerson2(BirthDate3); }
* notice that even if we assigned the value “Test” to the “Person ID”, the OnInsert trigger of table has been run and generated and used the next id “PERSON00004” instead of value “Test”.
DOWNLOAD NAV SAMPLE FILES. The C# console application create yourself for practicing 🙂
This was a very simple web service example in NAV and I hope it will help you to get started with this. In case you want to learn more, I recommend the following posts related to this subject:
–> Use XML Port to Export data from NAV:
http://www.kauffmann.nl/2011/01/15/how-to-use-xmlports-in-web-services-1/
https://www.kauffmann.nl/2011/02/24/how-to-use-xmlports-in-web-services-2/
http://www.kauffmann.nl/2011/06/09/how-to-use-xmlports-in-web-services-3/
–> Nav Tech Days Web Service Examples:
http://www.kauffmann.nl/2015/11/26/nav-techdays-2015-the-web-service-examples/
https://www.youtube.com/watch?v=FLFc_U3oQlA&list=PLI1l3dMI8xlCSzgunk77qeG3wVErAcFg6
–> How to use OData Web Services to Modify Data in NAV:
https://community.dynamics.com/nav/b/navvideos/archive/2014/01/31/how-do-i-use-odata-web-services-to-modify-data-in-microsoft-dynamics-nav-2013-r2