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
authInfoAuthInfo
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
instanceUrlstringIdentifies the Salesforce instance to which API calls should be sent.
apiVersionstringSalesforce API version
accessTokenstringAccess token
httpClientHttpClientOptional 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.
accessInfoAccessTokenResponseAccessTokenResponse 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
clientIdstringClient ID, a.k.a. Consumer Key
clientSecretstringClient Secret, a.k.a. Consumer Secret
usernamestringSalesforce username
passwordstringSalesforce password
tokenRequestEndpointstringToken request endpoint
apiVersionstringSalesforce API version
httpClientHttpClientOptional 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
AccessToken
public string AccessToken { get; }
Property Value
ApiVersion
public string ApiVersion { get; }
Property Value
ClientName
public string ClientName { get; set; }
Property Value
InstanceUrl
public string InstanceUrl { get; }
Property Value
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
blobUrlstringrelative blob URL
Returns
BlobRetrieveStream(string, string, string)
public Task<Stream> BlobRetrieveStream(string sObjectTypeName, string objectId, string blobField)
Parameters
Returns
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
queryStringstringSOQL query string starting with SELECT COUNT() FROM
queryAllboolTrue 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
sObjectTypeNamestringSObject name, e.g. "Account"
sObjectsList<SObject>Objects to create. Each sObject must have the entity type and reference id in the attributes property object.
autoFillAttributesboolAutomatically create attribute object property, reference Id will be the zero-based index of the array
customHeadersDictionary<string, string>Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.
fieldsToNullList<string>A list of properties that should be set to null, but inclusing the null values in the serialized output
ignoreNullsboolUse 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
sObjectTypeNamestringSObject name, e.g. "Account"
sObjectTObject to create
customHeadersDictionary<string, string>Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.
fieldsToNullList<string>A list of properties that should be set to null, but inclusing the null values in the serialized output
ignoreNullsboolUse 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
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
apexResourceUrlstringThe URL of the apex resource. Ex: /services/apexrest/DuplicateCheck should provide "DuplicateCheck"
requestRequestThe custom object to include in the request
customHeadersDictionary<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
RequestT
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
sObjectsList<CompositeSObject>Objects to update
allOrNoneboolOptional. 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.
collateSubrequestsboolOptional. 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.
customHeadersDictionary<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
currentInstanceUrlstringCurrent 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
objectTypeNamestringSObject 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
sObjectTypeNamestringSObject name, e.g. "Account"
objectIdstringSObject ID
fieldsList<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
objectTypeNamestringSObject 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
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
identityUrlstring
Returns
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
sObjectTypeNamestringSObject name, e.g. "Account"
fieldNamestringExternal ID field name
fieldValuestringExternal ID field value
sObjectTObject to update
customHeadersDictionary<string, string>Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.
fieldsToNullList<string>A list of properties that should be set to null, but inclusing the null values in the serialized output
ignoreNullsboolUse 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
queryStringstringSOQL query string, without any URL escaping/encoding
queryAllboolOptional. True if deleted records are to be included.await Defaults to false.
batchSizeint?Optional. Size of result batches between 200 and 2000
cancellationTokenCancellationTokenOptional. 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
queryStringstringSOQL query string, without any URL escaping/encoding
queryAllboolTrue 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
queryStringstringSOQL query string, without any URL escaping/encoding
queryAllboolTrue if deleted records are to be included
Returns
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
searchStringstring
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
searchStringstring
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
currentInstanceUrlstringInstance 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
sObjectTypeNamestringSObject name, e.g. "Account"
objectIdstringId of Object to update
sObjectTObject to update
customHeadersDictionary<string, string>Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.
fieldsToNullList<string>A list of properties that should be set to null, but inclusing the null values in the serialized output
ignoreNullsboolUse 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
sObjectsList<SObject>Objects to update
allOrNoneboolOptional. 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.
customHeadersDictionary<string, string>Custom headers to include in request (Optional). await The HeaderFormatter helper class can be used to generate the custom header as needed.
fieldsToNullList<string>A list of properties that should be set to null, but inclusing the null values in the serialized output
ignoreNullsboolUse 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