function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Casey Hardesty 10Casey Hardesty 10 

Best way to add a subscriber to Exact Target List from Trigger

So, I'm tasked with a project and need some general guidance. Basically, here is what I am trying to accomplish...

1. Our Org has a custom object that when created, I would like to fire off a trigger
2. The Trigger (or class that the trigger calls) would then find the subscriber (because they should already exist in Exact Target) and add that subscriber to an existing List in Exact Target. 

My question is... Can this be done with Apex code? I've read the documentation around adding subscribers to a list using the API but I haven't found any examples using APEX. I guess the other option would be to Invoke a Callout?

Thoughts? Thanks in advance for your time and effort.

Casey 
Best Answer chosen by Casey Hardesty 10
KevinPKevinP
Casey,

This can def. be done. You're talking about making HTTP Callouts to the exact target REST api from within Apex. I've done this a couple of times, and the example code I've posted below is for sending a push notification, but you can see how authentication is done, and how making api requests is done.
 
public class MCPushMessageUtils {
    private Static accessTokenResult authToken;
    private Static String MessageId    = 'someMessageId';
    private Static String ClientId     = 'clientId';
    private Static String ClientSecret = 'ClientSecret';
    
	// Helper Methods and classes  
	public class accessTokenResult {
		public String accessToken;
		public Integer expiresIn;
  	}
    
    private Static String generateMessageSendBody(String CommentBody){
 		return '{' +
      	'"MessageText": "'+CommentBody+'",' +
      	'"Override": true,'+
      	'"sound": "default",'+
      	'"SendTime": "2012-10-31 09:00",'+
      	'"content-available": 1'+
    	'}';
  	}   
 
	public Static Void getAuthToken() {
    	sfmc__c creds = sfmc__c.getInstance('Pusher');
    	String authBody = '{"clientId":"'+ClientId+'","clientSecret":"'+ClientSecret+'"}';
    	system.debug(authBody);
    	HttpResponse res = sendHTTPRequest('POST',
        	                               authBody,
            	                           'https://auth.exacttargetapis.com/v1/requestToken');
	    authToken = (accessTokenResult) System.JSON.deserialize(res.getBody(), accessTokenResult.class);
 	}
  
  	@future(callout=true)
  	Public Static Void triggerSend(String CommentBody){
    	getAuthToken();
    	system.debug(authToken.accessToken);
    	HttpResponse res = sendHTTPRequest('POST', 
        	                               generateMessageSendBody(CommentBody), 
            	                           'https://www.exacttargetapis.com/push/v1/messageApp/'+MessageId+'/send',
                	                       TRUE);
	    // Helpful debug tip for InvocableMethods
	    // Divide by zero to intentionally throw an error and cause the logs to be written
	    // Integer x = 1 / 0;
  	}
 
  	Public Static HttpResponse sendHTTPRequest(String Method, String Body, String URL){
    	return sendHTTPRequest(Method, Body, URL, False);      
  	}  
    
  	Public Static HttpResponse sendHTTPRequest(String Method, String Body, String URL, Boolean setAuth){
    	Http h = new Http();
	    HttpRequest req = new HttpRequest();
   	 	if(setAuth){
     		req.setHeader('Authorization', 'Bearer ' + authToken.accessToken);   
    	}
    	req.setHeader('Content-Type', 'application/json');
    	req.setEndpoint(URL);
    	req.setBody(Body);
	    req.setMethod(Method);

	    HttpResponse res = h.send(req);
        return res;
	}
}

 

All Answers

KevinPKevinP
Casey,

This can def. be done. You're talking about making HTTP Callouts to the exact target REST api from within Apex. I've done this a couple of times, and the example code I've posted below is for sending a push notification, but you can see how authentication is done, and how making api requests is done.
 
public class MCPushMessageUtils {
    private Static accessTokenResult authToken;
    private Static String MessageId    = 'someMessageId';
    private Static String ClientId     = 'clientId';
    private Static String ClientSecret = 'ClientSecret';
    
	// Helper Methods and classes  
	public class accessTokenResult {
		public String accessToken;
		public Integer expiresIn;
  	}
    
    private Static String generateMessageSendBody(String CommentBody){
 		return '{' +
      	'"MessageText": "'+CommentBody+'",' +
      	'"Override": true,'+
      	'"sound": "default",'+
      	'"SendTime": "2012-10-31 09:00",'+
      	'"content-available": 1'+
    	'}';
  	}   
 
	public Static Void getAuthToken() {
    	sfmc__c creds = sfmc__c.getInstance('Pusher');
    	String authBody = '{"clientId":"'+ClientId+'","clientSecret":"'+ClientSecret+'"}';
    	system.debug(authBody);
    	HttpResponse res = sendHTTPRequest('POST',
        	                               authBody,
            	                           'https://auth.exacttargetapis.com/v1/requestToken');
	    authToken = (accessTokenResult) System.JSON.deserialize(res.getBody(), accessTokenResult.class);
 	}
  
  	@future(callout=true)
  	Public Static Void triggerSend(String CommentBody){
    	getAuthToken();
    	system.debug(authToken.accessToken);
    	HttpResponse res = sendHTTPRequest('POST', 
        	                               generateMessageSendBody(CommentBody), 
            	                           'https://www.exacttargetapis.com/push/v1/messageApp/'+MessageId+'/send',
                	                       TRUE);
	    // Helpful debug tip for InvocableMethods
	    // Divide by zero to intentionally throw an error and cause the logs to be written
	    // Integer x = 1 / 0;
  	}
 
  	Public Static HttpResponse sendHTTPRequest(String Method, String Body, String URL){
    	return sendHTTPRequest(Method, Body, URL, False);      
  	}  
    
  	Public Static HttpResponse sendHTTPRequest(String Method, String Body, String URL, Boolean setAuth){
    	Http h = new Http();
	    HttpRequest req = new HttpRequest();
   	 	if(setAuth){
     		req.setHeader('Authorization', 'Bearer ' + authToken.accessToken);   
    	}
    	req.setHeader('Content-Type', 'application/json');
    	req.setEndpoint(URL);
    	req.setBody(Body);
	    req.setMethod(Method);

	    HttpResponse res = h.send(req);
        return res;
	}
}

 
This was selected as the best answer
doravmondoravmon
What is the messageId you parse in? Thanks
Ameer SalahAmeer Salah
How I can Create the app in dashboard