Sunday, 4 May 2014

Showing Attachment Preview and related record detail by passing attachment id in salesforce.com

Hi,

Here in the following example i am using one custom component ,visualforce page and an extension.Here the custom component is used to display preview of attachment by getting the id from visualforce page where it is included.In this component i am using "Object" ,"embed" html tags to show preview of attachment.

Component Code:
===============
<apex:component >
<apex:attribute name="height" type="String" description="TODO: Describe me"/>
<apex:attribute name="width" type="String" description="TODO: Describe me"/>
<apex:attribute name="value" type="String" description="TODO: Describe me"/>
<object data="/servlet/servlet.FileDownload?file={!value}" type="application/pdf" width="{!width}" height="{!height}">
 <embed src="/servlet/servlet.FileDownload?file={!value}" width="{!width}" height="{!height}"/>
</object >
</apex:component>

Visualforce Page:
=============
<apex:page standardcontroller="Attachment" extensions="Attachradiocls1" sidebar="false">
    <h1 style="padding:10px;width:100%;float:left; font-size:24px; color:#015ba7;">ABC Company</h1>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <td style="width: 283px; vertical-align:top; background:#CCC;border-right: #F0F0F0 2px solid;">
                <div style="margin:20px;">
                <h1>Contact Info</h1>
                <table>
                    <tr>
                        <td>
                            Contact Name:
                        </td>
                        <td>
                            {!contobj.Name}
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Contact Email:
                        </td>
                        <td>
                            {!contobj.Email}
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Lead Source:
                        </td>
                        <td>
                            {!contobj.Leadsource}
                        </td>
                    </tr>
                </table>
                </div>
            </td>
            <td>
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                        <c:Pdfcomponent value="{!attachment.id}" width="824px" height="1135px"/>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</apex:page>

Extension Code:
==============
public with sharing class Attachradiocls1 {
    public Contact contobj{get;set;}
    public Attachradiocls1 (ApexPages.Standardcontroller stdcontroller){
        contobj=new Contact();
        String id=ApexPages.CurrentPage().getParameters().get('id');
        if(id!=null && id!=''){
            Attachment att=[select id,name,parentId,body from attachment where id=:id];
            //body=att.body.tostring();
            contobj=[select id,name,Email,LeadSource from Contact where id=:att.parentId];
        }
        
    }
}

Output:
======





7 comments:

  1. Great Post Balaji.. awesome..!!!

    ReplyDelete
  2. This will preview only PDF Attachment or any Attachment as PDF.?

    ReplyDelete
  3. Hi Sunil,
    You need to specify the type in object tag according to your attachment type.

    ReplyDelete
  4. It will be usefull to get mime types
    http://www.sitepoint.com/web-foundations/mime-types-complete-list/

    ReplyDelete
  5. Nice Info...

    ReplyDelete
  6. getting error like preview unavailable..,,could you please help on this?

    ReplyDelete
  7. I couldn't get it work? could you please assist, where can I use these page and component, if I want to use it in a custom object layout what should I need to follow?

    ReplyDelete