Options

Problem with code in onInsert trigger when using webservices

andreaskellerandreaskeller Member Posts: 11
edited 2011-01-19 in NAV Three Tier
Hello Everybody

I have a project using Nav 2009 R2 where I expose a page as webservice. Everything works fine, the data can be inserted or updated.
Then I wanted to run some code in the onInsert trigger of the corresponding table. This code works fine when inserting through the classic Nav Client. When the code is triggered from the webservice the code doesn't funtion. When debugging the cs file of the table in Visual Studio I see that the code is processed, but I observed that not every field of the current record (the one to be inserted) is available. It seems as only the primary fields are available. The odd thing is that it still inserts the record correctly.
When I put this code in the onModify trigger of the table and in my c# code using the webservice I put an Update command after the Create command using the same object without modifying it, it works.

Has anybody observed something similar? Thanks for the feedback

Regards, Andreas

Comments

  • Options
    kinekine Member Posts: 12,562
    I assume that the webservice is working in similar way as user entering the values manually. The primary key is filled in as first and than insert is called. Than all other fields are validated and modify is called.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    kauffmannkauffmann Member Posts: 56
    I can confirm the assumption from kine. Pages that are used as web services work the normal as they do in the RTC. That is: insert a record after leaving the primary field.

    You can read more on this topic here: http://blogs.msdn.com/b/freddyk/archive ... d-rtm.aspx

    Quote from this post:
    Just to recap a couple of facts about Page based Web Services – we are using the pages and the code on the pages to work with sales orders, this means that we need to mimic the way the RoleTailored Client works, and the RoleTailored Client doesn’t create the order header and all the lines in one go before writing anything to the database. Instead what really happens is that once you leave the primary key field (the Order No) it creates the Order Header in the table. The same with the lines, they are created and then you type data into them, after which they are updated.

    This does not mean that de web service is doing the insert and update for you. You need to first call the Create method. This will return a reference to the inserted record (with the field Key filled in). Than you can use this variable to specify the other values and call Update function.
    var service = new Customer_Service();
    service.UseDefaultCredentials = true;
    
    var myCust = new Customer();
    service.Create(ref myCust);
    
    myCust.Name = "My Name";
    service.Update(ref myCust);
    
    A good programmer makes al the right mistakes
    My blog
  • Options
    andreaskellerandreaskeller Member Posts: 11
    Thanks for the replies. The execution of the code in the triggers makes more sense now. But to my knowledge you don't always have to call create first with the primary fields and then update the other fields. When you us FieldSpecified = true for non string fields a single create inserts the values into the database, just as it seems for code on triggers an update is necessary.
Sign In or Register to comment.