1

I want to select the fields name__c and description__c from my CustomObject along with the attachments from the attachment object.

Can I do this with my query below? What should i write in my inner select clause ParentId = Id?

public List <CustomObject__c> getTestList() {        
List <CustomObject__c> TestMSList = [SELECT Name__c, Desrciption__c, (SELECT Name FROM Attachment WHERE ParentId = Id) FROM CustomObject__c];
return TestList;        
}
1
  • You don't need to put where clause in inner query because inner query used for the only on Parent record to get the related child records, In your case No need to put ParentId = Id because you are using inner query and main query is your parent.
    – Nihar
    Jun 19, 2017 at 5:58

4 Answers 4

1

There are two methods to achieve your task.

  1. Utilize StandardController controller provided by salesforce. This allows you to <apex:relatedList> which displays the relatedlist of any particular record. For example, if you're implementing standardcontroller for Account then for a particular record of Account it will display it's related list such as Contacts, Opportunities etc.

  2. Fetch the record along with it's related list. Just the way as @AlexanderBerehovskiy highlighted.

Method 1

<apex:page standardController="Account" extensions="Acc">   
    <apex:form>
        <apex:relatedList list="CombinedAttachments" >
             <apex:facet name="header">Notes and Attachments</apex:facet>
         </apex:relatedList>
    </apex:form>    
</apex:page>

Method 2

VF Page:

<apex:page standardController="Account" extensions="Acc">
    <apex:form> 
        <apex:pageblock >
            <apex:pageBlockSection >
                <apex:inputField value="{!myAcc.name}"/>
                <apex:inputField value="{!myAcc.id}"/>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!myacc.attachments}" var="attach">
                <apex:outputField value="{!attach.Id}"/>
                <apex:outputField value="{!attach.Name}"/>
            </apex:pageBlockTable>
        </apex:pageblock>
    </apex:form>    
</apex:page>

Apex Controller:

public Account myAcc{get;set;}

    public Acc(ApexPages.StandardController controller) {
        myAcc= [SELECT id, name, (Select id,name from Attachments) FROM Account WHERE id = '0012800000EK4iY'];
    }
0

You can use standard child relationship Attachments. Here is documentation about it

List <CustomObject__c> TestMSList = [
    select Id, Name__c, Desrciption__c, (
        select Id, Name
        from Attachments
        )
    from CustomObject__c
];

In order to get attachments related to your custom object:

for(CustomObject__c testMS :TestMSList){
    List<Attachment> attachs = testMS.Attachments;
}
2
  • Ok, Thanks, how can I view the name of the attachment at the visualforce? Name is not possible, I need the name from the inner Select.
    – peXeq
    Jun 19, 2017 at 6:05
  • I updated and showed you how can you access selected attachments Jun 19, 2017 at 6:12
0

Please try this:

Set<ID> AttachmentIDs = new Set<ID>();
List<Attachment> tempObjectList = new List<Attachment>();
//Prepare a list of Attachment IDs to get the parent Attachment record
            for(Attachment att : tempObjectList){
                AttachmentIDs.add(att.ParentId);
            }
//Prepare a map of all the parent CustomObject__c
Map<ID,CustomObject__c> parentCustomObjectMap =  new Map<ID,CustomObject__c>([Select Id,Name,ParentId(Select Id,Name__c, Desrciption__c from CustomObject__c) from Attachment where ID IN: AttachmentIDs]);
1
  • Could you explain what you propose? Jun 19, 2017 at 7:50
0

You can use query from parent to child to get record from parent to child

List <CustomObject__c> TestMSList = [SELECT Name__c, Desrciption__c, (SELECT Name FROM Attachment WHERE ParentId = Id) FROM CustomObject__c Where Id =:SelectedIDFromObject];

Thank you, Amol Salve Salesforce Developer

You must log in to answer this question.

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