+ Start a Discussion
RoyGiladRoyGilad 

Analytics API from Apex

Hi All,

I am trying to run a report from apex and get the JSON response I've looked the the documentation but I can see how to get the data from out-side the system but I can't find how to run the report from within Salesforce,

 

Please help,

Thank you,

Roy

 

Link to the documentation:

http://www.salesforce.com/us/developer/docs/api_analytics/index.htm

 

 

Best Answer chosen by Admin (Salesforce Developers) 
RoyGiladRoyGilad

Hi All,

What I did in the end is that I have invoked the Analytics API as an external service:

 

public class AnalyticsAPIConnector{
   
    public static string runReportSync(string reportId){
   
        Http http = new Http();
        HttpRequest httpReq = new HttpRequest();
        HttpResponse httpRes = new HttpResponse();
       
        httpReq.setMethod('GET');
        httpReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
        httpReq.setEndpoint(
            URL.getSalesforceBaseUrl().toExternalForm()+
            '/services/data/v29.0/analytics/reports/' + reportId +
            '?includeDetails=true'
        );
       
        httpRes = http.send(httpReq);
       
        return httpRes.getBody();
   
    }
}

 

Not the best solution but it works, 

Enjoy,

Roy

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Do you want to use analytics api right within apex to access data from same salesforce org?

Ashish_SFDCAshish_SFDC

Hi Roy, 

 

 

I dont think that is directly possible, you may try calling the URL and working on the same, 

 

See the links below for similar discussion, 

 

http://salesforce.stackexchange.com/questions/337/can-report-data-be-accessed-programatically

 

http://sfdc-heretic.warped-minds.com/2006/04/10/progmatic-access-to-salesforcecom-reports/

 

 

Regards,

Ashish

 

RoyGiladRoyGilad

Hi All,

What I did in the end is that I have invoked the Analytics API as an external service:

 

public class AnalyticsAPIConnector{
   
    public static string runReportSync(string reportId){
   
        Http http = new Http();
        HttpRequest httpReq = new HttpRequest();
        HttpResponse httpRes = new HttpResponse();
       
        httpReq.setMethod('GET');
        httpReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
        httpReq.setEndpoint(
            URL.getSalesforceBaseUrl().toExternalForm()+
            '/services/data/v29.0/analytics/reports/' + reportId +
            '?includeDetails=true'
        );
       
        httpRes = http.send(httpReq);
       
        return httpRes.getBody();
   
    }
}

 

Not the best solution but it works, 

Enjoy,

Roy

This was selected as the best answer
Ashish_SFDCAshish_SFDC

Thats really Awesome!!!

 

Thanks a lot for sharing the Solution Roy.

 

Regards,

Ashish