0

I want to export attachments of those Owners who belong to a certain Organisation.

I tried:

SELECT BodyLength FROM Attachment WHERE OwnerId.Organisation_Name__r = 'ExampleCompanyName', but got the following error:

ERROR at Row:1:Column:41 Didn't understand the relationship 'OwnerId' in the field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Can somebody help me out?

2

Assuming there is a field on User called Organisation_name__c

You have:

SELECT BodyLength 
  FROM Attachment 
  WHERE OwnerId.Organisation_Name__r = 'ExampleCompanyName'

The relationshipname from Attachment.OwnerId is called Owner, not OwnerId

so, you need:

SELECT BodyLength 
   FROM Attachment 
   WHERE Owner.Organisation_Name__c = 'ExampleCompanyName'

The TYPEOF (polymorphic) syntax is only relevant when the sobject can be owned by either users or queues - but Attachment can never be owned by a queue

2
  • Are you sure that OwnerId is not a polimorphic field on the Attachment object? And we shouldn't use TYPEOF there? SOAP API Developer guide says it's polimorphic and refers to calendar and user.
    – Alyona
    Aug 26 at 20:01
  • Good point -I was not aware of the Event relationship. TYPEOF is the way to go
    – cropredy
    Aug 26 at 20:05
1

Please try this

SELECT BodyLength,
  TYPEOF Owner
    WHEN User THEN Organisation_Name__c
  END
FROM Attachment
WHERE OwnerId IN
    (
    SELECT Id
    FROM User WHERE Organisation_Name__c = 'ExampleCompanyName'
    )

Source: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_polymorph_keys.htm

4
  • Hey, thank you for your quick reaction. Unfortunately, I get this error: No such column 'Organisation_Name__c' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
    – Lekü
    Jun 21 at 12:54
  • What is the exact name of the "Organisation Name" field on the user object? Are you sure it's Organisation_Name__c ?
    – Alyona
    Jun 21 at 13:17
  • I am sorry, it is Organisation_Name, but still, I get the error: BodyLength FROM Attachment WHERE Owner.Organisation_Name = 'ExampleCompanyName' ^ ERROR at Row:1:Column:41 No such column 'Organisation_Name' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. I don't get it because there is no entity called name.
    – Lekü
    Jun 21 at 13:21
  • This one will work, I recreated this in one of my orgs. I will change my first answer accordingly SELECT BodyLength, TYPEOF Owner WHEN User THEN Organisation_Name__c END FROM Attachment WHERE OwnerId IN ( SELECT Id FROM User WHERE Organisation_Name__c = 'ExampleCompanyName' )
    – Alyona
    Jun 21 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.