Friday 19 January 2018

CRUD with Odata and Data entities in Dynamics 365 for operations - CREATE, UPDATE and DELETE Operations

You can use POSTMAN or FIDDLER for any of the testing. I personally like POSTMAN.

I assume that you have already gone through pages on READ operations. If not, please go through all that for better understanding of upcoming discussion on CREATE,UPDATE and DELETE operations in Dynamics 365 for operations(D365O).

Reading through data entities was fun, isn't it. But as a developer you must be willing to do more than just reading the data using Data entities.

Microsoft has done commendable work in whole data entity framework in D365O, it now can be used as point of entry for other operations such as create, update and delete. This a big relief considering AIF is practically a history for D365O.

Although there are other ways also to deal with data inside D365O, I find using Data entities the easiest.

I have created a custom table named 'BeaconService' for my testing purpose.

1. CREATE operations:

In Odata terminology, D365 CREATE operation can be referred as POST operation. Yes, you are right, that means you have to setup a method type to POST.

URL would look like this

https://*******devaos.cloudax.dynamics.com/data/Beaconservice

Set the Authorization code prefixed by 'bearer'

Then Set the body, keeping mandatory fields like BeaconID(in my case) and dataareaId as shown below.

{
      "EndTime": 0,
      "BeaconNum": "DA:AF:A9:FD:Y6:T4",
      "OfferId": "ST100006",
      "Description": "25% flat discount for your amazing audio experience",
      "EffectiveDate": "2015-12-25T12:00:00Z",
      "StartTime": 0,
      "dataAreaId": "usrt",
      "Category": "Headphones",
      "Offer": "headphones on discounted rate",
      "Brand": "Sony"
}



Now execute it (click on Send button if using POSTMAN).
This will give same output on the postman and error if any. Now you can go to the database or D365 screen to see the data.

2. UPDATE Operations


In Odata terminology, D365 UPDATE operation can be referred as PATCH operation.
i.e., you have to setup method type as PATCH.

URL would look like this
https://*******devaos.cloudax.dynamics.com/data/Beaconservice(BeaconNum='DA:AF:A9:FD:Y6:T4',dataAreaId='usrt')

Difference in the URL as you can see is, you'll have to include your primary key along with datareaid.

Now, Set the Authorization code prefixed by 'bearer'

Then Set the body as below, here you do not have to include primary keys

{
    "Category": "Earphones"
}


Now execute it (click on Send button if using POSTMAN).
This will leave the output screen blank if succeeded and error if any. Go ahead and verify the data.

3. DELETE Operations


In Odata terminology, D365 UPDATE operation can be referred as DELETE operation.
i.e., you have to setup method type as DELETE.

URL would look like this
https://*******devaos.cloudax.dynamics.com/data/Beaconservice(BeaconNum='DA:AF:A9:FD:Y6:T4',dataAreaId='usrt')

Here also, you'll have to include your primary key along with datareaid.

Now, Set the Authorization code prefixed by 'bearer'

Leave the body blank here. 


Then execute it (click on Send button if using POSTMAN).
This will leave the output screen blank if succeeded and error if any.
--------------------------------------------------------------------------------------

I am done here.
Thanks for your time!!!
Shoot the message if anything is missing or needed to be modified.

13 comments:

  1. Wonderful blog! Do you have any suggestions for aspiring
    writers? I'm planning to start my own website soon but I'm a little lost on everything.
    Would you advise starting with a free platform like Wordpress or go
    for a paid option? There are so many options out there that I'm completely
    confused .. Any recommendations? Thank you!

    ReplyDelete
    Replies
    1. Hey buddy, I am surely not a best person to advise on that. However, there are so many blogs to focus especially on writing.
      For starters, its always best to opt for free platforms and later on when you are comfortable and have enough readers, you can switch to paid versions for more convenience and options.

      Delete
  2. I am no longer positive the place you are getting
    your info, however great topic. I must spend some time learning much more or working out more.
    Thanks for excellent info I was searching for this information for my mission.

    ReplyDelete
    Replies
    1. I believe there would be some reason for your disagreement here. If you can point out few things, I can surely work on that for this and future topics...

      Delete
  3. How can I make it so that I can post multiple objects using Odata in D365FO? Seems like this crud only works with single object

    ReplyDelete
    Replies
    1. It works for multiple objects too...you just have to pass objects in an array[] with multiple opening'{' and closing'}' braces separated by comma(,)...
      If you have a more complex scenario, i'll suggest you to use custom services..

      Delete
    2. Hi, Thanks for such a wonderful post. Very very useful.
      I have tried sending multiple objects as you mentioned but could not succeed. Can you pls help me here. Below is the 'POST' request i used to create multiple customer groups.

      {
      [{
      "CustomerGroupId":"15","Description":"Wholesales customers","DefaultDimensionDisplayValue":"","ClearingPeriodPaymentTermName":"Net30","PaymentTermId":"Net30","IsSalesTaxIncludedInPrice":"No","WriteOffReason":"","TaxGroupId":"","dataAreaId":"usmf"
      },
      {
      "CustomerGroupId":"16","Description":"Wholesales customers","DefaultDimensionDisplayValue":"","ClearingPeriodPaymentTermName":"Net30","PaymentTermId":"Net30","IsSalesTaxIncludedInPrice":"No","WriteOffReason":"","TaxGroupId":"","dataAreaId":"usmf"
      }]
      }

      It produced the below error

      {"error": {
      "code": "",
      "message": "An error has occurred.",
      "innererror": {
      "message": "Invalid property identifier character: [. Path '', line 2, position 1.",
      "type": "Newtonsoft.Json.JsonReaderException",

      Delete
    3. I am sorry...I was extremely busy with work..... Are you able to use it for single object?..Unfortunately i don't have d365 access for now and so not able to verify it for you...

      Delete
    4. Hi,
      ok No problem :)
      It works for only a single object but not for multiple objects.

      Delete
  4. Hello
    How could I do to publish the token, so I will not be running the visual every time I want to run the webServices?
    Is there any way to do this?

    ReplyDelete
    Replies
    1. Hi, Indeed you can achieve this just by writing a small web application and later publish it on IIS locally to use within your organization or you can be more liberate and use Azure to publish it over internet....

      Delete
  5. Hello
    Could you please guide me to do what you tell me, I am trying to make the WebServices application not ask me for the token authentication but I could not find the solution
    I thank you very much if you can send me more information

    ReplyDelete
    Replies
    1. Hi michael, i am really sorry for the late reply. Hope you would have already got the solution to create a webservice in order to get token. If not, please send me an email

      Delete