Microsoft Dynamics Nav: Adding a Record through Web Services

Microsoft Dynamics Nav Web Services can not only read data, but they can also write data to a Microsoft Dynamics Nav database. This example demonstrates adding a record into a Microsoft Dynamics Nav database through Web Services using an XMLPort.

Microsoft Dynamics Nav: Adding a Record through Web Services

1. Create and XMLPort for transferring the data. Note: For this example the MaxOccurs property of the element should be set to ‘One’ to set a “one record limit”. It is possible to set multiple records

1.Create and XMLPort for transferring the data.

2. Create a CodeUnit and add functions that will be used to through the Web Service

GetCustomers(CustomerCode : Code[10];VAR CustXML : XMLport GetSetCustomer) Customer.SETRANGE("No.", CustomerCode); CustXML.SETTABLEVIEW(Customer); SetCustomers(VAR CustXML : XMLport GetSetCustomer) : Integer // Return 1 if import was successful IF CustXML.IMPORT THEN EXIT(1) ELSE EXIT(0);

3. Publish the CodeUnit as a Web Service

3.Publish the CodeUnit as a Web Service

4. Create a .NET Application and register the Microsoft Dynamics Nav Web Service

5. Create the code that interacts with the registered Web Service (Note: This code is for basic demonstration only and additional coding practices should be applied for production code)

        GetSetCustomers_Binding ws;

        private void Form1_Load(object sender, EventArgs e)
        {
            ws = new GetSetCustomers_Binding();
            ws.UseDefaultCredentials = true;
            ws.Url = "http://localhost:7047/DynamicsNAV/WS/CRONUS%20USA,%20Inc./Codeunit/GetSetCustomers";

        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            Customer cust = new Customer();
            Customers cs = new Customers();
            // in nav the XML should be set to maxoccurence of 1 so that we only return one 
            //customer
            ws.GetCustomers(txtNo.Text, ref cs);

            cust = cs.Customer[0];
            txtName.Text = cust.Name;
            txtPhone.Text = cust.Phone_No_;
            txtContact.Text = cust.Contact;
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            Customers cs = new Customers();
            Customer cust = new Customer();
            List<Customer> custlist = new List<Customer>();

            cust.No_ = txtNo.Text;
            cust.Name = txtName.Text;
            cust.Phone_No_ = txtPhone.Text;
            cust.Contact = txtContact.Text;
            custlist.Add(cust);
            cs.Customer = custlist.ToArray();

            if (ws.SetCustomers(ref cs) == 0)
            {
                MessageBox.Show("No Good.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };
        }

The sample application referenced in this post can be downloaded >>>here<<<.

View the following posts for additional examples:

Microsoft Dynamics Nav: Retrieving a set of Records through Web Services

Microsoft Dynamics Nav: Using an XMLPort as a .NET DataSource

1 thought on “Microsoft Dynamics Nav: Adding a Record through Web Services

Leave a Comment