Use fields dynamicly in webservices

KaiawaiaKaiawaia Member Posts: 6
Hello,

is it possible to access fields dynamicly? For example, I want to write a
value to a field. But I know which field this is only at runtime. I think for something like
this example shows:

account testContact = new Contact();
testAccount.Fields["City"] = "Berlin";

There is no "Fields" in Contact. That's only an example to show
what I mean. Is there any possiblility to use fields in this way?

Thanks,
Kai

Comments

  • krikikriki Member, Moderator Posts: 9,110
    You can use recordreference and fieldreference to do that.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ara3nara3n Member Posts: 9,256
    what Alain means is that you can use a publish a CU function and pass the parameters to update a field at runtime.
    In that function you can use recordref and fieldref variables
    You cannot do this through publishing pages as webservices.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • KaiawaiaKaiawaia Member Posts: 6
    Thanks for your replies. I will try with the references. That doesn't sound bad.
  • Ravi_ThakkarRavi_Thakkar Member Posts: 392
    Hello,
    ara3n wrote:
    what Alain means is that you can use a publish a CU function and pass the parameters to update a field at runtime.
    In that function you can use recordref and fieldref variables
    You cannot do this through publishing pages as webservices.
    We can use the Codeunit logic as here specified. But, as the Microsoft Help on Web Services, transactions done using Codeunit functions will require Concurrency to be managed manually.
    So, by using this method, Concurrency may become an issue.
    Ravi_Thakkar
    Ahmedabad, Gujarat, India
    E Mail : ravi.thakkar@hotmail.com
  • ara3nara3n Member Posts: 9,256
    Concurrency is always an issue, and every developer who developers in NAV needs to manage it in code. If two people are modifying the same record, the user should get an error that somebody else has modified the record.

    The bigger issue is managing transactions. Each call to a cu is a separate transaction. so you need to pay attention how transaction are committed and rolled back.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    If you want to assign fields dynamically from c# - you can use reflection to do that, in fact you can have web service reference be totally dynamic as explained here:

    http://blogs.msdn.com/freddyk/archive/2009/04/27/dynamic-references-to-nav-page-web-services-in-c.aspx

    If you just want to assign a field through reflection you would do:
    PropertyInfo name = typeof(Customer).GetProperty("Name"); 
    name.SetValue(customer, "new name", null);
    

    I use that in a number of places - although the above code is just written from the top of my head, so there could be spelling mistakes.
    Freddy Kristiansen
    Group Program Manager, Client
    Microsoft Dynamics NAV
    http://blogs.msdn.com/freddyk

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • Ravi_ThakkarRavi_Thakkar Member Posts: 392
    Hello Freddy,

    Really it's a nice blog which you have posted.
    Really it will be helpful to many. =D>

    Thanks.
    Ravi_Thakkar
    Ahmedabad, Gujarat, India
    E Mail : ravi.thakkar@hotmail.com
  • rajubodkhe28rajubodkhe28 Member Posts: 4
    hi Ravi,

    If you did fields dynamicly in webservices. then can u help me to insert and update sales order dynamicaly using C#.net Reflection namespace. I am able to read sales orders but not able to insert and update. i am not able to setvalue of propertyinfo filed and pass that property filed to insert method or update method or the web service

    if you have any code sample the kindly relply to this post

    :?

    Thanks and Regard
    Raju
Sign In or Register to comment.