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
vijaymindvijaymind 

User Image As Attachment Issue

Hi All ,

 

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

 

AravindBabu512AravindBabu512

Hi,

 

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.

 

Thanks,

Aravind.

vijaymindvijaymind

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 !

Prady01Prady01

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}"/>
      </apex:pageblocksection>
  </apex:pageblock>   
</apex:form> 

</apex:page>
              
// 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, name=myfile.name, body = myfile.body);
                
         /* insert the attachment */
         insert a;
         
        // cont.ImageId__c = a.id; 
        // update cont;
        return null;
    }   

}

 Hope it helps!!

 

vijaymindvijaymind

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 .

znithchhunznithchhun

I found this if it helps,

 

Http h = new Http();

HttpRequest req = new HttpRequest();
string firstImageURL ='http://www.myDomain.com/myImage.jpg';
//Replace any spaces with %20
firstImageURL = firstImageURL.replace(' ', '%20');
req.setEndpoint(firstImageURL);
req.setMethod('GET');
//If you want to get a PDF file the Content Type would be'application/pdf'
req.setHeader('Content-Type', 'image/jpeg');
req.setCompressed(true);
req.setTimeout(60000);
                 
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();

 

Reference: http://www.sundoginteractive.com/sunblog/posts/saving-the-contents-of-an-image-or-file-url-and-saving-to-an-attachment-or