I am using this function to check in SSJS if my email exists using the customer key.


  <script runat="server">
    Platform.Load("Core", "1.1.1");

    try {

      var emailKey = 'df_test_email';
      var results = Email.Retrieve({Property:"CustomerKey",SimpleOperator:"equals",Value:emailKey});
      if (results.length < 1) {
          Write("###Email Does Not Exist<br>");

Problem is the results always return zero, even though I know there is an email with the customer key set to df_test_email.

enter image description here

  • 1
    I need to test, but this may not work for Content Builder emails. I'll test shortly and let you know – EazyE Jun 12 '19 at 15:35

Why don't you use the REST API for this?

<script runat = "server" >
Platform.Load("Core", "1");
var url = 'https://auth.exacttargetapis.com/v1/requestToken';
var contentType = 'application/json';
var request = {
  "clientId": "YOUR CLIENT ID",
  "clientSecret": "YOUR SECRET"
var payload = Platform.Function.Stringify(request);
var result = HTTP.Post(url, contentType, payload);
var aRes = Platform.Function.ParseJSON(result.Response.toString());
var auth = aRes.accessToken;
var url = 'https://www.exacttargetapis.com/asset/v1/content/assets/query';
var contentType = 'application/json';

var request = {
  "query": {
    "leftOperand": {
      "property": "assetType.id",
      "simpleOperator": "equals",
      "valueType": "int",
      "value": 208 //HTMLEmail type
    "logicalOperator": "AND",
    "rightOperand": {
      "property": "name",
      "simpleOperator": "equals",
      "value": "df_test_email"
var payload = Platform.Function.Stringify(request);
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + auth];
var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);
var res = result.Response.toString();
| improve this answer | |
  • I am building this piece of code in SSJS, if there is already a built in SSJS function I'd prefer to use this than resort to using the API within SSJS. Trying to figure out if theres something wrong with the syntax or I'm not doing something quite right. – Deployment Failure Jun 12 '19 at 16:14
  • 3
    To my knowledge there is not a built in function like that which will retrieve a content builder asset. You have to use the REST API. Updated my answer to include the full snippet. – Jason Hanshaw Jun 12 '19 at 16:22
  • Just realised I've been trying to recreate the wheel when you've already provided the answer to getting the ID via API here. The same issue still occurs here "System.Collections.Generic.Dictionary`2[System.String,System.Object]". – Deployment Failure Jun 17 '19 at 16:33
  • 1
    FYI there is a much smaller resource you can use if you append the ID after /assets/ instead of query. 132KB vs 4.4MB – Deployment Failure Jun 17 '19 at 17:02
  • Despite using a filter condition it looks like its just returning all the assets, hence the large payload. (4.3MB) – Deployment Failure Jun 17 '19 at 17:03

This may be that you want:

function getemailname(pEID)
          var myEmail = Email.Init("myEmail");
          myEmail =  Email.Retrieve({Property:"ID",SimpleOperator:"equals",Value:pEID});

          for (var i = 0; i < myEmail.length; i++)
              return (myEmail[i].Name);


The reference is from https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/ssjs_retrieveExternalKey.htm?search_text=Email.Retrieve

| improve this answer | |
  • This does not retrieve content builder assets. – Jason Hanshaw Jun 17 '19 at 16:46

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.