Limiting related results to just an array of IDs

Currently is you specify a relationship in the related param of a GET call you get back an array full of objects in that relationship. Is there a way to limit the related results to just an array of the related IDs?

Current

{ 
  "record": [
    {
      "id": 1,
      "name": "Oil Change",
      "revision": "IR",
      "PackageCalledOns_by_packageId": [
        {
          "id": 2,
          "packageId": 1,
          "childId": 1,
          "createdUTC": "2014-08-25T17:59:01.497",
          "createdBy": null
        },
        {
          "id": 3,
          "packageId": 1,
          "childId": 2,
          "createdUTC": "2014-08-25T17:59:01.497",
          "createdBy": null
        },
        {
          "id": 4,
          "packageId": 1,
          "childId": 3,
          "createdUTC": "2014-08-25T17:59:01.497",
          "createdBy": null
        },
        {
          "id": 5,
          "packageId": 1,
          "childId": 4,
          "createdUTC": "2014-08-25T17:59:01.497",
          "createdBy": null
        }
      ]
    }
  ]
}

Preferred

{ 
  "record": [
    {
      "id": 1,
      "name": "Oil Change",
      "revision": "IR",
      "PackageCalledOns_by_packageId": [ 2, 3, 4, 5 ]
    }
  ]
}

Here is a server side script to run on the post_process. It adds the number of related Contacts to the Account object. It is a pretty easy modification to create an array of the child IDs instead… hope this helps

if (event.response.record.length) {
_.each(event.response.record, function(record, index, list) {
var account_id = record.Id;
var filter_data = {fields: ‘id’, filter: “AccountID=” + account_id};
var contact_data = platform.api.get(‘db/Contact’, filter_data);
record[‘total_contacts’] = contact_data.record.length;
});
}

Another option is to add a parameter to the API call, like this with Bill’s example.

/rest/db/Account/?ids=1&related=Contacts_by_AccountId&Contacts_by_AccountId.fields=id

That last parameter is the <relationship_name>.fields which behaves just like the “fields” parameter.

Some documentation is here, we’re currently cleaning it up.

@benbusse I was not able to get the REST method to work. My endpoint was

/rest/RDSdb/Orders?ids=1&order=orderNumber%20desc&related=OrderCalledOns_by_orderId.fields=id

I tried from both Swagger and a REST client app. Both sent back a response of:

{
   error: [1]
      0:  {
         context: null
         message: "Invalid relationship 'OrderCalledOns_by_orderId.fields=id' requested."
         code: 400
      }
}

@billappleton thanks I’ll give it a shot as soon as I have a chance!

EDIT
@billappleton it worked like a charm! thanks!

if (event.response.record.length) {
    _.each(event.response.record, function(record, index, list) {
        var orderId = record.id;
        var filter_data = {fields: 'id', filter: "orderId=" + orderId};
        var calledOnData = platform.api.get('RDSdb/OrderCalledOns', filter_data);
        
        var calledOnArray = [];
        _.each(calledOnData.record, function (record, index, list) {
            calledOnArray.push(record.id);    
        });
        
        record['called_ons'] = calledOnArray;
    });
}

@cmculler, the fields operator is a different parameter than the “related” parameter value. Try this…

/rest/RDSdb/Orders?ids=1&order=orderNumber%20desc&related=OrderCalledOns_by_orderId&OrderCalledOns_by_orderId.fields=id

It makes sense if you think about including multiple relationships in a single query.

@leehicks thanks for the clarification! That worked for the REST API and it’s good to know how to use those additional params.

Unfortunately the format still isn’t quite what I need. I think I’m going to have to either stick to the server side scripts across all of my relationships or customize my Ember-Data adapter to transform the response data prior to sending it back to the client.