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 (14)

  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 ?

  9. Sayantan says:

    Hello,
    We are trying to create a Sales Order in MS Nav (on premise 2013 R2) from Oracle Sales cloud.
    Going through the link https://msdn.microsoft.com/en-us/library/dd355398(v=nav.71).aspx , gave an impression that the Standard Sales Order creation page can be exposed as a SOAP service.
    Question 1: Is there any standard SOAP API available to create Sales Order OR we need to expose the SO creation page in NAV as a SOAP service?
    Question 2: In case the business validation to create Sales Order fail (credit check and other such validations) will this SOAP service send back an acknowledgment back to show the user the status of the attempt?
    Question 3: On the same lines, we want to create customers and items as well, is there any seeded SOAP service or need to create it, on similar lines?


    Sayantan

    1. FreddyDK says:

      The “standard” SOAP API is exposing the pages and codeunits as web services and call into these. Since NAV is a highly customized soloution we cannot create a standard API without ensuring that it can be customized, hence we use the pages.
      You can create your own API inside of NAV (codeunit or page) and use this as your interface.

  10. Sayantan says:

    Hello,
    We are trying to create a Sales Order in MS Nav (on premise 2013 R2) from Oracle Sales cloud.
    Going through the link https://msdn.microsoft.com/en-us/library/dd355398(v=nav.71).aspx , gave an impression that the Standard Sales Order creation page can be exposed as a SOAP service.
    Question 1: Is there any standard SOAP API available to create Sales Order OR we need to expose the SO creation page in NAV as a SOAP service?
    Question 2: In case the business validation to create Sales Order fail (credit check and other such validations) will this SOAP service send back an acknowledgment back to show the user the status of the attempt?
    Question 3: On the same lines, we want to create customers and items as well, is there any seeded SOAP service or need to create it, on similar lines?

    Sayantan

  11. Hi,

    I know this very old forem but im trying to use this solution in Nav 2016 and i followed the method and extended the service with the post function but its not apearing in the web service. Tried modifing the standard page too but its not apearing at all.

    Can anybody help please…

    So far i can create the sales order with lines fine and i can post it inside Nav without a problem..i need to post it through the c#.net code

    1. FreddyDK says:

      Haven’t tried that in a number of years. I will see if I get the time to try this again – I think it should still work.

      1. Charith Silva says:

        Hi Freddy,
        Thanks for the reply.
        It works and error was my fault. Now it creates the order but still not posting it may be permissions but i will figure it out.

        Thanks for the beautiful code …!!!!

Skip to main content