Wednesday, January 18, 2017

Inline VisualForce Page to Send Notes & Attachment to the Owner of the Account from Account Detail Page.





Page:

<apex:page standardController="Account" extensions="NotestAttsRelatedListController">
<apex:form >
<apex:pageBlock >
     <apex:pageblock title="Attachments">
            <apex:pageblockTable value="{!atts}" var="a">
            <apex:column headerValue="Actions">
                <apex:outputLink value="{!URLFOR($Action.Attachment.Download, a.Id)}" target="_blank">View</apex:outputLink>
            </apex:column>
            <apex:column value="{!a.Name}" headerValue="Title"/>
            <apex:column value="{!a.BodyLength }" headerValue="Size"/>
    <!--        <apex:column value="{!a.Parent.Type}" headerValue="Object Name"/>
            <apex:column value="{!a.Parent.Name}" headerValue="Record Name" />   -->
            <apex:column value="{!a.Owner.Name}" headerValue="Owner Name"/>
            <apex:column value="{!a.LastModifiedDate}"/>
            <apex:column headerValue="Send As Email">
                <apex:commandLink value="Send Attachment" action="{!SendAttachment}">
                    <apex:param name="attId" value="{!a.Id}" assignTo="{!aId}" />
                </apex:commandlink>
            </apex:column>
            </apex:pageblockTable>
  </apex:pageblock>
</apex:pageBlock>
</apex:form>

</apex:page>

Controller:

public class NotestAttsRelatedListController {

    public list<Attachment> atts {get;set;}
    public String aid {get; set;}

    public NotestAttsRelatedListController(ApexPages.StandardController controller) {
     
         atts = [Select a.Id,a.ContentType,a.ParentId,a.Parent.Type,a.Parent.Name,a.OwnerId,a.Owner.Name, a.Name, a.LastModifiedDate, a.BodyLength From Attachment a
               where a.ParentId=:ApexPages.currentPage().getParameters().get('id')];
    }
 
    Public pagereference SendAttachment(){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Account r = [select Owner.Email,Name from Account where Id= :ApexPages.currentPage().getParameters().get('id')];
        String[] toAddresses = new String[] {r.Owner.Email};  
        mail.setToAddresses(toAddresses);
        mail.setSenderDisplayName('Name');
        mail.setSubject('Attachment');
        mail.setBccSender(false);
        mail.setUseSignature(true);
        mail.setPlainTextBody('This is test email body. This mail is being sent from apex code');
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where Id= :aid]){
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
    }

}

Note: After Creation of VF page add it to Account page layout Otherwise it will show Visualforce Error.

No comments:

Post a Comment