0

I have a JSON object containg Subscriber Key, Email Address and some other attributes.

I would like to upsert the All Subscribers with this object, but it seems that Marketing Cloud only upserts the first one, and all subsequent array items aren't processed (Email address and Subscriber Key are null)

function massUpsert(subObj, allSubscribers) {
    var statusJSON = [];
    for (var i = 0; i < subObj.length; i++) {
        var email = subObj[i].EmailAddress;
        var Sk = subObj[i].SubscriberKey;
        var BrandCode = subObj[i].BrandCode;
        var SendLanguage = subObj[i].SendLanguage;
        var newSubscriber = {
            "EmailAddress": email,
            "SubscriberKey": Sk,
            "EmailTypePreference": "HTML",
            "Attributes": {
                "BrandCode": BrandCode,
                "SendLanguage": SendLanguage
            },
            "Lists": {
                "Status": "Active",
                "ID": allSubscribers,
                "Action": "Upsert"
            }
        };
        var subObj = Subscriber.Init(Sk);
        upsertStatus = subObj.Upsert(newSubscriber)
        var status = {};
        status["SK"] = Sk;
        status["Email"] = email;
        status["UpsertStatus"] = upsertStatus;
        statusJSON.push(status);
    }
}

Reponse:

{"UpsertingIntoAllSubs":[{"SK":"16347460029","Email":"email@gmail.com","UpsertStatus":"OK"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}, 
    {"SK":null,"Email":null,"UpsertStatus":"Error"}]}

When I processed each array item individually, the response is always "OK".

Does some kind of delay need to be implemented between each upsert? Any other suggestions?

  • Why not just put the upsert related code inside the for loop? – Adam Spriggs Apr 25 '16 at 13:24
  • Sorry I think the alignment is a bit out, i've edited it again to be clearer :) – C Carswell Apr 25 '16 at 14:08
  • Can you post a sample of your JSON object? – Adam Spriggs Apr 25 '16 at 14:27
  • [ { "subObj": [ { "SubscriberKey": "16347460029", "EmailAddress": "email1@gmail.com", }, { "SubscriberKey": "345383714258", "EmailAddress": "email2@gmail.com", }, { "SubscriberKey": "44568799855", "EmailAddress": "email3@gmail.com", }, { "SubscriberKey": "43658751542", "EmailAddress": "email4@gmail.com", }, { "SubscriberKey": "77836423975", "EmailAddress": "email5@gmail.com", } ] } ] – C Carswell Apr 25 '16 at 14:35
  • So have you confirmed that sk and email have values in every iteration of your loop? – Adam Spriggs Apr 25 '16 at 14:43
1

You're redefining subObj inside your function. I renamed subObj2 in the line that inits the Subscriber Object and everything works:

<script language=javascript runat="server">
Platform.Load("core","1.1.1");
var allSubscribers = 1103;
var subObj1 = [
  {"SubscriberKey":"16347460029","EmailAddress":"email1@gmail.com"},
  {"SubscriberKey":"345383714258","EmailAddress":"email2@gmail.com"},
  {"SubscriberKey":"44568799855","EmailAddress":"email3@gmail.com"},
  {"SubscriberKey":"43658751542","EmailAddress":"email4@gmail.com"},
  {"SubscriberKey":"77836423975","EmailAddress":"email5@gmail.com"}
];

function massUpsert(subObj, allSubscribers) {

  var statusJSON = [];

  for (var i = 0; i < subObj.length; i++) {

      var email = subObj[i].EmailAddress;
      var Sk = subObj[i].SubscriberKey;
      var BrandCode = subObj[i].BrandCode;
      var SendLanguage = subObj[i].SendLanguage;

      var newSubscriber = {
          "EmailAddress": email,
          "SubscriberKey": Sk,
          "EmailTypePreference": "HTML",
          "Attributes": {
              "BrandCode": BrandCode,
              "SendLanguage": SendLanguage
          },
          "Lists": {
              "Status": "Active",
              "ID": allSubscribers,
              "Action": "Upsert"
          }
      };
      var subObj2 = Subscriber.Init(Sk);
      var upsertStatus = subObj2.Upsert(newSubscriber)
      var status = {};
      status["SubscriberKey"] = Sk;
      status["EmailAddress"] = email;
      status["UpsertStatus"] = upsertStatus;
      statusJSON.push(status);

      Write("<br>" + Stringify(statusJSON).replace(/\{/g,",<br>"));

  }
}
massUpsert(subObj1,allSubscribers);
</script>

Output

[,
"SubscriberKey":"16347460029","EmailAddress":"email1@gmail.com","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"email1@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"email2@gmail.com","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"email1@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"email2@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"44568799855","EmailAddress":"email3@gmail.com","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"email1@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"email2@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"44568799855","EmailAddress":"email3@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"43658751542","EmailAddress":"email4@gmail.com","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"email1@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"email2@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"44568799855","EmailAddress":"email3@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"43658751542","EmailAddress":"email4@gmail.com","UpsertStatus":"OK"}, ,
"SubscriberKey":"77836423975","EmailAddress":"email5@gmail.com","UpsertStatus":"OK"}]
  • Thank you Adam! That was exactly my problem, I must be rusty today from my 2 weeks PTO :) – C Carswell Apr 26 '16 at 7:28
  • The code doesn't seem to work in a child BU. Do I need to do anything different? – sfdcFanBoy Sep 16 '18 at 10:51

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.