We recently got a need to fetch a report from Salesforce and display the same as interactive calendar. Our existing work on integration Salesforce REST API proved fruitful to fulfill the need. The solution involves leveraging Force.com SOAP API to make connection in order to get access token followed by making reports and dashboards API. Salesforce send report in JSON which can be easily parsed and rendered using interactive front end code.
STEP 1: AUTHENTICATE AND CREATE SALESFORCE API SESSION
You can now use objects of classes “SforceService” and “LoginResult” to connect to Salesforce API.
In order to access the Sforce API, we need to provide our login email, password, and the security token. Additionally, we need to generate and download appropriate WSDL which would be later used to make appropriate API calls from our code. Check existing blog Integrate Salesforce with .NET application for detail information.
Once connection with established with Sforce, we can fetch SessionId
(or Access Token) to make further calls.
STEP 2: MAKE WEB REQUEST FOR THE REPORT
Provided we have access token, we can make the final request for the report we want to execute and fetch from Force.com. Sforce exposes below API Url that can be used to send the report request:
https://c.na48.visual.force.com/services/data/v29.0/analytics/reports/[ReportId]?includeDetails=true
string ReportUrl = "https://c.na48.visual.force.com/services/data/v29.0/analytics/reports/" + YOUR_REPORT_ID + "?includeDetails=true";
To make the web request we also need to push the generated access token (or session id) onto the header and now we can make the request to receive the report data.
public string GetReportAsJsonData(string ReportUrl) { OpenConnection(); WebClient wClient = new WebClient(); wClient.Headers.Add("Authorization", "Bearer " + SessionId); return wClient.DownloadString(ReportUrl); }
Refer blog Integrate Salesforce with .NET application for details on OpenConnection()
and related code.
The code and API references use versionv29.0
. Salesforce API is continuously updated, so it’s essential to check whether this version is still supported or if there are newer versions with additional features or changes. You may need to update the API endpoint and reference the latest API version, likev52.0
or another recent one.
Salesforce authentication methods have evolved. While the SOAP API and session IDs (Access Tokens) are still commonly used, Salesforce has expanded to OAuth2 and more modern ways of handling authentication (like JWT and connected apps). Ensure that the process described in the post aligns with the current best practices for authentication, especially for API usage.