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
Marco MancillaMarco 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?
Gaurav Jain 7Gaurav Jain 7
Hi Macro,

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
 
Marco MancillaMarco Mancilla
I do not want to store the file in a rich text field. I want the file stored in the documents object and want a PATH (not the image itself, just the URL) to that file stored in a PLAIN TEXT URL field. I already have a separate page that is rendering the images based on that path. I do not need rich text.
Shajadi shaikShajadi shaik
I also wanted to upload image on case object using apex with IMAGE URL path. i tried this but there is some changes required. u can check this one. it may helpful to you. if u successfully upload image using IMAGE URL , pls do inform me at the same time.


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());
        }         
    }
}