In 2020's first ComponentOne release, a new .NET Standard service library, DataConnectors, was added to connect to business solutions like Microsoft Dynamics 365 and OData. And, in the second release of the same year, we enhanced the number of supported data sources to connect to Salesforce and Kintone data using an ADO. NET provider for each type. This blog will help you understand how to bind ComponentOne WinForms controls to the Salesforce (cloud-based Customer Relationship Management platform) data using the ADO. NET provider for Salesforce.

Here we will illustrate how to bind a real-world report to the Salesforce data. We will use FlexReport and create a medical prescription report containing a patient's details, the doctor selected for the checkup, and the medicines prescribed.

See FlexReport in Action!

Download the latest version of ComponentOne Studio Enterprise

Download Now!

The complete steps from creating the report to binding it with Salesforce data and previewing in the FlexViewer are as follows.

Design the Report using C1FlexReportDesigner

At first, design the report layout as per the requirement. The only thing that needs to be taken care of is, setting the Text property for the report fields whose values are coming from the Salesforce database.

In our report, the entities values: PatientDoctor, PatientID, PatientNo, etc., are coming from the database. So, we set the Text property's report fields as =PatientDoctor, =PatientID, =PatientNo, and so on to reflect the names of the fields present in the database.

How to Bind FlexReport to Salesforce CRM Data

How to Set Up the Application

Create a Windows Forms Application

Create a new Windows Forms app, then open the form designer and drag-drop the FlexReport and FlexViewer controls on the form.

How to Bind FlexReport to Salesforce CRM Data

Load the report created in step 1 in the c1FlexReport1 using the designer or the code given below:

c1FlexReport1.Load(@"..\..\Prescription.flxr", "Medical Prescription");

Add the NuGet Packages

  1. From the Project menu, select Manage NuGet Packages. The NuGet Package Manager appears.
  2. Select nuget.org from the Package source dropdown.
  3. Click the Browse tab and select AdoNet.Salesforce from the left pane.
  4. In the right pane, click Install. This adds the references for the above packages.

How to Bind FlexReport to Salesforce CRM Data

Access Salesforce Data with a Secure Connection using Authentication

The initial step for binding the report with the data is establishing a secure connection with the database. Use C1SalesforceConnection present in the DataConnector service library to connect to the data by creating the connection object. It then passes the connection string as a parameter to the class constructor.

The OpenAuth-based authentication, supported by the ADO. NET provider for Salesforce Data can help you access your data using a secure connection. OpenAuth is an open-standard authorization protocol or framework. It describes how unrelated servers and services can safely allow authenticated access to their assets without sharing the initial, related, single login credential. The ADO. NET provider for Salesforce supports the password credentials or client credentials grant types to establish a secure connection. To implement authentication, you would need to set the appropriate values for OAuthTokenEndPoint, OAuthAccessToken, OAuthRefreshToken, OAuthClientSecret, OAuthClientId, SecurityToken, Username, and Password properties. Based on the set of values provided, the library would automatically generate and consume the AccessToken/RefereshToken to maintain a secure connection.

const string url = "https://ap16.salesforce.com/services/data/v42.0";
const string username = "*****";
const string password = "*****";
const string securityToken = "*****";
const string clientId = "*****";
const string clientSecret = "*****";
const string OAuthTokenEndpoint =
"https://ap16.salesforce.com/services/oauth2/token";

//Configure Connection string
C1SalesforceConnectionStringBuilder builder = new C1SalesforceConnectionStringBuilder();
builder.Url = url;
builder.Username = username; 
builder.Password = password;
builder.SecurityToken = securityToken;
builder.OAuthClientId = clientId; 
builder.OAuthClientSecret = clientSecret; 
builder.OAuthTokenEndpoint = OAuthTokenEndpoint;

//Setup Connection
C1SalesforceConnection conn = new C1SalesforceConnection(builder.ConnectionString);

For details, refer to the detailed documentation topic OAuth Authorization.

Retrieve the Data using C1SalesforceDataAdapter and bind with FlexReport

The ADO. NET provider for Salesforce lets you query against the data source using standard SQL syntax. The provider supports most SQL functionalities, including join queries, functions, and summaries. The basic CRUD operations, such as insertion, updating, and deleting data, can be performed either in the connected or disconnected mode by using the C1SalesforceCommand and C1SalesforceDataAdapter objects.

The sample code below depicts how to retrieve the data from the source using the C1SalesforceDataAdapter object’s Fill method and bind it with the FlexReport using the Recordset and RecordSource properties of FlexReport’s DataSource object.

String sql = "SELECT * from [PatientInfo]";
using (C1SalesforceConnection c = new C1SalesforceConnection($@"{GCSalesforceServerConnectionString}"))
{
//Open Connection
c.Open();
using (C1SalesforceDataAdapter a = new C1SalesforceDataAdapter(c, sql))
{
//Filling Data Table with the help of adapter
DataTable t = new DataTable();
a.Fill(t);
c1FlexReport1.DataSource.Recordset = t;
c1FlexReport1.DataSource.RecordSource = "PatientInfo";
}
}

Render the FlexReport in FlexViewer

To render the report in the FlexViewer, set its DocumentSource property to the report instance.

c1FlexViewer1.DocumentSource = c1FlexReport1;

How to Bind FlexReport to Salesforce CRM Data

This report can be printed and exported to various formats - Excel, Pdf, HTML, Docx, Rtf, Png, Jpeg, Bmp, Tiff, etc.

How to Bind FlexReport to Salesforce CRM Data

From the 2021 v1 release of CompoenentOne, you can directly connect your FlexReport to Salesforce data using FlexReportDesigner. Try it yourself! Please leave any questions in the comments section below. Happy coding!

Try ComponentOne Studio

Download the latest version of ComponentOne Studio Enterprise

Download Now!