Skip to content
Michael Shattuck edited this page Feb 27, 2015 · 2 revisions

Using the SOAP API in SalesforceMagic is (almost ridiculously) simple.

Retrieving Data

First let's get a count of the all ExampleAttachments in our Salesforce instance.

The following method allows us to accomplish this:

int Count<T>(Expression<Func<T, bool>> predicate = null) where T : SObject;

Usage:

int attachmentsCount = client.Count<ExampleAttachment>();

The count method also accepts a lamba expression to filter counts. Let's grab a count of all ExampleAttachments that have the S3Id set.

Example:

int filteredCount = client.Count<ExampleAttachment>(x => x.S3Id != null);

Now let's actually perform the query, we'll grab 5 ExampleAttachments and set a condition to ensure that the records we retrieve have the S3Id field set:

SalesforceMagic provides a few different methods for querying data:

/// <summary>
///     Simple Query
///      - Query items based on generic object
///      - Limited by 200 records
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="limit"></param>
/// <returns></returns>
IEnumerable<T> Query<T>(int limit = 0) where T : SObject;

/// <summary>
///     Simple Query
///      - Query items based on generic object
///      - Generate query using predicate
///      - Limited by 200 records
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <param name="limit"></param>
/// <returns></returns>
IEnumerable<T> Query<T>(Expression<Func<T, bool>> predicate, int limit = 0) where T : SObject;

/// <summary>
///     Simple Query
///      - Query items based on generic object
///      - Utilize included raw query
///      - Limited by 200 records
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <returns></returns>
IEnumerable<T> Query<T>(string query);

/// <summary>
///     Advanced Query
///      - Query items based on generic object
///      - Returns query locator, and done status which
///        can be used to bypass the 200 record limit.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="limit"></param>
/// <returns></returns>
QueryResult<T> AdvancedQuery<T>(int limit = 0) where T : SObject;

/// <summary>
///     Advanced Query
///      - Query items based on generic object
///      - Generate query using predicate
///      - Returns query locator, and done status which
///        can be used to bypass the 200 record limit.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <param name="limit"></param>
/// <returns></returns>
QueryResult<T> AdvancedQuery<T>(Expression<Func<T, bool>> predicate, int limit = 0) where T : SObject;

/// <summary>
///     Advanced Query
///      - Query items based on generic object
///      - Utilize included raw query
///      - Returns query locator, and done status which
///        can be used to bypass the 200 record limit.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <returns></returns>
QueryResult<T> AdvancedQuery<T>(string query);

/// <summary>
///     Query More
///      - Used to retrieve the next set of records
///        available in a query using the queryLocator.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="queryLocator"></param>
/// <returns></returns>
QueryResult<T> QueryMore<T>(string queryLocator);

/// <summary>
///     Query Single
///      - Used to retrieve a single record
///        using filter criteria
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="predicate"></param>
/// <returns></returns>
T QuerySingle<T>(Expression<Func<T, bool>> predicate) where T : SObject;

/// <summary>
///     Query Single
///      - Used to retrieve a single record
///        using a raw string query
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <returns></returns>
T QuerySingle<T>(string query);

Example

var attachments = client.Query<ExampleAttachments>(x => x.S3Id != null, limit: 5);

Let's also go over the use of CRUD operations. First let's create a list of objects we can use.

SOAP API: Using the SOAP api for CRUD operations is very simple. It can easily deal with individual sObjects or an array.

SalesforceResponse Crud<T>(CrudOperation<T> operation) where T : SObject;
SalesforceResponse Insert<T>(IEnumerable<T> items) where T : SObject;
SalesforceResponse Insert<T>(T item) where T : SObject;
SalesforceResponse Upsert<T>(IEnumerable<T> items, string externalIdField) where T : SObject;
SalesforceResponse Upsert<T>(T item, string externalIdField) where T : SObject;
SalesforceResponse Update<T>(IEnumerable<T> items) where T : SObject;
SalesforceResponse Update<T>(T item) where T : SObject;
SalesforceResponse Delete<T>(IEnumerable<T> items) where T : SObject;
SalesforceResponse Delete<T>(T item) where T : SObject;

TODO: More documentation is needed here

Clone this wiki locally