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



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

  • 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.

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.