0

I need to duplicate an HTML Email with SSJS but in order to do that, firstly, I should get the content of the Email I want. I have written the code below but it doesn't work. The strange thing is the same code works for Data Extensions when I change ObjectType.

<script runat="server"> 

Platform.Load("core", "1");

var rr = Platform.Function.CreateObject("RetrieveRequest");
Platform.Function.SetObjectProperty(rr, "ObjectType", "Email");
Platform.Function.AddObjectArrayItem(rr, "Properties", "Name");
Platform.Function.AddObjectArrayItem(rr, "Properties", "ID");
Platform.Function.AddObjectArrayItem(rr, "Properties", "CustomerKey");

var sfp = Platform.Function.CreateObject("SimpleFilterPart");
Platform.Function.SetObjectProperty(sfp, "Property", "CustomerKey");
Platform.Function.SetObjectProperty(sfp, "SimpleOperator", "Equals");
Platform.Function.AddObjectArrayItem(sfp, "Value", "asgdsgsgsgsdgsg");

Platform.Function.SetObjectProperty(rr, "Filter", sfp);

var StatusAndRequestID = [0,0];
var Result = Platform.Function.InvokeRetrieve(rr,StatusAndRequestID);

if(Result != null) {

Write("It works");}

else { Write("It doesn't work");}
</script>
1

The example code for the SSJS Function SetObjectProperty inside the reference docs is actually related to your question. It looks like the properties require a prefix of Email. prior to each.

See snippet below:

// Set the columns
Platform.Function.AddObjectArrayItem(RetrieveRequest, "Properties", "Email.Name");
Platform.Function.AddObjectArrayItem(RetrieveRequest, "Properties", "Email.ID");
Platform.Function.AddObjectArrayItem(RetrieveRequest, "Properties", "Email.Subject");
Platform.Function.AddObjectArrayItem(RetrieveRequest, "Properties", "Email.Status");
Platform.Function.AddObjectArrayItem(RetrieveRequest, "Properties", "Email.CharacterSet");
Platform.Function.AddObjectArrayItem(RetrieveRequest, "Properties", "Email.ContentCheckStatus");

I would also look into using WSProxy for this and future SOAP API needs inside SSJS. It shortens your 'overhead' to the processing of your calls, making them more efficient and quicker.

The legacy methods for accessing SOAP object data, including what is used in the SSJS Core Library, are primarily wrappers around AMPScript functions. Using legacy SSJS methods adds overhead to the processing of the calls as the code translates the data between the different language runtimes. The WSProxy object is native to the platform and simpler to use than the SSJS methods. The object reduces overhead and increases speed of your API calls. The object’s usage syntax is much more JavaScript-like when compared to the existing platform functions.

| improve this answer | |
  • Can it get Email.Campaign too? Email.CampaignName? @Gortonington – sfdcFanBoy Apr 16 '19 at 4:44
  • I do not see those as options inside the Documentation on the email object nor a describe of it. So I do not believe those are options to retrieve. If I am missing something, then yes. As long as the properties are retrievable, then they can be pulled using the above method developer.salesforce.com/docs/… – Gortonington Apr 16 '19 at 14:48
0

Here's the documentation for WSProxy Retrieve (the better way to make SOAP API calls with SSJS): https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_WSProxy_advanced_retrieve.htm?search_text=for%20loop%20javascript

This is how you would write your code using WSProxy (and losing the Platform.Load("core", "1"); call). I tested this code, and you shouldn't need to prepend your property names with Email:

<script runat="server">
function Write(str) {
   Platform.Response.Write(str);
}

/*------------------------------------------*/

function Stringify(obj) {
   return Platform.Function.Stringify(obj);
}

/*------------------------------------------*/

var api = new Script.Util.WSProxy(); // Create WSProxy object

api.setClientId({ "ID": Platform.Function.AuthenticatedMemberID()}); // Set ClientID if you need to

var retrieveObject = 'Email'; // Set your object

var cols = ['Name','HTMLBody','CustomerKey','ID','ModifiedDate','CategoryID', 'Client.ID']; // Choose the properties you'd like the call to return

var filter = { 
    Property: "ID", 
    SimpleOperator: "equals", 
    Value: '123714'
};

var opts   = {}; // Set options, like number of results returned -- see docs
var props  = {QueryAllAccounts: false}; // Set properties like QueryAllAccounts -- see docs

var data = api.retrieve(retrieveObject, cols, filter, opts, props);

if (data['Status'] != 'OK'){
   Write('There was an error: '+data['Status']);
} else if (data['Results'].length == 0) {
   Write('No results were returned');
} else {
   Write('Here are my results!<br />'+Stringify(data).replace(/\n/g,"<br />\n")
}
</script>
| improve this answer | |

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.