0

I am trying add data that come from response of SOAP API to get data from list in data extension If response contains 2500 records then I am using for loop to add this data one by one in data extension. Soap API can provide only 2500 records at a time so if list contains more data then in response it will give overallstatus as moredataavailable, then will have to call soap api again by putting request id received from previous response in request body of api call to get next list of data. Our list contains around 3,14,000 records so I have to call api for 127 times approx to get all list records and put them in DE example of code snippet:

    <script runat="server"> 

    Platform.Load("core", "1"); 
    var log = DataExtension.Init(logde);
    var j = 0;
    function callRecursive(){
     recursive();
    }

    function recursive()
    {
      j++;
    for(var i =0; I < 2500; i++)
    {
     log.Rows.Add({"Message":"i ="+i});
    }

    if(j < 127)
    {
     recursive();
    }
    }
    </script>

This is just a sample code but my code scenario is same. while calling these apis automation gets stopped with error in between and I could not get all data from list and copy in DE. As automation does not mention any error it just gives error as 'Error has occured' so unable to find exact cause of the error. My actual code is as below:

   <script runat="server"> 

   Platform.Load("core", "1");     
    var endpoint = 'https://XXXX.soap.marketingcloudapis.com/Service.asmx';
    var result;
    var k = 0;
    var e =0;

    //access token call
    var logDE = "logde";
    var log = DataExtension.Init(logDE);
  //  var logDEcopy  = "logsdecopy";
//    var logcopy = DataExtension.Init(logDEcopy);
    var allSubDE = "testallsubde";
    var DE = DataExtension.Init(allSubDE);
    var accessToken = '';
    var continueRequestID = "";
    getAccessToken();
    function getAccessToken()
    {
    var url = 'https://YYYY.auth.marketingcloudapis.com/v2/token';
    var contentType = 'application/json';
    var  payloadForAccessToken = '{"grant_type":"client_credentials",';
    payloadForAccessToken += '"client_id":"something",';
    payloadForAccessToken += '"client_secret":"something",';
    payloadForAccessToken += '"account_id":"1234"}';
    var accessTokenResult = HTTP.Post(url, contentType, payloadForAccessToken);
    var statusCode = result["StatusCode"];
    var response = accessTokenResult["Response"][0];
    accessToken = Platform.Function.ParseJSON(response).access_token;
    callSOAPAPI();
    }

  function getPayload(){
   var payload = '';
    if(continueRequestID == ""){
    // payload='';
    // log.Rows.Add({"Message": "payload1"});
     payload += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
     payload += '<soapenv:Header>';
     payload += '<fueloauth>';
     payload += accessToken;
     payload += '</fueloauth>';
     payload += '</soapenv:Header>';
     payload += '<soapenv:Body>';
     payload += '<RetrieveRequestMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">';
     payload += '<RetrieveRequest>';
     payload += '<ObjectType>ListSubscriber</ObjectType>';
     payload += '<Properties>CreatedDate</Properties>';
     payload += '<Properties>ModifiedDate</Properties>';
     payload += '<Properties>Client.ID</Properties>';
     payload += '<Properties>SubscriberKey</Properties>';
     payload += '<Properties>Status</Properties>';
     payload += '<Properties>UnsubscribedDate</Properties>';
     payload += '<Properties>ID</Properties>';
     payload += '<Properties>Client.PartnerClientKey</Properties>';
     payload += '<Filter xsi:type="SimpleFilterPart">';
     payload += '<Property>ListID</Property>';
     payload += '<SimpleOperator>equals</SimpleOperator>';
     payload += '<Value>314</Value>';
     payload += '</Filter>';
     payload += '</RetrieveRequest>';
     payload += '</RetrieveRequestMsg>';
     payload += '</soapenv:Body>';
     payload += '</soapenv:Envelope>';
  }else{
  //  payload='';
 // log.Rows.Add({"Message": "payload2"});
     payload += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
     payload += '<soapenv:Header>';
     payload += '<fueloauth>';
     payload += accessToken;
     payload += '</fueloauth>';
     payload += '</soapenv:Header>';
     payload += '<soapenv:Body>';
     payload += '<RetrieveRequestMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">';
     payload += '<RetrieveRequest>';
     payload += '<ContinueRequest>';
     payload +=  continueRequestID;
     payload += '</ContinueRequest>';
     payload += '<ObjectType>ListSubscriber</ObjectType>';
     payload += '<Properties>CreatedDate</Properties>';
     payload += '<Properties>ModifiedDate</Properties>';
     payload += '<Properties>Client.ID</Properties>';
     payload += '<Properties>SubscriberKey</Properties>';
     payload += '<Properties>Status</Properties>';
     payload += '<Properties>UnsubscribedDate</Properties>';
     payload += '<Properties>ID</Properties>';
     payload += '<Properties>Client.PartnerClientKey</Properties>';
     payload += '<Filter xsi:type="SimpleFilterPart">';
     payload += '<Property>ListID</Property>';
     payload += '<SimpleOperator>equals</SimpleOperator>';
     payload += '<Value>314</Value>';
     payload += '</Filter>';
     payload += '</RetrieveRequest>';
     payload += '</RetrieveRequestMsg>';
     payload += '</soapenv:Body>';
     payload += '</soapenv:Envelope>';
  }
//  log.Rows.Add({"Message": "payload: "+payload});
  return payload;
  }
    //log.Rows.Add({"Message": "result: " + accessToken});

    // Create SOAP Call

  function callSOAPAPI()
    {
    try {
        result = HTTP.Post(endpoint, "text/xml", getPayload(), ["SOAPAction"], ["Retrieve"]);
        //result = Stringify(result).replace(/[\n\r]/g, '');
          //log.Rows.Add({"Message": result});
    } catch (e) {
        Write(Stringify(e));
        e = Stringify(e).replace(/[\n\r]/g, '');
        log.Rows.Add({"Message": "error: " + e});
    }

  if (result.StatusCode != 200) {
        //Bad response
       /* Write(Stringify(result));
        Write('unsuccess');*/
        log.Rows.Add({"Message": "result: " + Stringify(result)});
        e++;
        getAccessToken();
    } else {
        //Good response
        response = Stringify(result.Response);
      //  Write(response);
        Variable.SetValue("@Response","Response");
         k++;;
       // log.Rows.Add({"Message": "result: " + response});
       fillAllSubDE(response);
    }
    }
    function fillAllSubDE(response){
        var overallstatus1 = response.split("<OverallStatus>")[1];
        var oversallstatus = overallstatus1.split("</OverallStatus>")[0];
       // log.Rows.Add({"Message": "OverallStatus: " + oversallstatus});
        var res1 = response.split("</RequestID>")[1];
        var res2 = res1.split("</RetrieveResponseMsg>")[0];
        var resultarray = res2.split("</Results>");
         //log.Rows.Add({"Message": resultarray.length});
        for(i=0;i<2500;i++)
        {
         var subscriberKey1 = resultarray[i].split("<SubscriberKey>")[1];
        // var subscriberKey = subscriberKey1.split("</SubscriberKey>")[0];
         var modifiedDate1 = resultarray[i].split("<ModifiedDate>")[1];
      //   var modifiedDate = modifiedDate1.split("</ModifiedDate>")[0];
        // var status1 = resultarray[i].split("<Status>")[1];
         var status = status1.split("</Status>")[0];
      //   var createdDate1 = resultarray[i].split("<CreatedDate>")[1];     
         var createdDate = createdDate1.split("</CreatedDate>")[0];*/

        DE.Rows.Add({"SubscriberKey":subscriberKey1,"ModifiedDate":modifiedDate1,"Status":status1,"CreatedDate":createdDate1});

        }
        //  log.Rows.Add({"Message": "k: "+k});
       if(oversallstatus == "MoreDataAvailable")
       {
          var continueRequestID1 = response.split("<RequestID>")[1];
          var continueRequestID = continueRequestID1.split("</RequestID>")[0];
          log.Rows.Add({"Message": "k: "+k+" continueRequestID: " + continueRequestID});
          callSOAPAPI();
       }else{
           log.Rows.Add({"Message": "No continueRequestID found"});
       }
    }
   // Write(Stringify(result));

</script>

You can see error screenshot as below: enter image description here

  • Hi! Firstly I'd suggest not putting your client id and secret into your code examples. Also I'd suggest checking out WSProxy, this will allow you to interact with the SOAP API without having to concatenate a SOAP package and post to the API directly, simplifying your code and making it easier to test. – Josh de Blank Jul 5 at 0:42
  • Thanks Josh.. I tried to use WSProxy but unable to add data using it in data extension.. – pimobility persistent Jul 9 at 14:00
  • Looks like your script activity in your automation is timing out. It will error if it runs longer than 30 mins. Either work on improving the efficiency of your code and/or consider other options; possibly break up your data into segments that won't cause timeouts. – Josh de Blank Jul 9 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.