Please Help me...

Account sObject is Parent of Attachment. Now I want to bring Read-only Field Custom field (isReadOnly__c).

SELECT Id, Name, Account.isReadOnly__c FROM Attachment

How It Gets ?

Thanks in Advance


| improve this question | | | | |

You cannot do it like that.

What you need to do is gather all your attachment records and store the parentID

Then you need to create a collection of ParentIDs to Records.

Now you can manipulate them.

For Example:

Map<ID,Account> accountMap = New Map<ID,Account>();
Attachment[] files = [Select ID, ParentID From Attachment]; //You will need to filter as appropriate

for(Attachment file : files){
     if(file.ParentId.getSObjectType() == Account.SobjectType)

accountMap = New Map<ID<Account>([Select ReadOnly__c From Account Where ID IN :accountMap.keySet()];

Now you have a map of accounts and can cycle through the attachments and get the appropriate account record and check readOnly__c

| improve this answer | | | | |
  • 3
    if(file.ParentId.getSObjectType() == Account.SobjectType) avoids using describes unnecessarily. – sfdcfox Dec 23 '14 at 19:26

There is a feature you can use in your own orgs, via a pilot feature, called TYPEOF, that allows you to get a parent field for a concrete type.

That query looks like this:

SELECT Id, Name, 
  TYPEOF Parent WHEN Account THEN isReadOnly__c END
FROM Attachment

To enable this feature, you currently need to contact Support. Certain limitations may apply to this feature.

| improve this answer | | | | |
  • when printing the query or runing it in the dev console, the fields in this case isReadOnly__c doesnt show up in the result, how can you access that record ".account.isreadonly__c" value? – manza Dec 20 '19 at 2:46
  • 1
    @manza The developer console does not support TYPEOF (part of the "limitations" surrounding this feature right now). Try using Workbench to see a working example. – sfdcfox Dec 20 '19 at 3:22
  • @sfdxfox thank you, that have sense i read the docs, but i probably miss that part of developer console mot been supported, – manza Dec 20 '19 at 3:52

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.