function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Manoj DeshmukhManoj Deshmukh 

How to Create Custom Related List on Visualforce page For Notes and Attachment

Best Answer chosen by Manoj Deshmukh
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Use this code:
VF PAGE:
-------------

<apex:page standardController="Contact" extensions="cls">
    
	<apex:pageBlock  >
        
            <apex:pageBlockTable value="{!NotesList}" var="n" >
                <apex:column headerValue="Type" >
                    <apex:outputText rendered="{!n.isnote}" >Note</apex:outputText>
                    <apex:outputText rendered="{!NOT(n.isnote)}" >Attachment</apex:outputText>
                </apex:column>
                <apex:column headerValue="Title" value="{!n.title}" />
                <apex:column headerValue="Last Modified" value="{!n.LastModifiedDate}" />
                <apex:column headerValue="Created By" value="{!n.createdby.name}" />
            </apex:pageBlockTable>
        
        
    </apex:pageBlock>    
    
</apex:page>



Controller Class:
----------------------

public class cls{
    
    public noteandattachment[] NotesList{get;set;}
     
    public cls(ApexPages.standardController std){
        NotesList=new noteandattachment[]{};
         string ContactID=ApexPages.currentPage().getParameters().get('id');
        if(ContactID==NULL){
            system.debug('No Id available');
        }
        else{
            contact[] CNotes=new contact[]{};
            CNotes=[Select id,(select isnote,title,lastmodifieddate,createdby.name from NotesAndAttachments)  from contact where id =:ContactID];
            for( contact c:Cnotes ){
                for(NoteAndAttachment obj:c.NotesAndAttachments ){
                    NotesList.add(obj);
                }
            }
        }
    }
}

Let me know if it helps.
Thanks.​

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
If you mean notes and attachment as a related list for custom object then 
First make sure notes and attachment are enabled for that custom object.
Then in your VF page code, use this:

<apex:relatedList subject="{!account}" list="CombinedAttachments" />
Manoj DeshmukhManoj Deshmukh
Hi
I have requirement of create a Visualforce Related List to display Notes and Attachment Details in Contact object with Edit and Delete Actions.
Narender Singh(Nads)Narender Singh(Nads)
Why create a VF page when its already there in the related list of contact object with Edit and Delete actions.
Narender Singh(Nads)Narender Singh(Nads)
But still if you want to do that, your VF page will look something like this:
<apex:page standardController="Contact">
    <apex:relatedList list="CombinedAttachments" />
</apex:page>

 
Manoj DeshmukhManoj Deshmukh
Hi Narender ,
I want to restrict other user to edit and delete option means i want to hide these two options for other user . 
so this is the only one option to create a visualforce page.
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Use this code:
VF PAGE:
-------------

<apex:page standardController="Contact" extensions="cls">
    
	<apex:pageBlock  >
        
            <apex:pageBlockTable value="{!NotesList}" var="n" >
                <apex:column headerValue="Type" >
                    <apex:outputText rendered="{!n.isnote}" >Note</apex:outputText>
                    <apex:outputText rendered="{!NOT(n.isnote)}" >Attachment</apex:outputText>
                </apex:column>
                <apex:column headerValue="Title" value="{!n.title}" />
                <apex:column headerValue="Last Modified" value="{!n.LastModifiedDate}" />
                <apex:column headerValue="Created By" value="{!n.createdby.name}" />
            </apex:pageBlockTable>
        
        
    </apex:pageBlock>    
    
</apex:page>



Controller Class:
----------------------

public class cls{
    
    public noteandattachment[] NotesList{get;set;}
     
    public cls(ApexPages.standardController std){
        NotesList=new noteandattachment[]{};
         string ContactID=ApexPages.currentPage().getParameters().get('id');
        if(ContactID==NULL){
            system.debug('No Id available');
        }
        else{
            contact[] CNotes=new contact[]{};
            CNotes=[Select id,(select isnote,title,lastmodifieddate,createdby.name from NotesAndAttachments)  from contact where id =:ContactID];
            for( contact c:Cnotes ){
                for(NoteAndAttachment obj:c.NotesAndAttachments ){
                    NotesList.add(obj);
                }
            }
        }
    }
}

Let me know if it helps.
Thanks.​
This was selected as the best answer
Manoj DeshmukhManoj Deshmukh
Thnx Narender ,
can we add Edit and Delete action against Notes and Attachment Records
 
Narender Singh(Nads)Narender Singh(Nads)
Hi manoj,
Yes we can.

If my previous answer helped you, please mark it as the best answer so that others with similar requirement can benefit from this post.
Thanks.
Manoj DeshmukhManoj Deshmukh
Hi Narender 
Can you please help to complete above requirement 
Thanks
Narender Singh(Nads)Narender Singh(Nads)
Hi,

Use this code:
 
VF PAGE:
-------------


<apex:page id="Page" standardController="Contact" docType="HTML-5.0" extensions="cls">
    
    <script>
    	
    </script>
    
    <apex:form>
    
	<apex:pageBlock id="Table" >
        
            <apex:pageBlockTable value="{!NotesList}" var="n" >
                <apex:column headerValue="Actions" >
                    <apex:commandLink value="Edit" action="{!HandleEdit}" target="_blank" >
                        <apex:param name="NoteID" value="{!n.id}"/>
                    </apex:commandLink>
                    | 
                    <apex:commandLink value="Del" action="{!HandleDelete}" reRender="Page,Table" >
                        <apex:param name="NoteID" value="{!n.id}"/>
                    </apex:commandLink>
                </apex:column>
                <apex:column headerValue="Type" >
                    <apex:outputText rendered="{!n.isnote}" >Note</apex:outputText>
                    <apex:outputText rendered="{!NOT(n.isnote)}" >Attachment</apex:outputText>
                </apex:column>
                <apex:column headerValue="Title" value="{!n.title}" />
                <apex:column headerValue="Last Modified" value="{!n.LastModifiedDate}" />
                <apex:column headerValue="Created By" value="{!n.createdby.name}" />
            </apex:pageBlockTable>
        
        
    </apex:pageBlock>
    </apex:form>
    
</apex:page>


Controller Class:
----------------------

public class cls{
    
    public noteandattachment[] NotesList{get;set;}
     
    public cls(ApexPages.standardController std){
        NotesList=new noteandattachment[]{};
        GetNotes();
    }
    
    public pagereference HandleEdit(){
        String NoteID=ApexPages.currentPage().getParameters().get('NoteID');
        system.debug(NoteID);
        if(NoteID!=NULL){
            String EditPageURL='/'+NoteID+'/e?retURL=%2F'+NoteID;
            PageReference pageRef = new PageReference(EditPageURL);
            pageref.setRedirect(true);
            return pageRef;
        }
        return NULL;
    }
    
    public pagereference HandleDelete(){
        String NoteID=ApexPages.currentPage().getParameters().get('NoteID');
        string ContactID=ApexPages.currentPage().getParameters().get('id');
        system.debug(NoteID);
        note n=[select id, title from note where id=:NoteID];
        delete n;
        system.debug('HandleDelete FN');
        this.GetNotes();
        String EditPageURL='/apex/DelVF?id='+ContactID;
        PageReference pageRef = new PageReference(EditPageURL);
        pageref.setRedirect(true);
        return pageRef;
        //return NULL;
	}
    
    public void GetNotes(){
        system.debug('GetNotes FN');
        string ContactID=ApexPages.currentPage().getParameters().get('id');
        if(ContactID==NULL){
            system.debug('No Id available');
        }
        else{
            contact[] CNotes=new contact[]{};
            CNotes=[Select id,(select id,isnote,title,lastmodifieddate,createdby.name from NotesAndAttachments)  from contact where id =:ContactID];
            for( contact c:Cnotes ){
                for(NoteAndAttachment obj:c.NotesAndAttachments ){
                    NotesList.add(obj);
                }
            }
        }
    }
}

 
Manoj DeshmukhManoj Deshmukh
Hi 
I have add one Button to create New Attachment just like standard related list functionality. I have implement but after add file and click to done it will throw an below message. but the file is attach correctly against that record but why this message display

Data Not Available
The data you were trying to access could not be found. It may be due to another user deleting the data or a system error. If you know the data is not deleted but cannot access it, please look at our supportpage. 

Below is my code-:
VF Page-:

    <apex:pageBlock >
            
               <div align="center" draggable="false" >
                   <apex:commandlink action="{!switch}" target="_parent" >
                   <apex:commandButton value="New Attachment"  style="display: inline-block" />
                   <apex:param name="TrName" value="{!Transaction__c.Name}" assignTo="{!TransName}"/>
                   <apex:param name="Trid" value="{!Transaction__c.Id}" assignTo="{!TransId}"/>
                   </apex:commandlink><br/>
               </div>
            
            <apex:pageBlockTable value="{!NotesList}" var="n" >
            

Apex class-:

public pageReference switch() {
        String TransactionName= ApexPages.currentPage().getParameters().get('TrName'); 
        String Transactionid= ApexPages.currentPage().getParameters().get('Trid');         
        PageReference pageRef = new PageReference('/p/attach/NoteAttach?pid='+Transactionid+'&parentname='+TransactionName+'&retURL=%2F0030K00001NOY76');
        pageRef.setRedirect(true);
        return pageRef;
    }

 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
I suggest you post your error as a new query, I can only guess why you are getting this error(never encountered it).
Manoj DeshmukhManoj Deshmukh
Hi,
Its working now the url will be wrong

PageReference pageRef = new PageReference('/p/attach/NoteAttach?pid='+Transactionid+'&parentname='+TransactionName+'&retURL=%2F'+TransactionID);

Your HandleDelete function not work when I delete attachment it will throw an error List has no rows for assignment to SObject
Narender Singh(Nads)Narender Singh(Nads)
That's because my handledelete function only queries on Notes object, not on Attachment object. Create another function to delete the attachment by querying the record.
 
Baddepally RamlingappaBaddepally Ramlingappa
Hi, 

I have created custom related list called Active Quotes for Opportunities. For this to happen, I have created a lookup field in quote in relation to opportunity and added list button called New Quote for Active Quotes custom related list

In lightening, if New Quote button of custom related list which has url as below is clicked, backgroud got screwed up and on Edit Screen of Quote, it is not populating Opportunity and related account. 
/0Q0/e?retURL={!Opportunity.Id}&oppid={!Opportunity.Id}
As I am passing opportunity id to quote, it doesn't show up opportunity and account related to it and background gets screwed up.

Please check images before button click and after click to find background gto screwed up.

background image before New Quote button clicked

background image after New Quote button clicked

In Salesforce Classic, it is working as expected.

Could you please help how to fix it.