3

Afternoon!

I've been trying to find some information as in how to add a certain email to a suppression list in Marketing Cloud via Apex, but at the end of the day, I haven't found any information I could really understand.

I've seen the SOAP envelopes in the Marketing Cloud API docs, but I don't really know how to work with those envelopes or to be honest how to actually make the callout since I've only developed integrations using REST APIs.

If you guys could provide me some insight into how to connect to the Mkt Cloud API as well as use the envelopes provided by the docs, it'd be great! :)

Thanks in advance!

  • developer.salesforce.com/docs/… this is the api reference. you can use the rest api if you want, but you don't have the same features in the rest and soap api – Torsen Dec 11 '17 at 14:55
  • @Torsen hey :) I figured some time ago that I can can authenticate using the auth.exacttargetapis.com/v1/requestToken endpoint. The problem is I don't really understand how envelopes are supposed to be used. I'm trying to use the SOAP API. – Javier García Manzano Dec 11 '17 at 15:20
  • Are you just interested in how to implement SOAP Calls in Apex? – Patlatus Dec 11 '17 at 15:21
7
+50

So first of all you need to perform login action to retrieve fuel OAuth token.

Request format is following due to the documentation

POST https://auth.exacttargetapis.com/v1/requestToken
Content-Type: application/json
{
    "clientId": "YOUR_CLIENT_ID",
    "clientSecret": "YOUR_CLIENT_SECRET"
}

Response is following:

200 OK
{
    "accessToken": "YOUR_ACCESS_TOKEN"
    "expiresIn": 3600
}

To perform such callouts, you need some Apex class with the following method

private static String makeSimpleJSONPostCall(String endPoint, String soapBody){
    Http h = new Http();
    HttpRequest r = new HttpRequest();
    r.setTimeout(60000);
    r.setEndpoint(endPoint);  
    r.setMethod('POST');
    r.setHeader('Content-type','application/json');    
    r.setBody(soapBody);    
    HttpResponse res = h.send(r);
    return r.getBody();
}

Now you can perform get OAuth token request using the following form

private static String MARKETING_CLOUD_AUTH_URL = 'https://auth.exacttargetapis.com/v1/reaquestToken';
private static String ACCESS_TOKEN = 'accessToken';
private static String getMarketingCloudOAuthToken(String clientId, String clientSecret){
    String responseBody = makeSimpleJSONPostCall(
        MARKETING_CLOUD_AUTH_URL,
        JSON.serialize( new Map<String, String>{
            'clientId' => clientId,
            'clientSecret' => clientSecret
        } )
    );
    return ((Map<String, String>) JSON.deserialize(responseBody, Map<String, String>.class)).get( ACCESS_TOKEN ); 
}

The method above assumes successful authorization. You might consider case when res.getStatusCode() != 200, in such case method getMarketingCloudOAuthToken might throw an exception.

Next, you need to send SOAP Envelope example from the document you provided

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Header>
      <Security xmlns="https://www.marketingcloud.com/">
         <fueloauth>YOUR_ACCESS_TOKEN</fueloauth>
      </Security>
   </Header>
   <Body>
      <UpdateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
         <Objects xsi:type="Subscriber">
            <PartnerKey xsi:nil="true"/>
            <ObjectID xsi:nil="true"/>
            <EmailAddress>example@example.com</EmailAddress>
            <Lists>
               <PartnerKey xsi:nil="true"/>
               <ObjectID xsi:nil="true"/>
               <ID>123456</ID>
               <Status>Active</Status>
               <Action>create</Action>
            </Lists>
         </Objects>
      </UpdateRequest>
   </Body>
</Envelope>

You just need to perform HTTPCallout from Apex like this

private static String makeHTTPXMLPost(String endPoint, String soapBody){
    Http h = new Http();
    HttpRequest r = new HttpRequest();
    r.setTimeout(60000);
    r.setEndpoint(endPoint);  
    r.setMethod('POST');
    r.setHeader('SOAPAction','DoesNotMatter'); 
    r.setHeader('Accept','text/xml');  
    r.setHeader('Content-type','text/xml');    
    r.setHeader('charset','UTF-8'); 
    reqLLogin.setBody(soapBody);    
    HttpResponse s = h.send(r);
    return s.getBody();
}

You can use makeHTTPXMLPost method in the following format

private static String performMCAction(String accessToken, String email, String listId, String status, String action){
    String soapEnvelopeTemplate = '    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
    '       <Header>'+
    '          <Security xmlns="https://www.marketingcloud.com/">'+
    '             <fueloauth>{0}</fueloauth>'+
    '          </Security>'+
    '       </Header>'+
    '       <Body>'+
    '          <UpdateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">'+
    '             <Objects xsi:type="Subscriber">'+
    '                <PartnerKey xsi:nil="true"/>'+
    '                <ObjectID xsi:nil="true"/>'+
    '                <EmailAddress>{1}</EmailAddress>'+
    '                <Lists>'+
    '                   <PartnerKey xsi:nil="true"/>'+
    '                   <ObjectID xsi:nil="true"/>'+
    '                   <ID>{2}</ID>'+
    '                   <Status>{3}</Status>'+
    '                   <Action>{4}</Action>'+
    '                </Lists>'+
    '             </Objects>'+
    '          </UpdateRequest>'+
    '       </Body>'+
    '    </Envelope>';
    String body = String.format(template, new String[]{accessToken, email, listId, status, action});
    return makeHTTPXMLPost( 'https://auth.exacttargetapis.com/' , body );
}

I hope this would help.

Important point: to make Apex HTTP Callout work, you need to add marketing cloud URL to remote site settings. You can do this following this link:

https://login.salesforce.com/0rp/e?EndpointUrl=https://auth.exacttargetapis.com/&SiteName=MarketingCloud

for production and developer edition instances or this link

https://test.salesforce.com/0rp/e?EndpointUrl=https://auth.exacttargetapis.com/&SiteName=MarketingCloud

for sandboxes and after you follow this link click save to add marketing cloud URL to remote site settings.

Not the answer you're looking for? Browse other questions tagged or ask your own question.