1

I'm trying to get my head around SSJS to create a script that can store the provided values in the SFMC database using Script.Util.HttpRequest function. I am able to see that the values provided were all correct but I'm still getting the following error:

Error: {"message":"Unable to retrieve security descriptor for this frame.","description":"System.InvalidOperationException: Unable to retrieve security descriptor for this frame. - from mscorlib\r\n\r\n"} Error name: "TypeError" Error message: "Unable to retrieve security descriptor for this frame."

I tried to store values using HTTP.Post function but it also throws an error. Please find my code below.

  try {
    var authEndpoint = "xxxx";
    var client_id = "xxxx";
    var client_secret = "xxxx";
    var account_id = "xxxx";
    var grant_type = "client_credentials";

    //auth
    var url =
      "https://" + authEndpoint + ".auth.marketingcloudapis.com/v2/token";
    var contentType = "application/json";
    var payload = '{"grant_type":"' + grant_type + '",';
    payload += '"client_id":"' + client_id + '",';
    payload += '"client_secret":"' + client_secret + '",';
    payload += '"account_id":"' + account_id + '"}';

    // create token
    var accessTokenResult = HTTP.Post(url, contentType, payload);
    var statusCode = accessTokenResult["StatusCode"];
    var response = accessTokenResult["Response"][0];
    var resultCode = accessTokenResult.StatusCode;
    accessToken = Platform.Function.ParseJSON(response).access_token;
    Write("<br><br> Access Token = " + accessToken + "<br><br>");

    // Debug info below
    Write("Status Code: " + resultCode + "<br><br>");
    if (resultCode === 200) {
      // Another API call here or something else
      var restURL = Platform.Function.ParseJSON(response).rest_instance_url;
      Write("Rest URL = " + restURL + "<br><br>");

      var customer_name = "John Doe";
      var company_name = "ABC";
      var EmailAddress = "[email protected]";
      var Product_Name1 = "Adidas Golf Shirt";
      var Product_Quantity1 = 35;
      var Customization1 = "Embroidery";
      var Product_Name2 = "Under Armour Cap";
      var Product_Quantity2 = 42;
      var Customization2 = "Logo";

      var restPayload = '{"To": {';
      restPayload += '"Address": "' + EmailAddress + '",';
      restPayload += '"SubscriberKey": "' + EmailAddress + '",';
      restPayload += '"ContactAttributes": {';
      restPayload += '"SubscriberAttributes": {';
      restPayload += '"SubscriberKey": "' + EmailAddress + '",';
      restPayload += '"EmailAddress": "' + EmailAddress + '",';
      restPayload += '"Customer Name": "' + customer_name + '",';
      restPayload += '"Company Name": "' + company_name + '",';
      restPayload += '"Product_Name1": "' + Product_Name1 + '",';
      restPayload += '"Product_Quantity1": ' + Product_Quantity1 + ",";
      restPayload += '"Customization1": "' + Customization1 + '",';
      restPayload += '"Product_Name2": "' + Product_Name2 + '",';
      restPayload += '"Product_Quantity2": ' + Product_Quantity2 + ",";
      restPayload += '"Customization2": "' + Customization2 + '",';
      restPayload += "}}}}";

      var payloadStr = Stringify(restPayload); // Creating a API readable JSON payload

      var apiRoute, bearerToken;

      apiRoute =
        restURL + "messaging/v1/messageDefinitionSends/key:xxxx/send";

      bearerToken = "Bearer " + accessToken;

      var headerName = ["Authorization"];
      var headerValue = [bearerToken];

      Write("API Route = " + apiRoute + "<br><br>");
      Write("Payload = " + restPayload + "<br><br>");
      Write("Header Name = " + headerName + "<br><br>");
      Write("Header Value = " + headerValue + "<br><br>");

      var req = new Script.Util.HttpRequest(apiRoute);
      req.emptyContentHandling = 0;
      req.retries = 2;
      req.continueOnError = true;
      req.contentType = "application/json";
      req.method = "POST";
      req.setHeader(headerName, headerValue);
      req.postData = payloadStr;

      var resp = req.send();
      var resultStatusStr = String(resp.statusCode);
      var resultContentStr = String(resp.content);
      var resultStatusJSON = Platform.Function.ParseJSON(resultStatusStr);
      var resultContentJSON = Platform.Function.ParseJSON(resultContentStr);
      Write("<br>resultStatusJSON: " + Stringify(resultStatusJSON));
      Write("<br>resultContentJSON: " + Stringify(resultContentJSON));
    } else {
      // block of code to be executed if the condition1 is false
      Write("Result Code is not 200 <br><br>");
    }
  } catch (e) {
    Write("<br><br> Error: " + Stringify(e));
    Write("<br><br> Error name: " + Stringify(e.name));
    Write("<br><br> Error message: " + Stringify(e.message));
  }

1 Answer 1

1

I was able to troubleshoot the problem and find a solution. You can change the value of the following if you have the same problem.

from

   var headerName = ["Authorization"]; 
   var headerValue = [bearerToken]; 

to

  var headerName = "Authorization";
  var headerValue = bearerToken; 

You must log in to answer this question.

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