6

I am trying to load Attachment info along with Type and Id of its parent. I have used the following query before and it worked fine:

SELECT Id, Name, Parent.Id, Parent.Type FROM Attachment

But now for one of the Salesforce instances Parent attribute in response stays null for most of the attachments. I also tried doing something like this:

SELECT Id, Name, Parent.Id, Parent.Type FROM Attachment WHERE Parent.Type = 'Task'

And while filtering works correctly, information about parent is not returned. I am not sure what has changed and why this type of query doesn't work any more.

Any pointers what I should be looking at? Thanks!


Update: 22 May 2014

This is not an answer, but as a workaround in my particular case (just need to know Type of the Parent) it is possible to use ParentId value to figure out the Parent Type by getting list of all available SObjects (http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_describeGlobal.htm) and finding the one with correct keyPrefix.

6
  • 1
    This REST API - Attachments of Task/Event objects returning null parent fields suggests that the behaviour depends on the type of the parent object but doesn't offer any further explanation.
    – Keith C
    Commented May 21, 2014 at 7:39
  • Are you certain that all of the Parent objects have a "type"? For example, an Asset object is a valid Parent but has no Type. A Task object does have a type so your select will return that value. Also, what field are you trying to update with the Parent.Type? The attachment object has a "ContentType" but that is related to the attachment object, not the parent. Commented May 21, 2014 at 7:40
  • @KeithC this looks exactly like the issue I am facing. Unfortunately, it has no answer. I.e. I know that parents of these Attachments are Tasks and previously their attributes were returned as part of the query, but not anymore Commented May 21, 2014 at 14:48
  • If the REST API allows you to set an API version (and otherwise defaults to the current of 30) you might be able to get back the previous behaviour by specifying API 29.
    – Keith C
    Commented May 21, 2014 at 14:53
  • Tried bunch of different API versions (30, 29, 28, 26) and result is the same. But i've noticed one thing (independent of API version) Parent info is not returned whenever Attachment parent is a Task for other Parents (e.g. custom object - info is included in response). My idea was that perhaps these Tasks were deleted or archived. But no, these tasks are accessible by Id via API Commented May 21, 2014 at 16:52

1 Answer 1

3

In my experience, Type is NOT returnable as a field in a SOQL query on a polymorphic field (meaning you can't request it in a SELECT clause), but it IS filterable, meaning that you can use it in the WHERE clause. There seems to be a special provision for being able to filter by the SObject Type of a polymorphic field's target object via <PolymorphicField>.Type, but you won't get <PolymorphicField>.Type returned back to you on the rows retrieved.

In general, the reference for which fields are returnable / filterable across a polymorphic field is determined by the set of fields documented in the Name object --- for most objects, the only useful field available is Name, and sometimes FirstName and LastName (on Contact, Lead, and User), and then, when querying a polymorphic field for which one of the possible targets is User, you can get at a bunch of User fields (e.g. queries on Owner.IsActive, Owner.ProfileId will work)

However, you should be able to quickly determine the SObject Type of the Parent object via various workarounds. Since you can query on ParentId, you can check the key prefix of every record and go from there.

For example, from Apex you could do:

// Get a map of our returned Attachments 
// by their parent Object type
Map<Schema.SObjectType,List<SObject>> recordsByObjectType
    = new Map<Schema.SObjectType,List<SObject>>();

for (List<Attachment> atts : [SELECT Id, Name, ParentId FROM Attachment]) {
    for (Attachment a : atts) {
        Schema.SObjectType t = Id.valueOf(a.ParentId).getSObjectType();
        if (!recordsByObjectType.containsKey(t)) {
            recordsByObjectType.put(t,new List<SObject>());
        }
        recordsByObjectType.get(t).add(a);
    }
}

// Grab all Attachments to Account records
List<SObject> accountAttachments = recordsByObjectType.get(Account.SObjectType);
// Grab all Attachments to Task records
List<SObject> taskAttachments = recordsByObjectType.get(Task.SObjectType);
2
  • Here is an example where SOQL query on a polymorphic field returns info about parent: SELECT Id, Parent.Id, Parent.Type FROM Note. And like I mentioned in my question this type of query always worked previously. I am not using Apex, so I don't have any additional functions like this Id.valueOf(a.ParentId).getSObjectType(); to figure out correctly the type of any parent reference Commented May 22, 2014 at 18:20
  • Hii @zachelrath. I ran your code, but i am not able to find Parent.Name with respect to task. Can you please help me how to find task name
    – Radhe
    Commented Jan 23, 2019 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .