But, if I do this, I get the subscriber ID as contactId in SendLog. I want the subscriber Id as ID of a custom object that we are using as subscriber between SFMC and SFDC.
You should use the subscriber Id of salesforce, it will be needed if you want to take advantage of all the JourneyBuilder's activities it has.
can REST API be used for the same, and if yes how?
Yes it can be used, you should not use the user/password for SOAP request but the token retrieved in the REST API call described in: https://developer.salesforce.com/docs/atlas.en-us.mc-getting-started.meta/mc-getting-started/requestToken.htm
Then you can use the code in a SOAP API call:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://exacttarget.com/wsdl/partnerAPI">
<SOAP-ENV:Header>
<fueloauth xmlns="http://exacttarget.com">YOUR_TOKEN</fueloauth>
</SOAP-ENV:Header>
[...]
Or in the any REST call as usual in the Authorization header.
You can embed your rest api call in APEX like (for retrieving the token):
private final String CONTENT = 'Content-Type';
private final String AUTHORIZATION = 'Authorization';
private final String BEARER = 'Bearer ';
private final String FORM_URLENCODED = 'application/x-www-form-urlencoded';
private final Integer CODE_200_OK = 200;
private final String ACCESS_TOKEN = 'accessToken';
private final String EXPIRES_IN = 'expiresIn';
private final String METHOD_POST = 'POST';
public final static String LOGIN_URL = '<YOUR_SUBDOMAIN>.auth.marketingcloudapis.com/v1/requestToken';//OLD url is https://auth.exacttargetapis.com/v1/requestToken
public HttpResponse performRequest (String method, String url, Map<String, String> headerMap, String body){
HttpResponse response = null;
HttpRequest httpRequest = new HttpRequest();
Http httpClient = new Http();
httpRequest.setEndpoint(url);
httpRequest.setMethod(method);
if(headerMap != null){
for(String headerName : headerMap.keySet()){
httpRequest.setHeader(headerName, headerMap.get(headerName));
}
}
if(String.isNotEmpty(body)) httpRequest.setBody(body);
httpRequest.setTimeout(TIMEOUT);
response = httpClient.send(httpRequest);
return response;
}
@Future(callout=true)
private String doLogin(String clientID, String clientSecret){
String body;
String accessToken;
Map<String, String> headers = new Map<String, String>();
body = 'clientId=' + clientID + '&clientSecret=' + clientSecret;
headers.put(CONTENT, FORM_URLENCODED);
HttpResponse response = performRequest(METHOD_POST, LOGIN_URL, headers, body);
if(response.getStatusCode() == CODE_200_OK){
Map<String, Object> responseBody = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
accessToken = (String) responseBody.get(ACCESS_TOKEN);
expiresIn = (Integer) responseBody.get(EXPIRES_IN);
}
return accessToken;
}
For sending Triggered emails you have do as its in the documentation: https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/messageDefinitionSends.htm and the code in apex looks like:
private final String TRIGGERED_EMAIL_BODY = '{ '+
'"To": { '+
'"Address": "{emailAddress}",'+
'"SubscriberKey": "{subscriberKey}", '+
' "ContactAttributes": { '+
' "SubscriberAttributes": { '+
' {attributes} '+
' }'+
' }'+
'},'+
'"OPTIONS": { '+
' "RequestType": "ASYNC" '+
'}'+
'}';
private final String ACCESS_TOKEN = 'accessToken';
private final String SEND_MAIL_URL = '/messaging/v1/messageDefinitionSends/key:{triggeredEmailKey}/send';
private final String BASE_URL = '<YOUR_SUBDOMAIN>';
private final String APPLICATION_JSON = 'application/json';
public HttpResponse sendTriggeredEmail (String triggeredEmailKey, String emailAddress, String subscriberKey, Map<String, String> attributesMap, String token){
String token = getAccessToken();
String method = METHOD_POST;
String url = BASE_URL + SEND_MAIL_URL.replace('{triggeredEmailKey}', triggeredEmailKey);
Map<String, String> headerMap = new Map<String, String>();
String attributes = '';
String body ;
if(attributesMap != null && attributesMap.size() > 0){
for(String attr : attributesMap.keySet()){
attributes += '"' + attr + '" : "' + attributesMap.get(attr) + '" ,';
}
attributes = attributes.removeEnd(' ,');
}
body = TRIGGERED_EMAIL_BODY.replace('{emailAddress}', emailAddress).
replace('{subscriberKey}',subscriberKey).
replace('{attributes}', attributes);
headerMap.put(CONTENT, APPLICATION_JSON);
headerMap.put(AUTHORIZATION, BEARER + token);
HttpResponse response = performRequest(method, url, headerMap, body);
return response;
}