Lightning Components and App Builder make it easy to customize Record Home Pages (pilot feature in Winter 16). Sometimes a simple component can add a lot of value to a page by streamlining a common task.

In this article I share a simple ProfilePicture component that allows you to upload a profile picture using drag-and-drop, and display it on the Record Home page. It can be used for Accounts, Contacts and other objects.

Watch the video below to see how to install and use the ProfilePicture component.

Source Code


<aura:component controller="ProfilePictureController"
    <!-- Id of the Record the page hosting this component is showing -->
    <aura:attribute name="recordId" type="Id"/>

    <aura:attribute name="pictureSrc" type="String" 

    <aura:attribute name="message" type="String" default="Drag profile picture here"/>

    <aura:handler name="init" value="{!this}" action="{!c.onInit}" />
    <div ondragover="{!c.onDragOver}" ondrop="{!c.onDrop}">
        <img src="{!v.pictureSrc}"/>
  • The flexipage:availableForAllPageTypes interface indicates the component can be used in App Builder
  • The force:hasRecordId interface indicates the current record Id should be injected in the component’s recordId attribute


    // Load current profile picture
    onInit: function(component) {
        var action = component.get("c.getProfilePicture"); 
            parentId: component.get("v.recordId"),
        action.setCallback(this, function(a) {
            var attachment = a.getReturnValue();
            if (attachment && attachment.Id) {
	            component.set('v.pictureSrc', '/servlet/servlet.FileDownload?file=' 
                                                  + attachment.Id);
    onDragOver: function(component, event) {

    onDrop: function(component, event, helper) {
        event.dataTransfer.dropEffect = 'copy';
        var files = event.dataTransfer.files;
        if (files.length>1) {
            return alert("You can only upload one profile picture");
        helper.readFile(component, helper, files[0]);


    readFile: function(component, helper, file) {
        if (!file) return;
        if (!file.type.match(/(image.*)/)) {
  			return alert('Image file not supported');
        var reader = new FileReader();
        reader.onloadend = function() {
            var dataURL = reader.result;
            component.set("v.pictureSrc", dataURL);
            helper.upload(component, file, dataURL.match(/,(.*)$/)[1]);
    upload: function(component, file, base64Data) {
        var action = component.get("c.saveAttachment"); 
            parentId: component.get("v.recordId"),
            base64Data: base64Data, 
            contentType: file.type
        action.setCallback(this, function(a) {
            component.set("v.message", "Image uploaded");
        component.set("v.message", "Uploading...");



    text-align: center;

.THIS > p {
    font-size: 14px;
    color: #CCCCCC;
    margin: 4px 0;

.THIS img {
    min-height: 100px;
    max-height: 200px;
    max-width: 300px;
	width: auto;
    height: auto;
    border-radius: 4px;


This part is required to use the component in App Builder. The attribute makes the initial message (“Drag profile picture here”) configurable in App Builder.

<design:component label="ProfilePicture">
	<design:attribute name="message" label="Message"/>

Apex Controller

public with sharing class ProfilePictureController {
    public static Attachment getProfilePicture(Id parentId) {

        // Attachment permissions are set in parent object (Contact)
        if (!Schema.sObjectType.Contact.isAccessible()) {
            throw new System.NoAccessException();
            return null;
        return [SELECT Id, Name, LastModifiedDate, ContentType FROM Attachment 
            WHERE parentid=:ParentId AND ContentType IN ('image/png', 'image/jpeg', 'image/gif') 
            ORDER BY LastModifiedDate DESC LIMIT 1];
    public static Id saveAttachment(Id parentId, String fileName, String base64Data, String contentType) { 

        // Edit permission on parent object (Contact) is required to add attachments
        if (!Schema.sObjectType.Contact.isUpdateable()) {
            throw new System.NoAccessException();
            return null;

        Attachment attachment = new Attachment();
        attachment.parentId = parentId;
        attachment.body = EncodingUtil.base64Decode(base64Data); = fileName;
		attachment.contentType = contentType;
        insert attachment;

  • Adjust the code to work with a specific object type. For example, replace Contact with Account if you want to use the component with Account objects. You could also write a generic version that detects the object type based on the id, and checks permissions on that object type.
  • getProfilePicture() retrieves the profile picture for a specific record. By convention, we use the latest image attached to the record. You could easily tweak that strategy. For example, you could add a custom field to the parent object to keep track of the Id of the specific attachment you want to use as the profile picture.
  • saveAttachment() creates the attachment for the picture you uploaded

Installation Instructions

See my previous post for detailed steps on how to enable the Lightning Experience and the pilot feature that allows you to customize Record Home Pages with App Builder.

Resources and Acknowledgement

Peter Knolle’s generic FileUpload component provided a great starting point to build ProfilePicture. Peter’s article also includes a strategy for dealing with large files.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS