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 protected]","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?

6
  • Why not just put the upsert related code inside the for loop? Commented Apr 25, 2016 at 13:24
  • Sorry I think the alignment is a bit out, i've edited it again to be clearer :) Commented Apr 25, 2016 at 14:08
  • Can you post a sample of your JSON object? Commented Apr 25, 2016 at 14:27
  • [ { "subObj": [ { "SubscriberKey": "16347460029", "EmailAddress": "[email protected]", }, { "SubscriberKey": "345383714258", "EmailAddress": "[email protected]", }, { "SubscriberKey": "44568799855", "EmailAddress": "[email protected]", }, { "SubscriberKey": "43658751542", "EmailAddress": "[email protected]", }, { "SubscriberKey": "77836423975", "EmailAddress": "[email protected]", } ] } ] Commented Apr 25, 2016 at 14:35
  • So have you confirmed that sk and email have values in every iteration of your loop? Commented Apr 25, 2016 at 14:43

1 Answer 1

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":"[email protected]"},
  {"SubscriberKey":"345383714258","EmailAddress":"[email protected]"},
  {"SubscriberKey":"44568799855","EmailAddress":"[email protected]"},
  {"SubscriberKey":"43658751542","EmailAddress":"[email protected]"},
  {"SubscriberKey":"77836423975","EmailAddress":"[email protected]"}
];

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":"[email protected]","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"[email protected]","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"44568799855","EmailAddress":"[email protected]","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"44568799855","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"43658751542","EmailAddress":"[email protected]","UpsertStatus":"OK"}]
[,
"SubscriberKey":"16347460029","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"345383714258","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"44568799855","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"43658751542","EmailAddress":"[email protected]","UpsertStatus":"OK"}, ,
"SubscriberKey":"77836423975","EmailAddress":"[email protected]","UpsertStatus":"OK"}]
2
  • Thank you Adam! That was exactly my problem, I must be rusty today from my 2 weeks PTO :) Commented Apr 26, 2016 at 7:28
  • The code doesn't seem to work in a child BU. Do I need to do anything different? Commented Sep 16, 2018 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .