Salesforce Connector for PowerShell How to Get Report
Introduction
In this article we will delve deeper into Salesforce and PowerShell integration, and will learn how to get report. We are continuing from where we left off. By this time, you must have installed ODBC PowerPack, created ODBC Data Source, and configured authentication settings in your Salesforce account .
So, let's not waste time and begin.
Use Query Builder to generate SQL query
-
The first thing you have to do is open Query Builder:
ZappySys API Driver - SalesforceAmazon Ads Connector can be used to get Amazon advertisements using Amazon Advertisements API, download various advertisement-related reports.SalesforceDSN
-
Then simply select the Get Report endpoint (action).
-
Continue by configuring the Required parameters. You can also set optional parameters too.
-
Move on by hitting Preview Data button to preview the results.
-
If you see the results you need, simply copy the generated query:
-
That's it! You can use this query in PowerShell.
Let's not stop here and explore SQL query examples, including how to use them in Stored Procedures and Views (virtual tables) in the next steps.
SQL query examples
Use these SQL queries in your PowerShell data source:
How to Get report
SELECT * FROM get_report
WITH
(
"RecordType" = 'keywords',
"ReportDate" = 'today',
"Metrics" = 'abcd-1234-metrics'
)
Get Report in PowerShell
-
Open your favorite PowerShell IDE (we are using Visual Studio Code).
-
Use this code snippet to read the data using
SalesforceDSNdata source:"DSN=SalesforceDSN"
For your convenience, here is the whole PowerShell script:
# Configure connection string and query $connectionString = "DSN=SalesforceDSN" $query = "SELECT * FROM Customers" # Instantiate OdbcDataAdapter and DataTable $adapter = New-Object System.Data.Odbc.OdbcDataAdapter($query, $connectionString) $table = New-Object System.Data.DataTable # Fill the table with data $adapter.Fill($table) # Since we know we will be reading just 4 columns, let's define format for those 4 columns, each separated by a tab $format = "{0}`t{1}`t{2}`t{3}" # Display data in the console foreach ($row in $table.Rows) { # Construct line based on the format and individual Salesforce fields $line = $format -f ($row["CustomerId"], $row["CompanyName"], $row["Country"], $row["Phone"]) Write-Host $line }Access specific Salesforce table field using this code snippet:
You will find more info on how to manipulate$field = $row["ColumnName"]DataTable.Rowsproperty in Microsoft .NET reference.For demonstration purposes we are using sample tables which may not be available in Salesforce. -
To read values in a console, save the script to a file and then execute this command inside PowerShell terminal:
You can also use even a simpler command inside the terminal, e.g.:. 'C:\Users\john\Documents\dsn.ps1'
More actions supported by Salesforce Connector
Learn how to perform other actions directly in PowerShell with these how-to guides:
- Get Beta Report
- Get Profiles
- Test Connection
- Make Generic API Request
- Make Generic API Request (Bulk Write)