Monday, 7 August 2017

CRUD with Odata and Data entities in Dynamics 365 for operations - READ Operations-2

....Contnd from part 1

1. SORTING:
you can sort the data so as to display data in ascending or descending order as below using keyword $orderby

https://********devaos.cloudax.dynamics.com/data/Beaconservice?$orderby=BeaconNum desc

and the result will look like as below. However, for sorting multiple fields can be used as
https://********devaos.cloudax.dynamics.com/data/Beaconservice?$orderby=BeaconNum desc,OfferId asc


{
    "@odata.context": "https://*****devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.etag": "W/\"JzEsNTYzNzE0NDU3OCc=\"",
            "Description": "Beacon 3",
            "dataAreaId": "usrt",
            "BeaconNum": "345678912",
            "EffectiveDate": "2017-05-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand3",
            "StartTime": 0,
            "Category": "Category3",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer3"
        },
        {
            "@odata.etag": "W/\"Jzg0ODAyMzczOCw1NjM3MTQ0NTc3Jw==\"",
            "Description": "Beacon 2",
            "dataAreaId": "usrt",
            "BeaconNum": "234567891",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus": "InActive",
            "Brand": "Brand2",
            "StartTime": 0,
            "Category": "Category2",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "offer2"
        },
        {
            "@odata.etag": "W/\"JzE0NTk5MjkxNjcsNTYzNzE0NDU3Nic=\"",
            "Description": "beacon 1",
            "dataAreaId": "usrt",
            "BeaconNum": "123456789",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand1",
            "StartTime": 0,
            "Category": "Category1",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer1"
        }
    ]
}

2. FILTER(Get selective records):
You can filter out the records based on selection criteria to get relevant data. keyword would be $filter
https://********devaos.cloudax.dynamics.com/data/Beaconservice?$filter=BeaconNum eq '345678912'



{
    "@odata.context": "https://********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.etag": "W/\"JzEsNTYzNzE0NDU3OCc=\"",
            "Description": "Beacon 3",
            "dataAreaId": "usrt",
            "BeaconNum": "345678912",
            "EffectiveDate": "2017-05-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand3",
            "StartTime": 0,
            "Category": "Category3",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer3"
        }
    ]
}

If provided field is of type Int, then you don't have to use singe quotes(').
However, there is a trick if you want to use a filter on Enum field. you need to have a AOT name of an used enum. In my case AOT name of an enum is BeaconStatus with values "Active","Inactive" and "Locked".
enum field is "BeaconStatus".
After fieldname, value have to be passed using "Microsoft.Dynamics.DataEntities" as below.
If you have read my previous post, full metadata provides us the type to use. In our case it was "Microsoft.Dynamics.DataEntities.BeaconStatus"
I have to filter out the records where BeaconStatus is 'InActive'.

https://********devaos.cloudax.dynamics.com/data/Beaconservice?$filter=BeaconStatus eq Microsoft.Dynamics.DataEntities.BeaconStatus'InActive'

{
    "@odata.context": "https://*********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.etag": "W/\"Jzg0ODAyMzczOCw1NjM3MTQ0NTc3Jw==\"",
            "Description": "Beacon 2",
            "dataAreaId": "usrt",
            "BeaconNum": "234567891",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus": "InActive",
            "Brand": "Brand2",
            "StartTime": 0,
            "Category": "Category2",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "offer2"
        }
    ]
}

To Filter out records based on the date or datetime field, you need to provide it in Json standards as 2017-01-08T12:00:00Z https://*********devaos.cloudax.dynamics.com/data/Beaconservice?$filter=EffectiveDate eq 2017-05-08T12:00:00Z
Output will be as below
{
  "@odata.context":"https://********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
  "value":[
    {
      "@odata.etag":"W/\"JzEsNTYzNzE0NDU3OCc=\"",
   "Description":"Beacon 3",
   "dataAreaId":"usrt",
   "BeaconNum":"345678912",
   "EffectiveDate":"2017-05-08T12:00:00Z",
   "BeaconStatus":"Active",
   "Brand":"Brand3",
   "StartTime":0,"Category":
   "Category3","EndTime":0,
   "Offer":"",
   "OfferId":"Offer3"
    }
  ]
}

3. SELECTED FIELDS:
You might face a scenario where you don't need all fields to be displayed. for that you can use $select

https://*********devaos.cloudax.dynamics.com/data/Beaconservice?$select=BeaconNum,Description,OfferId

Result will look like
{
    "@odata.context": "https://******devaos.cloudax.dynamics.com/data/$metadata#Beaconservice(BeaconNum,Description,OfferId)",
    "value": [
        {
            "@odata.etag": "W/\"JzE0NTk5MjkxNjcsNTYzNzE0NDU3Nic=\"",
            "BeaconNum": "123456789",
            "Description": "beacon 1",
            "OfferId": "Offer1"
        },
        {
            "@odata.etag": "W/\"Jzg0ODAyMzczOCw1NjM3MTQ0NTc3Jw==\"",
            "BeaconNum": "234567891",
            "Description": "Beacon 2",
            "OfferId": "offer2"
        },
        {
            "@odata.etag": "W/\"JzEsNTYzNzE0NDU3OCc=\"",
            "BeaconNum": "345678912",
            "Description": "Beacon 3",
            "OfferId": "Offer3"
        }
    ]
}
4.Get selective number of records(TOP):
There might be scenarios where you  might want to get only few top records. $top keyword can be used for this purpose as below
https://*********devaos.cloudax.dynamics.com/data/Beaconservice?$top=1

{
    "@odata.context": "https://********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.etag": "W/\"JzE0NTk5MjkxNjcsNTYzNzE0NDU3Nic=\"",
            "Description": "beacon 1",
            "dataAreaId": "usrt",
            "BeaconNum": "123456789",
            "EffectiveDate": "2017-01-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand1",
            "StartTime": 0,
            "Category": "Category1",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer1"
        }
    ]
}

5. Skip some records from result(SKIP)
If you want to skip some records for selection using $skip
https://**********devaos.cloudax.dynamics.com/data/Beaconservice?$skip=2



{
    "@odata.context": "https://*********devaos.cloudax.dynamics.com/data/$metadata#Beaconservice",
    "value": [
        {
            "@odata.etag": "W/\"JzEsNTYzNzE0NDU3OCc=\"",
            "Description": "Beacon 3",
            "dataAreaId": "usrt",
            "BeaconNum": "345678912",
            "EffectiveDate": "2017-05-08T12:00:00Z",
            "BeaconStatus": "Active",
            "Brand": "Brand3",
            "StartTime": 0,
            "Category": "Category3",
            "EndTime": 0,
            "Offer": "",
            "OfferId": "Offer3"
        }
    ]
}

6.COUNT
https://*******devaos.cloudax.dynamics.com/data/Beaconservice/$count


3

Will be discussing about use of logical operators with Data entities on my Next blog on READ operations.

3 comments:

  1. Has anyone here used Dynamics Stream for their Microsoft Dynamics 365 implementation? I'm looking for a partner in the UK to help us get set up and wondering about experiences with them. They seem to offer the full range of services (implementation, migration, customization) which is appealing, but it would be great to hear from others who have worked with them.

    ReplyDelete