+ Start a Discussion
George AdamsGeorge Adams 

Add link to attachment on related list (custom object)

Hello,

I've created a custom object (Hearing Results) that appears as a related list on most Contact page layouts.

Here's the related list on the contact:

User-added image

And here is one of those records with it's attachment:

User-added image

Is it possible to have a column in the first screenshot, on the related list, that lets the user view the attachment that is on the associated Hearing Results record? (just like how there's a "view" link for the attachment once you actually click through to the Hearing Result record)

Thanks!
John Pipkin 14John Pipkin 14
George, 

That would not be possible with standard functionality. The issue is that there could be multiple attachments per hearing result record. So if a hearing result has 2 attachments, which attachment should the "view" link to? One way to accomplish this would be to build a custom visualforce page to put on the contact page layout. There are many ways to accomplish this, but will require custom development. 

Hope that helps!
George AdamsGeorge Adams
Thanks for the info!

I assumed it would require some custom development, just wanted an idea of where to begin. I think it would be fine to have a "view" button that opens all attachments on the record. This feature isn't too mission-critical right now though.
John Pipkin 14John Pipkin 14
George, 

Here is a small sample of what you would do. The apex:command button on the top of the page redirects to a standard salesforce url that lists all of the record's attachments.

VF
<apex:page standardController="Hearing_Result__c" extensions="NoteAttachments_Ext" sidebar="false" showHeader="false" docType="html-5.0">

<script src="/soap/ajax/36.0/connection.js"/>
<script src="https://code.jquery.com/jquery-2.1.4.js"/>
<script type="text/javascript">
	
	function goToUrl(u){
		$('[id$="newURL"]').val(u);
		redirectToUrl();
	}
</script>

	<apex:form>
		<apex:inputHidden value='{!newurl}' id="newURL"/>
		<apex:actionFunction action="{!redirToUrl}" name="redirectToUrl"/>

		<apex:outputPanel id="redirectPanel">
			<apex:outputText rendered="{!shouldRedirect}">
				<script type="text/javascript">
					window.top.location.href = '{!redirectUrl}';
				</script>
			</apex:outputText>
		</apex:outputPanel>

		<apex:pageBlock title="Notes and Attachments">
			<apex:pageBlockButtons location="top">
				<apex:commandButton value="View All" onclick="goToUrl('/ui/content/ViewAllNotesPage?id={!Hearing_Result__c.Id}');" reRender="redirectPanel"/>
			</apex:pageBlockButtons>
			<apex:pageBlockTable value="{!attachments}" var="att">
				<apex:column>
					<apex:facet name="header"/>
					<apex:commandLink value="View" onclick="goToUrl('/{!att.id}');" reRender="redirectPanel"/>
				</apex:column>
				<apex:column>
					<apex:outputField value="{!att.Name}"/>
				</apex:column>
			</apex:pageBlockTable>
		</apex:pageBlock>
	</apex:form>
</apex:page>

Extension:
 
public class NoteAttachments_Ext{

	private final Hearing_Result__c hr;
	public Boolean shouldRedirect {
		get{
			if(shouldRedirect==null){
				shouldRedirect = false;
			}
			return shouldRedirect;
		}
		set;
	}
	public String newurl {get;set;}
	public String redirectUrl {get;set;}
	

	public NoteAttachments_Ext(ApexPages.StandardController stdController) {
        this.hr = (Hearing_Result__c)stdController.getRecord();
    }

	public void redirToUrl(){
		shouldRedirect = true;
		redirectUrl = Url.getSalesforceBaseUrl().toExternalForm() + newurl;
	}

	public List<Attachment> getAttachments(){
		return [Select Id, Name from Attachment where ParentId =:hr.Id];
	}
}

Hope that helps!