I have a use case where I need to make a REST API call to the third-party application to get coupons. I was able to make API call and capture coupons into DE, but due to API limitations, I'm only able to receive 1000 coupons at once. I have to execute the same script again for an additional 1000 coupons, and so on...If I had to generate 50k coupons I will have to execute the same script for 50 times.
My question is there a way to execute the SSJS script the desired number of times?
Below is the SSJS script that captures 1000 coupons into a DE:
<script runat=server>
Platform.Load("core", "1.1.1");
var accessToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
var url = 'https://company.com/coupons'
var payload = '{"usageLimit":1,"discountLimit":10,"startDate":"2023-01-24T14:15:22Z","expiryDate":"2023-08-24T14:15:22Z","numberOfCoupons":20000,"uniquePrefix":"AC","attributes":{},"validCharacters":["A","B","G","Y","Z","H","1","6","9","3"],"couponPattern":"AC#####"}';
var auth = accessToken;
var req = new Script.Util.HttpRequest(url);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json"
req.setHeader("Authorization", auth);
req.method = "POST"; /*** You can change the method here ***/
req.postData = payload;
var resp = req.send();
var resultString = Stringify(resp.content)
var resultJSON = Platform.Function.ParseJSON(String(resp.content))
//Write("Response:" +resultJSON);
//Write("resultString:" +resultString);
if (resultJSON.length > 0) {
for (var i = 0; i < resultJSON.data.length; i++ ) {
var id = resultJSON.data[i].id;
var created = resultJSON.data[i].created;
var campaignId = resultJSON.data[i].campaignId;
var CouponCode = resultJSON.data[i].value;
var discountLimit = resultJSON.data[i].discountLimit;
var startDate = resultJSON.data[i].startDate;
var expiryDate = resultJSON.data[i].expiryDate;
var reservation = resultJSON.data[i].reservation;
var batchId = resultJSON.data[i].batchId;
Write("id:" +id+ "<br>");
Write("created:" +created+ "<br>");
Write("campaignId:" +campaignId+ "<br>");
Write("CouponCode:" +CouponCode+ "<br>");
Write("discountLimit:" +discountLimit+ "<br>");
Write("startDate:" +startDate+ "<br>");
Write("expiryDate:" +expiryDate+ "<br>");
Write("reservation:" +reservation+ "<br>");
Write("batchId:" +batchId+ "<br>");
var rows = Platform.Function.UpsertData("CouponJson",["ID","CouponCode"],[id,CouponCode],["Created","CampaignID","Discountlimit","StartDate","expiryDate","reservation","batchid"],[created,campaignId,discountLimit,startDate,expiryDate,reservation,batchId]);
}
} else {
Write("no products found")
}
</script>
Thank you in advance for your inputs.