0

I am trying to create an automation activity (SSJS) that would opt out or opt in a subscriber at a keyword level. I am using the code below and was able to run it in Automation Studio without errors. However, the Opt In Status does not seem to update. Any suggestions?

<script runat="server">
   Platform.Load("Core", "1.1.1");

try {
         /* get OAuth 2.0 access token */
          var payload = '{"grant_type":"client_credentials",';
                payload += '"client_id":"xxxxxx",';
                payload += '"client_secret":"xxxxxx",';
                payload += '"scope":"list_and_subscribers_read list_and_subscribers_write",';
                payload += '"account_id":"xxxxxx"}';
          var OAuth2URL = "https://xxxxxx.auth.marketingcloudapis.com/v2/token";
          var contentType = 'application/json';
          var accessTokenResult = HTTP.Post(OAuth2URL, contentType, payload);
          var tokenObj = Platform.Function.ParseJSON(accessTokenResult["Response"][0]);
          var accessToken = tokenObj.access_token;
          var auth = ["Bearer " + accessToken];

          
                          /* create the payload */
                          var payload = {
                                         "contactKey":"test1",
                                         "attributeSets": [{
                                               "name":"MobileConnect Subscriptions",
                                               "items": [{
                                                     "values": [
                                                           { "name":"Keyword", "value":"AB173FC3-3326-4ABB-A8B9-995D22E469C6" },
                                                           { "name":"Mobile Number", "value":"61400000000" },
                                                           { "name":"Opt In Status", "value":0 },
                                                           { "name":"Opt Out Status", "value":1 }
                                                     ]
                                               }]
                                         }]
                                 };

                          /* make the API call and get the response */ 
                          var contactsUrl = "https://mcnm75lqhcrhj-ky-7jf9h275334.rest.marketingcloudapis.com/contacts/v1/contacts";
                          var req = new Script.Util.HttpRequest(contactsUrl);
                          req.emptyContentHandling = 0;
                          req.retries = 2;
                          req.continueOnError = true;
                          req.contentType = "application/json";
                          req.setHeader("Authorization", auth);
                          req.method = "patch";
                          req.postData = Stringify(payload);
                          var response = req.send();
                          Write(Stringify(response));
        } catch(e) { Write(Stringify(e)); }       

</script>

Also, I tried running the same code via cloudpages but I am getting this 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"}

Thanks in advance

6
  • I can't think of a use case where you'd be using an Automation to update a single Mobile Contact. Have you considered using Data Extract Activity -> Mobile Import Activity. It's more scalable and less complex.
    – Macca
    Commented Jun 18, 2021 at 1:23
  • Hi Macca. The code above is just a test so I am only updating a test contact for now.
    – smn
    Commented Jun 18, 2021 at 2:23
  • Hi @smn, You'd probably want to consider running a Query Activity to identify those you need to unsubscribe - include a Status field with value "Inactive", followed by a Data Extract Activity to dump your results to file in the Safehouse. Then, use a File Transfer Activity to move your file to FTP and finally a Mobile Import Activity to import your file into MobileConnect.
    – Macca
    Commented Jun 18, 2021 at 3:02
  • Hi @Macca, thanks. However I have tried doing the mobile import but that does not work.
    – smn
    Commented Jun 19, 2021 at 2:03
  • "does not work", how?
    – Macca
    Commented Jun 21, 2021 at 0:39

1 Answer 1

0

There could be several reasons:

  1. Check if the scope of the Installed Package that you use corresponds to the scope of the code - it should be SMS: Read, Write and List and Subscribers: Read, Write;
  2. Make sure you are indeed trying to unsubscribe opted in contact with the correct mobile phone number format and with the correct keyword;
  3. I am not sure about this but try updating the code for the variable tokenObj = Platform.Function.ParseJSON(accessTokenResult.Response[0]);

Hope it helps.

1
  • thanks. I did verify that the package have the correct permissions. Also, I don't think the code for getting the tokenobj is the issue as I have ran a test on just getting the token and it works.
    – smn
    Commented Jun 23, 2021 at 3:53

You must log in to answer this question.

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