0

I have to write apex code on salesforce service/sales cloud to update dynamic list of Subscribers status on Marketing cloud (SFMC), I will be having list of contactkey from marketing cloud (SOQL query for an object)

I have done some search and found this is achievable using Integration with Marketing Cloud using SOAP API (looks like REST is not supported to update subscriber status)

The link below has some c# and Java examples (but not Apex code) https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/update.htm

And I have some experience on REST calls, but new to SOAP integration. (I have postman client and have tested rest successfully)

Need help step by step from having the correct endpoints, and creating dynamic soap envelop etc using Apex code

Please help

0

@A Iqbal try this one create two classes one is testet class and second one is testintegration class this is code

   public with sharing class testet
         {  
 public static String getHeader2()
   {
     string soapMessage='';
     soapMessage='<soapenv:Header>';
  soapMessage+='<Security xmlns="'+'http://docs.oasis-open.org/wss/2004/01/oasis- 
 200401-wss-wssecurity-secext-1.0.xsd'+'">';           
 soapMessage+='<UsernameToken>';
            soapMessage+='<Username>'+'mc username here'+'</Username>';
            soapMessage+='<Password>'+'****'+'</Password>';
        soapMessage+='</UsernameToken>';
        soapMessage+='</Security>';
      soapMessage+='</soapenv:Header>';



    return soapMessage;
  }
public static Dom.Document processRequest(String etend, String respone, String action) {
    Dom.Document doc;
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');
    req.setHeader('Content-Type', 'text/xml');
    req.setHeader('SOAPAction', action);
    req.setEndpoint(etend);
    req.setBody(respone);
    HttpResponse res;

        res = h.send(req);   
        System.debug('res:'+res.getBody());

    return doc;
    }
 }
  ---------------------------------------------
 Second class

         public with sharing class testintegration
          {
       public static string endpoint;
       public static string partnerurl= 'http://exacttarget.com/wsdl/partnerAPI';
    public static string soapurl= '<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
   xmlns:par="'+partnerurl+'" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
 instance">';
    @future(callout=true)
public static void sendData()
{
    endpoint ='https://webservice.s7.exacttarget.com/Service.asmx';
    String soapMessage = '';
       soapMessage+=soapurl;
      soapMessage+=testet.getHeader2();


           soapMessage+='<soapenv:Body>';
                soapMessage+='<UpdateRequest xmlns="'+partnerurl+'">';

                   soapMessage+='<Options>';
                   soapMessage+='<SaveOptions>';
                   soapMessage+='<SaveOption>';
                   soapMessage+='<PropertyName>'+'*'+'</PropertyName>';
                   soapMessage+='<SaveAction>'+'UpdateAdd'+'</SaveAction>';
                   soapMessage+='</SaveOption>';                                                                                                                          
                    soapMessage+='</SaveOptions>';
                    soapMessage+='</Options>';
                soapMessage+='<Client>';
                       soapMessage+='<ID>'+*****+'</ID>';
                       soapMessage+='</Client>';  
                        soapMessage+='<Objects xsi:type="Subscriber">';

                            soapMessage+='<EmailAddress>'+'*****'+'</EmailAddress>';

                            soapMessage+='<SubscriberKey>'+'****'+'</SubscriberKey>';
                        soapMessage+='<Attributes>'; 
                    soapMessage+='<Name>Email Address</Name><Value>'+'*****'+'</Value>';                            
                    soapMessage+='</Attributes>';
                    soapMessage+='<Status>'+'Unsubscribed'+'</Status>';
                    soapMessage+='</Objects>';
                soapMessage+='</UpdateRequest>';
           soapMessage+='</soapenv:Body>';
       soapMessage+='</soapenv:Envelope>';
       System.debug('ReqMessage:'+soapMessage);


      Dom.document response = Etprocess.processRequest(endpoint, soapMessage, 'Update');
      System.debug('Response:'); 
}
  }
0

//My working code that might help others //Note:Please create a class and insert below 2 methods //Calling Marketing Cloud 'UpdateRequest' to update subscriber status //Please write code to get access token (or look for a sample)

public static HttpResponse updateMCSubscriber(String email,String subscriberKey, String listId, String status, String action){

    string soapEndpoint='https://<YOURMCINSTANCE>.soap.marketingcloudapis.com/Service.asmx';

    //----------------------------------note below--------------
    //Please write code to get the access token here alternatively you can use username and password as as show by "pkharries"
    string token='';//getToken();

    String soapEnvelopeTemplate = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
    soapEnvelopeTemplate+='<Header>';
    soapEnvelopeTemplate+='<fueloauth>'+token+'</fueloauth>';
    soapEnvelopeTemplate+='</Header>';
    soapEnvelopeTemplate+='<Body>';
    soapEnvelopeTemplate+='<UpdateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">';
    soapEnvelopeTemplate+='<Objects xsi:type="Subscriber">';
    soapEnvelopeTemplate+='<PartnerKey xsi:nil="true"/>';
    soapEnvelopeTemplate+='<ObjectID xsi:nil="true"/>';
    soapEnvelopeTemplate+='<EmailAddress>'+email+'</EmailAddress>';
    soapEnvelopeTemplate+='<SubscriberKey>'+subscriberKey+'</SubscriberKey>';
    soapEnvelopeTemplate+='<Lists>';
    soapEnvelopeTemplate+='<PartnerKey xsi:nil="true"/>';
    soapEnvelopeTemplate+='<ObjectID xsi:nil="true"/>';
    soapEnvelopeTemplate+='<ID>'+listId+'</ID>';
    soapEnvelopeTemplate+='<Status>'+status+'</Status>';
    soapEnvelopeTemplate+='<Action>'+action+'</Action>';
    soapEnvelopeTemplate+='</Lists>';
    soapEnvelopeTemplate+='</Objects>';
    soapEnvelopeTemplate+='</UpdateRequest>';
    soapEnvelopeTemplate+='</Body>';
    soapEnvelopeTemplate+='</Envelope>';

    String body = String.format(soapEnvelopeTemplate, new String[]{token, email,subscriberKey, listId, status, action});
    //System.debug('body:' + body);
    return makeHTTPXMLPost(soapEndpoint, body,action);  
}  
0
private static HttpResponse makeHTTPXMLPost(String endPoint, String soapBody,String SOAPAction){
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setTimeout(60000);
    req.setEndpoint(endPoint);  
    req.setMethod('POST');
    req.setHeader('SOAPAction',SOAPAction); 
    req.setHeader('Accept','text/xml');  
    req.setHeader('Content-type','text/xml');    
    req.setHeader('charset','UTF-8'); 
    req.setBody(soapBody);    
    HttpResponse res = http.send(req);
    //return s.getBody();
    return res;
}
0

//code to test implementation

String email='askaslam@gmail.com';

string skey='0030N50001dsItqQAE';

String listId='71'; String status='Active'; String action='Update';

//status='Unsubscribed';

HttpResponse res=updateMCSubscriber(email, skey,listId, status, action);

System.debug('getStatus():' + res.getStatus());

System.debug('getStatusCode():' + res.getStatusCode());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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