Extending page Web Services (and creating a Sales Order again)


It has been working in the same way since NAV 2009, but I still get asked often how this works, so why not write up a quick post on this. I also realize that my prior post on how to create Sales Orders through Web Services was made very complex due to compatibility with NAV 2009.

This post only works in NAV 2009 SP1 and will show how to extend the Order page with a Post function and show how to Create a Sales Order from C# and post it.

Extending the page

First of all, we need to create a codeunit with the function, we want to add to the Order page.

image

Then we expose this codeunit with the same name as the page we want to extend, without putting a check in the published column

image

Note: All functions in the codeunit needs to have the first parameter be of the same type as the base record as the page you want to extend, else the page will no longer be available and you will get an error in the event log on the Service Tier.

Now taking a look at the WSDL in a browser will show us the new function as a first class citizen

image

and we can start using this.

Creating a Sales Order through Web Services

This might seem like repeating myself from a prior post, but that post did contain a lot of other information, which really isn’t necessary if you only target SP1.

Creating an order is a 3 step process:

  1. Create the Order Header
  2. Fill out the Order Header and create the Order lines
  3. Fill out the Order lines

Creating the Order header

Is really simple

Order_Service service = new Order_Service();
service.UseDefaultCredentials = true;

Order order = new Order();
service.Create(ref order);

After this we have a Order Number and an empty order – exactly like leaving the order No. field on the Sales Order Page.

Fill out the Order Header and create the Order lines

In this sample I will just fill out the Sell_to_Customer_No – a number of the other Order Header fields will be auto-updated when updating the order

order.Sell_to_Customer_No = "10000";

Then we need to create the Order lines – in this sample I will create 5. BTW – It is NOT trivial to add an order line after the fact, so I suggest you add the needed number of lines in one go:

order.SalesLines = new Sales_Order_Line[5];
for (int i = 0; i < 5; i++)
    order.SalesLines[i] = new Sales_Order_Line();
service.Update(ref order);

Fill out the Order lines

In this sample, I will just create 5 lines with green ROME guest chairs.

for (int i = 0; i < 5; i++)
{
    order.SalesLines[i].Type = OrderPageRef.Type.Item;
    order.SalesLines[i].No = "1960-S";
    order.SalesLines[i].Quantity = 1;
}
service.Update(ref order);

That’s it – the order is created and you can find it in the Client.

And at last… – Post the order

Having created the order, now it is time to post the order

service.PostOrder(order.Key);

As you can see, the function takes a Record parameter, but we give it a Key.

Note, that calling a function does not make an implicit Update – meaning that if you have done changes to the record in C# and call the function, you will get an error when calling update later. Reason – the PostOrder function has changed the record and will tell you that the record was changed by another user.

After calling a function on a page you will need to Re-read the record if you need to do more work.

That’s it

Enjoy

Freddy Kristiansen
PM Architect
Microsoft Dynamics NAV

Comments (8)

  1. romantiq77 says:

    I am having an issue here.. at runtime, I have an error at the following point:

    order.SalesLines = new Sales_Order_Line[5];

    for (int i = 0; i < 5; i++)

       order.SalesLines[i] = new Sales_Order_Line();

    service.Update(ref order); <—

    The runtime error is:

    The Sales Line does not exist.   Identification fields and values:  Document Type=’Order’,Document No.=’PNDOC10666′,Line No.=’10000′

    I am running 2009 SP1… do you have any idea what is wrong?  I am quite stumped!

  2. romantiq77 says:

    Never mind!  It was some dodgy code on the insert trigger of the Sales Line table..

  3. FJPerez says:

    Thanks for this wonderfull post!!!!

    Hello i have a problem when in service.create line this says than cant't find the class.

    I dont know what's wrong!

    Can you help me?

    Thanks from Madrid!!

  4. FJPerez says:

    Hello again,

    I've fixed the problen it was that i need to replace the text in the connection with the name comany at webconfig.

    Now my problem is that i insert in the header but nothing in the lines… i don't know what happens.

    If anybody can help me wil be nice 😉

    Thanks anyway!

  5. Nemo says:

    I have tried to create sales order by following the way that you have described in blog. Yes, I can create sales order in ONE GO. but when i tried to create some additional sales lines, it cannot be created.

    My simple question Is it possible or not ? and how ?

    I really apprecite to hear you soon.

    Thank you.

    Nemo

  6. cc says:

    How to Add a line to an existing Sales Order

  7. Julien says:

    Hello,

    Thanks for this post !!

    I'm able to create an order, but I can't add lines.

    what object do you use as Sales_Order_Lines when you do:

    order.SalesLines = new Sales_Order_Line[5];

    I have:

    Dim cde As New CommandeServiceRef.Commande_vente

    MonServiceCommande.Create(cde)

    ok until there

    Dim ligne As New CommandeLigneServiceRef.Commande_vente_ligne

    ligne.Type = CommandeLigneServiceRef.Type.Item

    ligne.No = MyItem.No

    ligne.Quantity = 1

    ok until there

    cde.SalesLines=New CommandeLigneServiceRef.Commande_vente_ligne(5)

    this doesn't work …

    thank you for your help

  8. Julien says:

    After importing My_Project.CommandeServiceRef, I can reach Sales_order_line

    but the code:

    cde.SalesLines = New Sales_Order_Line(5)

    doesn't work …

    too many arguments for command new

    any idea ?