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

User Image As Attachment Issue

Hi All ,


I want to insert user image as attachment . Any Idea ? 





Could you please provide a little more information on your requirement. Based on my understanding, if you want to add image to User/Contact you can add a Text (Rich Text) field and add images to it. This field will look for image located in your machine.





User userObject = [select id,Email,FirstName,FullPhotoUrl,LastName,Name,SmallPhotoUrl,companyName,Title,Username,UserType from User where Id =:UserId];


I want to get SmallPhotoUrl or FullPhotoUrl value as blob so that I could insert this image as attachment where ever I want . But it only retrun the photoUrl not like a download url .


Hope you understand !


Hello there if you looking for a custom button to upload pic on the record, then here a sample i found few days back!!

<apex:page standardController="Contact" extensions="attachmentsample">

<apex:form >
  <apex:sectionHeader title="Upload a Attachment into Salesforce"/>
  <apex:pageblock >
      <apex:pageblocksection columns="1">
          <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
          <apex:commandbutton value="Save" action="{!Savedoc}"/>

// this is the controller for uploading the image

public class attachmentsample {
public Contact cont;
    public attachmentsample(ApexPages.StandardController controller) {
  //  this.cont = (Contact)controller.getRecord();

    Public Attachment myfile;
    Public Attachment getmyfile()
        myfile = new Attachment();
        return myfile;
    Public Pagereference Savedoc()
        String Contactid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = Contactid,, body = myfile.body);
         /* insert the attachment */
         insert a;
        // cont.ImageId__c =; 
        // update cont;
        return null;


 Hope it helps!!



Thanks but that is not my solution ...


I have smallimage url field sting value getting from User object query . if you copy and paste that url in salesforce url instance then user pic is visible that pic I want to get first in blob type and then I want insert that image in any object the object attachment list .


I found this if it helps,


Http h = new Http();

HttpRequest req = new HttpRequest();
string firstImageURL ='';
//Replace any spaces with %20
firstImageURL = firstImageURL.replace(' ', '%20');
//If you want to get a PDF file the Content Type would be'application/pdf'
req.setHeader('Content-Type', 'image/jpeg');
HttpResponse res = null;
res = h.send(req);
//These next three lines can show you the actual response for dealing with error situations
string responseValue ='';
responseValue = res.getBody();
system.debug('Response Body for File: ' + responseValue);
//This is the line that does the magic.  We can get the blob of our file.  This getBodyAsBlob method was added in the Spring 2012 release and version 24 of the API.
blob image = res.getBodyAsBlob();