You need to sign in to do that
Don't have an account?
Marco Mancilla
How to upload an image and store its URL in a field on a record
Hello,
I want to add a button to my Product2 object that lets a user upload a photo of the product. I would like the image URL be stored automatically in a field on the Product2 record after uploading. I am guessing that to achieve this, I need to make some kind of visualforce widget for uploading the image, tie that to the action of a button on the object, and then have some kind of workflow rule or apex trigger fire after the image is uploaded and enter its URL into the field. What is the easiest way to do this?
I want to add a button to my Product2 object that lets a user upload a photo of the product. I would like the image URL be stored automatically in a field on the Product2 record after uploading. I am guessing that to achieve this, I need to make some kind of visualforce widget for uploading the image, tie that to the action of a button on the object, and then have some kind of workflow rule or apex trigger fire after the image is uploaded and enter its URL into the field. What is the easiest way to do this?
You can use Text Area (Rich text).
The maximum size of an image that can be uploaded in a rich text area field is 1 MB. Only .gif, .jpg, and .png file types are supported.
Images within the rich text area fields count towards file storage for the object that contains the field.
Help docs seem to answer all your questions:
https://help.salesforce.com/HTViewHelpDoc?id=fields_using_rich_text_area.htm
Mark it as Best Answer, if it helps
Global class TestImage {
public blob testData {get; set;}
String TestImageUrl = 'https://www.wissen.com/wp-content/uploads/2019/04/Wissen-Technology-Logo-Final-03.jpg';
Global void TestImageUpload(){
try{
//Creating case
Case cs = new Case(Subject = 'Facing Issue with Product' ,
Origin = 'Phone',Status = 'New',Priority = 'Medium',
Description = 'The product is not good and not working properly');
insert cs;
System.debug(cs.Id);
System.debug(cs.CaseNumber);
System.debug(cs.AccountId);
HttpRequest req = new HttpRequest();
req.setEndpoint(TestImageUrl);
req.setBody(TestImageUrl);
req.setMethod('GET');
Http binding = new Http();
HttpResponse response = binding.send(req);
String base64 = EncodingUtil.base64Encode(response.getBodyAsBlob());
Blob testData = Blob.valueOf(base64);
//String Image = 'response.getBodyAsBlob()';
ContentVersion cv = new ContentVersion();
cv.ContentLocation ='S';
cv.Title = 'Test Image';
cv.PathOnClient ='Technology-Logo-Final-03.jpg';
// Blob testData = Blob.valueOf(base64);
cv.VersionData = testData;
insert cv;
String Verid =[Select ContentDocumentid from ContentVersion where id =: cv.id].ContentDocumentid;
ContentDocumentLink link = new ContentDocumentLink();
link.ContentDocumentid = verid;
link.linkedEntityId = cs.Id;
link.shareType = 'I';
link.visibility = 'ALLUsers';
//insert link;
} catch(exception e){
System.debug(e.getmessage());
}
}
}