1

I am trying to delete multiple records using REST Api.

    var contentType = 'application/json; charset=utf-8';
    var headerNames = ["Authorization"];
    var headerValues = [("Bearer "+sessionId)];

    var allRecords = DataExtension.Init("MyDataExtension").Rows.Retrieve();
    for (var i=0; i < allRecords.length; i++){
          var ContactId = allRecords[i].ContactId;
          var Url = ('https://blablabla.my.salesforce.com/services/data/v42.0/sobjects/Contact/' + ContactId);

          var result = HTTP.Delete(Url, contentType, headerNames, headerValues);
        }

However, I am getting this error:

Jint.Native.JsException: Object expected: Delete.

What is wrong with this ssjs code? or is there any other way to do this?

EDIT

Below is my edited code. This returns error:

The remote server returned an error: (400) Bad Request.

var contentType = 'application/json; charset=utf-8';
var headerNames = ["Authorization"];
var headerValues = [("Bearer "+sessionId)];

var allRecords = DataExtension.Init("MyDataExtension").Rows.Retrieve();

var url = 'https://blablabla.my.salesforce.com/services/data/v42.0/composite';
var payload = { "compositeRequest": [] };

for(var i = 0; i < allRecords.length; i++) {
//var contact = Stringify(allRecords[i].ContactId);
payload.compositeRequest.push(
    { "method": "DELETE",
    "url": "/services/data/v42.0/sobjects/Contact/"+allRecords[i].ContactId   //allRecords[i].ContactId OR var contact?
    });
if(payload.compositeRequest.length == 25 || i == allRecords.length - 1) {
    var response = HTTP.Post(url, "application/json", Stringify(payload), headerNames, headerValues);  // JSON.Stringify OR Stringify?
    log.Rows.Add({Log: "response: " + response});
    payload.compositeRequest = [];
    }
}
1

HTTP.Delete is not a valid method name. You only have the options for GET and POST. Instead, use the POST method and use the composite request resource. As a bonus, you can delete up to 25 records at once this way.


var url = 'https://blablabla.my.salesforce.com/services/data/v42.0/composite';
var payload = { "compositeRequest": [] };
for(var i = 0; i < allRecords.length; i++) {
  payload.compositeRequest.push(
    { "method": "DELETE", 
      "url": "/services/data/v42.0/sobjects/Contact/"+allRecords[i].ContactId 
    });
  if(payload.compositeRequest.length == 25 || i == allRecords.length - 1) {
    var response = HTTP.Post(url, "application/json", Platform.Function.Stringify(payload), headerNames, headerValues);
    payload.compositeRequest = [];
  }
}
  • Thank you for your answer. It's really helpful. However, now I am getting a different error: "Object expected: stringify" – Stuart Sep 26 '18 at 16:12
  • @Stuart Try Stringify instead of Platform.Function.Stringify? Or... Did you make sure to use a capital S instead of lowercase s? – sfdcfox Sep 26 '18 at 16:15
  • I edited my post above. When I log the url variable in the loop, i see it still ends with "....services/data/v42.0/composite". It should change to "....services/data/v42.0/contactId + delete request, right? If that makes sense.. Thanks. – Stuart Sep 27 '18 at 6:50
  • @Stuart No, it's a composite resource endpoint. It allows you to POST multiple requests at once. Please feel free to check the link in the answer, it describes how this feature works. – sfdcfox Sep 27 '18 at 14:18
  • Because of your answer, I read a lot of documentation about this feature. I learned a lot from it, but unfortunately I cant get it to work. The closest I got was getting error: "Unable to retrieve security descriptor for this frame". I abandoned REST for this problem and found a solution using SOAP. Thanks a lot for your time and effort. – Stuart Sep 28 '18 at 7:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.