Table of Contents

Class ForceClient

Namespace
NetCoreForce.Client
Assembly
NetCoreForce.Client.dll
public class ForceClient
Inheritance
ForceClient
Inherited Members

Constructors

ForceClient(AuthInfo)

Login to Salesforce using the username-password authentication flow, and initialize the client

public ForceClient(AuthInfo authInfo)

Parameters

authInfo AuthInfo

ForceClient(string, string, string, HttpClient, AccessTokenResponse)

Initialize the client using previously obtained access token and instance url, if using the AuthenticationClient separately.

public ForceClient(string instanceUrl, string apiVersion, string accessToken, HttpClient httpClient = null, AccessTokenResponse accessInfo = null)

Parameters

instanceUrl string

Identifies the Salesforce instance to which API calls should be sent.

apiVersion string

Salesforce API version

accessToken string

Access token

httpClient HttpClient

Optional HttpClient object. Defaults to a shared static instance for best performance, but a custom HttpClient can be specified when custom properties are needed e.g. proxy settings.

accessInfo AccessTokenResponse

AccessTokenResponse object, to store all of the OAuth details received via the AuthenticationClient

ForceClient(string, string, string, string, string, string, HttpClient)

Login to Salesforce using the username-password authentication flow, and initialize the client

public ForceClient(string clientId, string clientSecret, string username, string password, string tokenRequestEndpoint, string apiVersion = null, HttpClient httpClient = null)

Parameters

clientId string

Client ID, a.k.a. Consumer Key

clientSecret string

Client Secret, a.k.a. Consumer Secret

username string

Salesforce username

password string

Salesforce password

tokenRequestEndpoint string

Token request endpoint

e.g. https://login.salesforce.com/services/oauth2/token

apiVersion string

Salesforce API version

httpClient HttpClient

Optional HttpClient object. Defaults to a shared static instance for best performance, but a custom HttpClient can be specified when custom properties are needed e.g. proxy settings.

Properties

AccessInfo

The Access Token Response data received after a successful login May not be available if the client was initialized with a pre-existing access token

public AccessTokenResponse AccessInfo { get; set; }

Property Value

AccessTokenResponse

AccessToken

public string AccessToken { get; }

Property Value

string

ApiVersion

public string ApiVersion { get; }

Property Value

string

ClientName

public string ClientName { get; set; }

Property Value

string

InstanceUrl

public string InstanceUrl { get; }

Property Value

string

Methods

BlobRetrieveStream(string)

Retrieve blob data at the specified URL. Assumes a relative URL - If a full URL is used, the instance portion will be ignored.

public Task<Stream> BlobRetrieveStream(string blobUrl)

Parameters

blobUrl string

relative blob URL

Returns

Task<Stream>

binary content stream

BlobRetrieveStream(string, string, string)

public Task<Stream> BlobRetrieveStream(string sObjectTypeName, string objectId, string blobField)

Parameters

sObjectTypeName string
objectId string
blobField string

Returns

Task<Stream>

CountQuery(string, bool)

Get a basic SOQL COUNT() query result

The query must start with SELECT COUNT() FROM, with no named field in the count clause. COUNT() must be the only element in the SELECT list.

public Task<int> CountQuery(string queryString, bool queryAll = false)

Parameters

queryString string

SOQL query string starting with SELECT COUNT() FROM

queryAll bool

True if deleted records are to be included

Returns

Task<int>

The Task<TResult> returning the count

CreateMultiple(string, List<SObject>, bool, Dictionary<string, string>, List<string>, bool)

Create mlutiple records

public Task<SObjectTreeResponse> CreateMultiple(string sObjectTypeName, List<SObject> sObjects, bool autoFillAttributes = true, Dictionary<string, string> customHeaders = null, List<string> fieldsToNull = null, bool ignoreNulls = true)

Parameters

sObjectTypeName string

SObject name, e.g. "Account"

sObjects List<SObject>

Objects to create. Each sObject must have the entity type and reference id in the attributes property object.

autoFillAttributes bool

Automatically create attribute object property, reference Id will be the zero-based index of the array

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

fieldsToNull List<string>

A list of properties that should be set to null, but inclusing the null values in the serialized output

ignoreNulls bool

Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null

Returns

Task<SObjectTreeResponse>

SObjectTreeResponse object, includes new object IDs, and errors if any

Exceptions

ForceApiException

Thrown when creation fails

CreateRecord<T>(string, T, Dictionary<string, string>, List<string>, bool)

Create a new record

public Task<CreateResponse> CreateRecord<T>(string sObjectTypeName, T sObject, Dictionary<string, string> customHeaders = null, List<string> fieldsToNull = null, bool ignoreNulls = true)

Parameters

sObjectTypeName string

SObject name, e.g. "Account"

sObject T

Object to create

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

fieldsToNull List<string>

A list of properties that should be set to null, but inclusing the null values in the serialized output

ignoreNulls bool

Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null

Returns

Task<CreateResponse>

CreateResponse object, includes new object's ID

Type Parameters

T

Exceptions

ForceApiException

Thrown when creation fails

DeleteRecord(string, string)

Delete record

public Task DeleteRecord(string sObjectTypeName, string objectId)

Parameters

sObjectTypeName string

SObject name, e.g. "Account"

objectId string

Id of Object to update

Returns

Task

void, API returns 204/NoContent

Exceptions

ForceApiException

Thrown when update fails

DescribeGlobal()

Get a List of Objects

Use the Describe Global resource to list the objects available in your org and available to the logged-in user. This resource also returns the org encoding, as well as maximum batch size permitted in queries.

public Task<DescribeGlobal> DescribeGlobal()

Returns

Task<DescribeGlobal>

Returns DescribeGlobal object with a SObjectDescribe collection

Dispose()

Dispose client - only disposes instance HttpClient, if any. Shared static HttpClient is left as-is.

public void Dispose()

ExecuteApexPost<Request, T>(string, Request, Dictionary<string, string>)

Execute request against ApexRest custom endpoints.

public Task<T> ExecuteApexPost<Request, T>(string apexResourceUrl, Request request, Dictionary<string, string> customHeaders = null)

Parameters

apexResourceUrl string

The URL of the apex resource. Ex: /services/apexrest/DuplicateCheck should provide "DuplicateCheck"

request Request

The custom object to include in the request

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

Returns

Task<T>

List of UpdateMultipleResponse objects, includes response for each object (id, success, errors)

Type Parameters

Request
T

Exceptions

ArgumentException

Thrown when missing required information

ForceApiException

Thrown when update fails

ExecuteCompositeRecords(List<CompositeSObject>, bool, bool, Dictionary<string, string>)

Execute multiple composite records. The list can contain up to 200 objects. The list can contain objects of different types, including custom objects. Each object must contain an attributes map. The map must contain a value for type.

public Task<CompositeRequestResponse> ExecuteCompositeRecords(List<CompositeSObject> sObjects, bool allOrNone = false, bool collateSubrequests = true, Dictionary<string, string> customHeaders = null)

Parameters

sObjects List<CompositeSObject>

Objects to update

allOrNone bool

Optional. Indicates whether to roll back the entire request when the update of any object fails (true) or to continue with the independent update of other objects in the request. The default is false.

collateSubrequests bool

Optional. Controls whether the API collates unrelated subrequests to bulkify them (true) or not (false). When subrequests are collated, the processing speed is faster, but the order of execution is not guaranteed (unless there is an explicit dependency between the subrequests).If collation is disabled, then the subrequests are executed in the order in which they are received. The default is true.

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

Returns

Task<CompositeRequestResponse>

List of UpdateMultipleResponse objects, includes response for each object (id, success, errors)

Exceptions

ArgumentException

Thrown when missing required information

ForceApiException

Thrown when update fails

GetAvailableRestApiVersions(string)

List summary information about each REST API version currently available, including the version, label, and a link to each version's root. You do not need authentication to retrieve the list of versions.

public Task<List<SalesforceVersion>> GetAvailableRestApiVersions(string currentInstanceUrl = null)

Parameters

currentInstanceUrl string

Current instance URL. If the client has been initialized, the parameter is optional and the client's current instance URL will be used

Returns

Task<List<SalesforceVersion>>

List of SalesforceVersion objects

GetObjectBasicInfo(string)

Retrieve (basic) metadata for an object.

Use the SObject Basic Information resource to retrieve metadata for an object.

public Task<SObjectBasicInfo> GetObjectBasicInfo(string objectTypeName)

Parameters

objectTypeName string

SObject name, e.g. Account

Returns

Task<SObjectBasicInfo>

Returns SObjectMetadataBasic with basic object metadata and a list of recently created items.

GetObjectById<T>(string, string, List<string>)

Get SObject by ID

public Task<T> GetObjectById<T>(string sObjectTypeName, string objectId, List<string> fields = null)

Parameters

sObjectTypeName string

SObject name, e.g. "Account"

objectId string

SObject ID

fields List<string>

(optional) List of fields to retrieve, if not supplied, all fields are retrieved.

Returns

Task<T>

Type Parameters

T

GetObjectDescribe(string)

Get field and other metadata for an object.

Use the SObject Describe resource to retrieve all the metadata for an object, including information about each field, URLs, and child relationships.

public Task<SObjectDescribeFull> GetObjectDescribe(string objectTypeName)

Parameters

objectTypeName string

SObject name, e.g. Account

Returns

Task<SObjectDescribeFull>

Returns SObjectMetadataAll with full object meta including field metadata

GetOrganizationLimits()

Lists information about limits in your org.

This resource is available in REST API version 29.0 and later for API users with the View Setup and Configuration permission.

public Task<OrganizationLimits> GetOrganizationLimits()

Returns

Task<OrganizationLimits>

GetUserInfo(string)

Get current user's info via Identity URL

https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/oauth_using_identity_urls.htm

public Task<UserInfo> GetUserInfo(string identityUrl)

Parameters

identityUrl string

Returns

Task<UserInfo>

UserInfo

InsertOrUpdateRecord<T>(string, string, string, T, Dictionary<string, string>, List<string>, bool)

Inserts or Updates a records based on external id

public Task<UpsertResponse> InsertOrUpdateRecord<T>(string sObjectTypeName, string fieldName, string fieldValue, T sObject, Dictionary<string, string> customHeaders = null, List<string> fieldsToNull = null, bool ignoreNulls = true)

Parameters

sObjectTypeName string

SObject name, e.g. "Account"

fieldName string

External ID field name

fieldValue string

External ID field value

sObject T

Object to update

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

fieldsToNull List<string>

A list of properties that should be set to null, but inclusing the null values in the serialized output

ignoreNulls bool

Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null

Returns

Task<UpsertResponse>

UpsertResponse object, includes new object's ID if record was created and no value if object was updated

Type Parameters

T

Exceptions

ForceApiException

Thrown when request fails

QueryAsync<T>(string, bool, int?, CancellationToken)

Retrieve a IAsyncEnumerable<T> using a SOQL query. Batches will be retrieved asynchronously.

When using the iterator, the initial result batch will be returned as soon as it is received. The additional result batches will be retrieved only as needed.

public IAsyncEnumerable<T> QueryAsync<T>(string queryString, bool queryAll = false, int? batchSize = null, CancellationToken cancellationToken = default)

Parameters

queryString string

SOQL query string, without any URL escaping/encoding

queryAll bool

Optional. True if deleted records are to be included.await Defaults to false.

batchSize int?

Optional. Size of result batches between 200 and 2000

cancellationToken CancellationToken

Optional. Cancellation token

Returns

IAsyncEnumerable<T>

IAsyncEnumerable<T> of results

Type Parameters

T

QuerySingle<T>(string, bool)

Retrieve a single record using a SOQL query.

Will throw an exception if multiple rows are retrieved by the query - if you are note sure of a single result, use Query{T} instead.

public Task<T> QuerySingle<T>(string queryString, bool queryAll = false)

Parameters

queryString string

SOQL query string, without any URL escaping/encoding

queryAll bool

True if deleted records are to be included

Returns

Task<T>

result object

Type Parameters

T

Query<T>(string, bool)

Retrieve records using a SOQL query.

Will automatically retrieve the complete result set if split into batches. If you want to limit results, use the LIMIT operator in your query.

public Task<List<T>> Query<T>(string queryString, bool queryAll = false)

Parameters

queryString string

SOQL query string, without any URL escaping/encoding

queryAll bool

True if deleted records are to be included

Returns

Task<List<T>>

List{T} of results

Type Parameters

T

Search(string)

Executes a SOSL search, returning a simple generic object in the results collection that primarly results in a list of object IDs

public Task<SearchResult<SObjectGeneric>> Search(string searchString)

Parameters

searchString string

Returns

Task<SearchResult<SObjectGeneric>>

SearchResult{SObjectGeneric}

Search<T>(string)

Executes a SOSL search, returning a type T, e.g. when using "RETURNING Account" in the SOSL query.

Not properly matching the return type T and the RETURNING clause of the SOSL query may return unexpected results

public Task<SearchResult<T>> Search<T>(string searchString)

Parameters

searchString string

Returns

Task<SearchResult<T>>

SearchResult{T}

Type Parameters

T

TestConnection(string)

Does a basic test of the client's connection to the current Salesforce instance, and that the API is responding to requests.

This does not validate authentication.

Makes a call to the Versions resource, since it requires no authentication or permissions.

public bool TestConnection(string currentInstanceUrl = null)

Parameters

currentInstanceUrl string

Instance URL. Defaults to the client's current instance, this would typically only need to be specified if it is needed to test the connection to a different SFDC instance.

Returns

bool

True or false. Does not throw exceptions, only false in case of any errors.

UpdateRecord<T>(string, string, T, Dictionary<string, string>, List<string>, bool)

Update single record

public Task UpdateRecord<T>(string sObjectTypeName, string objectId, T sObject, Dictionary<string, string> customHeaders = null, List<string> fieldsToNull = null, bool ignoreNulls = true)

Parameters

sObjectTypeName string

SObject name, e.g. "Account"

objectId string

Id of Object to update

sObject T

Object to update

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

fieldsToNull List<string>

A list of properties that should be set to null, but inclusing the null values in the serialized output

ignoreNulls bool

Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null

Returns

Task

void, API returns 204/NoContent

Type Parameters

T

Exceptions

ForceApiException

Thrown when update fails

UpdateRecords(List<SObject>, bool, Dictionary<string, string>, List<string>, bool)

Update multiple records. The list can contain up to 200 objects. The list can contain objects of different types, including custom objects. Each object must contain an attributes map. The map must contain a value for type. Each object must contain an id field with a valid ID value.

public Task<List<UpsertResponse>> UpdateRecords(List<SObject> sObjects, bool allOrNone = false, Dictionary<string, string> customHeaders = null, List<string> fieldsToNull = null, bool ignoreNulls = true)

Parameters

sObjects List<SObject>

Objects to update

allOrNone bool

Optional. Indicates whether to roll back the entire request when the update of any object fails (true) or to continue with the independent update of other objects in the request. The default is false.

customHeaders Dictionary<string, string>

Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.

fieldsToNull List<string>

A list of properties that should be set to null, but inclusing the null values in the serialized output

ignoreNulls bool

Use with caution. By default null values are not serialized, this will serialize all explicitly nulled or missing properties as null

Returns

Task<List<UpsertResponse>>

List of UpsertResponse objects, includes response for each object (id, success, errors)

Exceptions

ArgumentException

Thrown when missing required information

ForceApiException

Thrown when update fails