S4SLB Code Samples (Pre-Sitecore 9)

When you query a Salesforce for a large number of object records, often you will only get 2000 records. To retrieve all the records requires you programmatically check the response object to see if there are more records. S4S encapsulates this in its EntitysFromQueryResult() method as seen in the example below. Note the method has the returnAll parameter set to true. 

Sample code snippet for retrieve Salesforce campaign members of multiple Salesforce campaigns via S4S.

CampaignMemberDataSource campaignMemberDataSource = new CampaignMemberDataSource(salesforceSession);

//Generate filter to omit deleted campaign members
campaignMemberDataSource.AddDataSourceFilter(CampaignMember.Fields.IsDeleted.Name,ComparisonOperator.Equals, false);

//Generate filter to get campaign members from comma separated campaignIds 
campaignMemberDataSource.AddDataSourceFilter(CampaignMember.Fields.CampaignId.Name, Operator.OperatorFor(ComparisonOperator.In), campaignIdString.Split(','));

//Narrow down the fields to retrieve from the query
string[] requiredFields = new List<string>(new[] { "Id", "ContactId", "LeadId", "CampaignId", "Campaign.Name", "Lead.Email", "Contact.Email", "Contact.FirstName", "Contact.LastName", "Lead.FirstName", "Lead.LastName" }); 

QueryResult result = campaignMemberDataSource.RunQuery(requiredFields);

List<CampaignMember> campaignMembers = campaignMemberDataSource.EntitysFromQueryResult<CampaignMember>(result, true);

SalesforceDataSource.EntitysFromQueryResult<T>(QueryResult queryResult) now takes an optional boolean parameter returnAll (defaults to false). When false, only the records in the current query result will be converted to entities. When true, all records in the query will be added to the list. This will automatically page through all the QueryResults.

There are other ‘EntitysFromQueryResult’ methods in S4S which can use to retrieve data with paging options as follows.

//Return dataset between startIndex and endIndex
public List<T> EntitysFromQueryResultPager<T>(QueryResultPager queryResultPager, int startIndex, int endIndex);

//Return dataset according to the pageIndex and pageSize
public List<T> EntitysFromQueryResultPaged<T>(QueryResult queryResult, int pageIndex, int pageSize);