The use case here is that I need to fetch the image/png of Lightning Dashboard and store it in Files via Apex

So first, I try to do a callout to Dashboards REST API -

Http http = new Http();
HttpRequest req = new HttpRequest();

req.setHeader('Authorization', 'Bearer '+Userinfo.getSessionId());
req.setHeader('Content-Type', 'image/png');

HttpResponse res = http.send(req);

if(res.getStatusCode() == 302){
    res = new Http().send(req);
    system.debug('response-->' + res.getbody());

Then, once I get the results, I try to store it in Files using by creating a new ContentVersion record -

ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.VersionData = res.getBodyAsBlob();
cv.Title = 'Sample File';
cv.PathOnClient = 'test.png';

insert cv;

However when I download the png file and view the file, it looks like the file didn't generate properly -

enter image description here

1 Answer 1


This is a limitation of the API, called out in the documentation:

Downloads are requested by entering the URI in a browser address bar.

Use of this API from Apex isn’t supported.

  • Well that escalated quickly! Thanks for the doc reference. Adding an additional point here from the documentation - "To use the Analytics Download API, your org must have a CRM Analytics license"
    – Aditya M
    Commented Apr 1 at 11:16

You must log in to answer this question.

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