[SalesForce] Get Asset via REST API with only a Account Id

Given a list of Account ID's, I want to get all assets associated with them.

So far, I worked out I can get the account by calling :

/services/data/v32.0/sobjects/Account/{accountId}

Seems simply enough. However this object does not contain assets (Or even Asset ID's) in the response.

I then called

services/data/v32.0/sobjects/

In the hope that this would give me more info on how to get the assets associated with an account. It does infact return some documentation on possible ways to get individual assets.

/services/data/v32.0/sobjects/Asset/{ID}

But the {ID} is infact the AssetID, not the AccountID.

Is there any way, given an accountID, to get all assets associated with them via REST (Not SOAP, SOQL etc).

Best Answer

You can execute SOQL via the REST API:

/services/data/v36.0/query/?q=SELECT+Name+FROM+Asset+WHERE+AccountId+=+'001000000000AAA'


Just for fun completeness, it is nigh trivial to use Apex REST and be truly RESTful. Better to avoid Apex though, as you indicate is your intention.

@RestResource(urlMapping='/AssetsByAccount/*')
global class AccountAsset
{
    @HttpGet
    global static List<Asset> getChildren()
    {
        return [
            SELECT Name FROM Asset WHERE AccountId = 
            :RestContext.request.requestUri.substringAfterLast('/')
        ];
    }
}

/services/apexrest/AssetsByAccount/001000000000AAA

Related Topic