SmartConnect 2018
Salesforce eOne_SC_RTDS
The following class is created when a Salesforce real time data source is registered inside SmartConnect. It is used to process the inserts and updates triggered by Salesforce real time data source triggers.
public class eOne_SC_RTDS
{
// all callouts will be made asyncronously
@future (callout=true)
public static void Process(string entity, string action, set<ID> ids)
{
// retrieve the master record associated with the entity passed in
eOne_RTDS_Master__c recd = [select Name,SourceId__c,Query__c,IsCreate__c,IsUpdate__c,IsDelete__c from eOne_RTDS_Master__c where Name = :entity LIMIT 1];
// if there is no master record linked to the entity stop processing
if(recd == null) return;
// if the entity is not registered for the triggering process stop processing
if(recd.IsCreate__c == false && action == 'create') return;
if(recd.IsUpdate__c == false && action == 'update') return;
if(recd.IsDelete__c == false && action == 'delete') return;
// create a variable to receive the parsed data source query
string queryReplace = '';
string query = recd.Query__c;
// cater for more than one record update / insert triggering the data source
for(ID id : ids)
{
if(queryReplace.length() == 0)
queryReplace += String.valueOf(id);
else
queryReplace += '\',\'' + String.valueOf(id);
}
// replace the empty id in the query with the id(s) from the trigger
query = query.replace('000000000000000000',queryReplace);
// run the query against Salesforce
List<sObject> results = Database.query(query);
// check the record(s) returned by the query. If no records stop processing
if(results.size() == 0) return;
// serialize the query results using the default Salesforce JSON serializer
String xml = JSON.serialize(results);
// read the real time data source settings to determine how to connect to the SmartConnect WCF REST service
eOne_RTDS_Settings__c settings = [select Name,Url__c,Domain__c,Username__c,Password__c from eOne_RTDS_Settings__c where Name = '0'];
// if a settings record is not found stop processing
if(settings == null) return;
// create a new HttpRequest
HttpRequest req = new HttpRequest();
// set the endpoint. method and timeout for the request
req.setEndpoint(settings.Url__c + '/sf/' + recd.SourceId__c + '/' + action);
req.setMethod('POST');
req.setTimeout(120000);
// create and attach the user credentials read from the settings table
string username = settings.Domain__c;
if(username.length() != 0) username += '\\\\';
username += settings.Username__c + ':' + settings.Password__c;
Blob headerValue = Blob.valueOf(username);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// place the query xml into the body of the web request
req.setBody(xml);
// send the web request
Http http = new Http();
HttpResponse res = http.send(req);
}
}
Note: Comments and code indenting are not included in the object created in Salesforce