Through REST API I am capturing url for a particular image now,I want to read the data from that url in blob format and then attach that image in sales force ,

can any please help to read the data from url This is my trigger:

trigger AttachmentImage on Item__c (before insert,before update) {
   list<Item__c>  lst=[select id,URL__c from Item__c];

    Blob pic ;
    pic =blob.valueof('http://multisite.dhrusoff.com/wp-content/plugins/forces/img/ballon1949004662-300X300.jpg');
    for(Item__c b:lst)

       if(b.URL__c !=null) {

         Attachment a = new Attachment (ParentId = b.id,Body = pic,Name = 'Item Image.jpeg');
          System.debug('==========Attach======= - ' +a); 
          System.debug('==========Attach======= - ' +a.id);
        insert a;



2 Answers 2


You can't fetch image this way. Salesforce won't allow you to fetch image with such random URL.


Above will only convert the URL into blob not the real image (blob).

Solution can be that add the URL to Remote site setting and then do callout to get actual image. Convert image data to base64 String. Match the String values with existing image base64 string.

  • Hi Ashwani i will capturing image url's from php and store url in url field of a custom object .for every we will have different url. it is not not site url
    – user23824
    Commented Aug 20, 2015 at 13:49
  • 2
    Salesforce won't allow callout to URL until it is added in Remote Site Setting. @user23824
    – Ashwani
    Commented Aug 20, 2015 at 13:51

Please find the working example as below. You can fetch the image from external URL provided you have add the URL in remote site setting and set the Content-Type as image/jpeg.

Callouts cannot be made from triggers as that would hold up the database transaction until the callout completed. The only way that you can execute a callout from a trigger is to schedule it to run asynchronously, for example by executing a method with the @future method.

Check if the Notes And Attachments related list is present or else add to the layout. Also it takes some time to appear the image in the related list as this is a future method.


trigger HelloWorldTrigger on Book__c(after insert) {
    Book__c[] books = Trigger.new;
    Id bookId = books[0].Id;


public with sharing class BookTriggerHelper {
    public BookTriggerHelper() {

    public static void attachImage(Id bookId){
        Book__c[] books = [Select Id From Book__c where Id = :bookId];
        Http h = new Http();
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'image/jpeg');
        // Send the request, and return a response
        HttpResponse res = h.send(req);
        Attachment att = new Attachment();
        att.Body = res.getBodyAsBlob();
        att.contentType = 'image/jpeg';
        att.Name = 'ballon1949004662-300X300.jpg';
        att.parentId = books[0].Id;
        insert att;
  • n.b. trigger is not bulkified
    – cropredy
    Commented Aug 20, 2015 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .