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?
upsert
related code inside thefor
loop?sk
andemail
have values in every iteration of your loop?