1

I know I already ask on how to convert the Dashboard into an image about a week ago. But that code isn't working for me. All i getting from that code is a blank image rather then an image of the Dashboard. I'm not sure if it's is the URL problem or my codes problem.

Can Anyone Help me Fix it?

What I'm doing is:

  • Convert the Salesforce Dashboard into an image file
  • Store the Image file into the Document Folder

Code

I have already tried using jpg and the result is still the same

String base64Data = '';
HttpRequest req = new HttpRequest();
req.setEndpoint('https://ap4.salesforce.com/01Z/o');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
Blob image = res.getBodyAsBlob();
base64Data =  EncodingUtil.base64Encode(image);
System.debug('image= ' + image);

Document doc = new Document();
doc.Name = 'SF Dashboard Image.jpeg';
doc.body = EncodingUtil.base64Decode(base64Data);
doc.folderId = '00l6FA00001pqSe'; //your folder id

insert doc;
System.debug('Insert= ' + doc);

I have also tried

req.setEndpoint(EncodingUtil.urlEncode('https://ap4.salesforce.com/01Z/o', 'UTF-8'));

But i get this error message

System.CalloutException: no protocol: https%3A%2F%2Fap4.salesforce.com%2F01Z%2Fo


Salesforce Dashboard URL

I copied the URL that I highlighted in blue

enter image description here


Image I receive from the program enter image description here

1

/01Z/o is not an URL to any specific Dashboard. Its the URL for Dashboard home page, which in turn opens the recently opened Dashboard using redirection. So hitting /01Z/o in a Rest call will not help you to get the report chart images.

Let's say you have the dashboard ID stored in a variable - myDashId.

PageReference dbPage = new PageReference('/'+myDashId); //constructing your dashboard page's URL
    Blob pageBlob = dbPage.getContent(); //Hitting the page and retrieving the HTML
    String dHtml = pageBlob.toString(); //The response doesn't hold the image, it holds the HTML text of the dashboard.

The string dHtml holds the following sort of text:

enter image description here

You will have to find out all the img tags in the string and fetch its src attribute value to get the report chart images. Yes, separate image for each charts. Only the src values which has the servlet word in it are the report chart images. So you will have to search for it.

Map<Integer, String> imageURLIndexedMap = new Map<Integer, String>();
Integer count=0;
for(Boolean flag = true; flag==true;) //loop untill there is no <img/> present in the dHtml
{
    if(dHtml.contains('<img')) 
    {
        dHtml = dHtml.subString(dHtml.indexOf('<img')); //discard anything present before the <img phrase.
        imgUrl = dHtml.subString(dHtml.indexOf('"')+1); //finding " means finding the src attribute value of <img/> tag
        imgUrl = imgUrl.subString(0, imgUrl.indexOf('"')-1); //fetch the text till next ". so you have all of the src value of the image tag.
        if(!imgUrl.contains('servlet')) // does the src value has servlet in it?
        {
            dHtml = dHtml.subString(dHtml.indexOf('"')+1); //if no, increase the counter by 1 and continue searching
            imgUrl = '';
        }
        else
        {
            //found? great!!!!!
            String tempimgUrl = EncodingUtil.urlDecode(imgUrl, 'UTF-8'); //URL decode the src value.
            imgUrl = imgUrl.replaceAll('&amp;', '&'); //simple replace
            if(tempimgUrl.contains('Your Chart Name you provided in dashboard')) //just to verify you have got the intended chart URL, match with your chart header text.
                imageURLIndexedMap.put(count, imgUrl);
                count++;
            dHtml = dHtml.subString(dHtml.indexOf('<img')+5); //increase the counter, and continue searching for me, or just break.
            imgUrl = '';
        }
    }
    else
    {
        flag = false; //terminating the loop
    }
}

So now you will have the relative URLs of the chart Images in the map. Now all you have to do is to fire few callouts and retrieve the content as Blob and save in Document.

| improve this answer | |
  • so for this code i need to do a if else for each chart chart? Like example if I have a chart with the Chart Name Sales and another chart with a Chart Name Support I need do a if else right? if(tempimgUrl.contains('Your Chart Name you provided in dashboard')) //just to verify you have got the intended chart URL, match with your chart header text. imageURLMap.put(count, imgUrl); count++; – Luke Au Apr 13 '17 at 9:18
  • Another question is your variable imageURLMap what do i set it as? i tried setting it as a String and also tried setting as a Blob but I keep on getting an error when I'm trying to save it as it is not define – Luke Au Apr 13 '17 at 9:36
  • imageURLMap should have been imageURLIndexedMap, it was a typo, corrected now. And yes, if you need to catch multiple chart URLs, you will need a if else there with every chart names. – Santanu Halder Apr 13 '17 at 9:56
  • Ok thanks i will try later when i get on my laptop if any other problem occurs i will just comment here – Luke Au Apr 13 '17 at 10:23
  • where is the blob variable that I need to take upload the file to the document folder Document doc = new Document(); doc.Name = 'SF Dashboard Image.jpeg'; doc.body = EncodingUtil.base64Decode(base64Data); doc.folderId = '00l6FA00001pqSe'; //your folder id – Luke Au Apr 13 '17 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.