0

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.

1
  • 2
    you would just wrap that script in another for loop to keep running it however many times you need. Could even use a do/while loop to keep doing it until it returns empty or until x parameter is met if a for loop won't work for your need. May 19, 2023 at 20:39

1 Answer 1

0

You can create an automation with a SSJS activity and configure the automation schedule to run the number of times that you need.

https://www.youtube.com/watch?v=9lUfFLC0zeY

2
  • Thanks for your comment, but this doesn't solve my use-case. If I had to create 100k coupons I will have to add 100 script activities in an automation. I feel it's not good practice. I was able to figure out looping script for the desired number of times.
    – Harish
    May 25, 2023 at 6:27
  • You shouldn't create x script activities. You should figure out and design your SSJS activity to loop the number of times that you need to request coupons. May 29, 2023 at 15:32

You must log in to answer this question.

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