I have byte array data of user image and what is best way to upload contact/lead profile photo and what is the REST API for uploading the same.
I have also social site photo url. is there any API to upload using photo url as profile image?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityI have byte array data of user image and what is best way to upload contact/lead profile photo and what is the REST API for uploading the same.
I have also social site photo url. is there any API to upload using photo url as profile image?
You can upload image to salesforce by making a http callout to the URL and get the image as response and save it to the documents object. First create a folder in documents tab and save the images.
start up code that can help you.
public class ImageUpload{
public Static void ImageUploadVAttachment(){
Http h = new Http();
HttpRequest req = new HttpRequest();
string firstImageURL = 'Endpoint URL should go here';
firstImageURL = firstImageURL.replace(' ', '%20');
req.setEndpoint(firstImageURL);
req.setMethod('GET');
req.setHeader('Content-Type', 'image/jpg');
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);
// getBodyAsBlob method
blob image = res.getBodyAsBlob();
//Not that by simply inserting into ContentVersion that the actual ContentDocument entry will be created for you
ContentVersion cv = new ContentVersion();
cv.VersionData = image; //This is our blob
cv.title = 'MyImageName';
cv.Description = 'MyImageDescription';
cv.FirstPublishLocationId = '058q00000000Y6B'; //This is the library
cv.TagCsv = 'Tag1';
cv.PathOnClient = '';
cv.ReasonForChange = ''; // If we update an image then we may have to set this
//cv.ContentDocumentID = ''; //If we are inserting a revision to a document then we need this. May have to query for it first
//When testing this remember that it takes a few seconds for a document to show up in the library
insert cv;
}
}