Create Multiple Records in Salesforce with One API Call

S4S includes what we call disparate updates. This is where you can update multiple objects in Salesforce with a single API call which will help keep the API count down. The following code shows how the DisparateSalesforceService can send multiple entities, in this case, analytics objects, to Salesforce.

The highlighted code shows where the multiple entries are populated. The code can be included in a custom WFFM Submit Action or modified for use with a Sitecore custom form. The implementing class, FuseIT.S4S.WebToSalesforce.WebToEnitity, is used as follows:


//sfEntityName can be "Lead" or "Contact"
GenericSalesforceEntity genericEntity = new GenericSalesforceEntity(sfEntityName);
FieldService fieldService = FieldService.Instance(sfEntityName, submitAction.SalesforceSessionInstance);
List<SalesforceField> salesforceFields = fieldService.FieldsForObject();

//Set WFFM form field vales into Salesforce entity fields according to mapping details
submitAction.SetEntity(ref genericEntity, fields, salesforceFields, mapping);

WebToEntity webToSfEntity = new WebToEntity();

//Get current sitecore contact's visitor id
string visitorid = webToSfEntity.GetCurrentVisitorId();

Int32 totalVisitsValue = 0;
Int32 totalPageCount = 0;
string goalReached = string.Empty;

//Get total visit value, total page count and goals reached for the relavant sitecore visitor id
if (webToSfEntity.GetVisitsData(visitorid, out totalVisitsValue, out totalPageCount, out goalReached))
{
    //Update genericEntity object with above values
	webToSfEntity.UpdateSfEntityAnalyticsFields(genericEntity, fieldService, visitorid, totalVisitsValue, totalPageCount, goalReached);
}


SaveResult saveResult;
try
{
	//Save Salesforce entity (Contact or Lead)
	GenericSalesforceService genericSalesforceService = submitAction.GenericSalesforceServiceInstance(sfEntityName);
    saveResult = genericSalesforceService.Save(genericEntity);
    idValue = saveResult.id;

    List<SitecoreVisit> sitecoreVisits;

    //Get Sitecore contact's visit information
    if (webToSfEntity.GetContactData(visitorid, numVisits, out sitecoreVisits))
    {
        try
		{
			//Get a list of GenericSalesforceEntity objects which has been created using Sitecore visit information. This list includes Sitecore visits, goals, profiles and patterncards as GenericSalesforceEntity objects 			
			List<EntityBase> entities = webToSfEntity.UpdateSitecoreAnalyticsInSalesforce(idValue, sfEntityName, sitecoreVisits);
            DisparateSalesforceService disparateSalesforceService = new DisparateSalesforceService(submitAction.SalesforceSessionInstance);

			//Insert records in batch
            SaveResult[] saveResults = disparateSalesforceService.InsertEntities(entities);
        }
        catch (SalesforceException sfEx)
        {
            if (sfEx.GetType() != typeof(SalesforceCreateException) && sfEx.Message.Contains("DUPLICATE_VALUE"))
            {
                Logging.Error(this, "Update Analytics failed with Salesforce Exception.", sfEx);
                throw;
            }
		}
	}
    else
    {
        Logging.Warn(this, "Unable to get Analytics data for contact Id: " + visitorid);
    }   
}
catch (Exception ex)
{
     … 
}