0

I need to create a job that monitors the delete jobs that are running in our BU every day. I managed to use this API (https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/ContactsDeleteRequestsDetails.htm) and get a response, but I can't access the data in the inner part of the response.

Here is the full code:

<script runat=server>
    Platform.Load("Core", "1");
    
    var deLog = DataExtension.Init('97975E14-6583-49F7-B628-7790BB8FF1EC'); // Monitorizare Delete Log
    var deError = DataExtension.Init('A3AFED88-A758-4A1E-B3D8-A0BDE266EAA1'); // Monitorizare Delete Error
    var deMonitorizare = DataExtension.Init('D16950BB-DD04-44D2-8493-97EE4B1622EA'); // Monitorizare Delete
    
    deLog.Rows.Add({Name: 'START'});
    
    var resp;
    
    var getAccessTokenEndpoint = 'https://auth.exacttargetapis.com/v1/requestToken';
    var endpoint = 'https://mcwxf7761p78fdnb687k3s7lj1d1.rest.marketingcloudapis.com/contacts/v1/contacts/analytics/deleterequests?startDateUtc=2021-09-25T00:00:00&endDateUtc=2021-09-26T00:00:00';
    var clientId = '*********';
    var clientSecret = '*********';
    
    var loginBody = {};
    loginBody.clientId = clientId;
    loginBody.clientSecret = clientSecret;
    
    var loginResult = HTTP.Post(getAccessTokenEndpoint, 'application/json', Platform.Function.Stringify(loginBody), null, null);
    
    if(loginResult.StatusCode != 200) {
        deError.Rows.Add({Name: 'Login was unsuccessful!'});
        deError.Rows.Add({Name: 'Login Result: ' + Platform.Function.Stringify(loginResult)});
    }
    
    var loginObj = Platform.Function.ParseJSON(loginResult.Response[0]);
    var headerNames = 'Authorization';
    var headerValues = 'Bearer ' + loginObj.accessToken;
    
    function HttpRequest() {
        var req = new Script.Util.HttpRequest(endpoint);
        req.emptyContentHandling = 1;
        req.retries = 2;
        req.continueOnError = true;
        req.contentType = "application/json"
        req.setHeader(headerNames, headerValues);
        req.method = "GET";
        
        resp = req.send();
        
        deLog.Rows.Add({Name: 'Server Response Content: ' + resp.content + '; ' + 'Server Response ContentType: ' + resp.contentType + '; ' + 'Server Response Encoding: ' + resp.encoding + '; ' + 'Server Response Header: ' + resp.headers + '; ' + 'Server Response ReturnStatus: ' + resp.returnStatus + '; ' + 'Server Response StatusCode: ' + resp.statusCode});
    }
    
    try {
        HttpRequest();
        
        deLog.Rows.Add({Name: 'Response: ' + resp.content.operations[0].status});
    } catch(e) {
        deError.Rows.Add({Name: 'Error catched: ' + Platform.Function.Stringify(e)});
    }
    
    deLog.Rows.Add({Name: 'END'});
    
</script>

Again, I'm getting the full response for the API call (saved in the resp variable), but I can't access the inner part like the status of the operations. I'm getting the following error every time:

Error catched: {"message":"Use of Common Language Runtime (CLR) is not allowed","description":"System.Security.SecurityException: Use of Common Language Runtime (CLR) is not allowed - from Jint\r\n\r\n"}

Can someone help me out please?

1 Answer 1

4

You need to change the content returned from CLR to a String and then JSON to dissect the info. This can be done using String()and ParseJSON()function.

var resultJSON = Platform.Function.ParseJSON(String(resp.content));

Read this article https://gortonington.com/restapimethodinssjs/

You must log in to answer this question.

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