[SalesForce] REST API Methods in AMPScript or SSJS

Is there any way via AMPScript or SSJS to utilize HTTP methods outside of GET and POST?

I have had no problem utilizing these two via the HTTPPost() and HTTPGet() functions, but have not found any way to utilize any of the other methods – such as PUT, DELETE or PATCH.

The reason I ask is that I have inherited some script all in AMPscript that gathers, formats and separates the data/HTML for emails, but now we need to use PUT to update via REST API and I would rather not have to rewrite the whole thing or otherwise export this data and send to another system to run the REST call.

I also would prefer a non-clientside option as it is a major security risk – so no JavaScript/Ajax.


EDIT

I have found something that seems to hopefully point me in the right direction, but have had no luck myself yet.

The function is in SSJS as Script.Util.HttpRequest()

It shows the option to change the method, but as I said, I have not gotten this to work yet:

Additional Properties
contentType – String value indicating content
type sent with requests using POST method
method – String value
indicating HTTP method to use:
GET
DELETE
HEAD
OPTIONS
PATCH
POST
PUT
postData – String value indicating POST data sent with request –
required for POST method

Best Answer

GREAT NEWS!!!

Firstly I want to thank SFMC Global Support, for the first time since they were Salesforce they were actually EXTREMELY helpful and quick. (Shout out to Jeff Ross)

We found out that with the Script.Util.HttpRequest() function you actually CAN use

GET
DELETE
HEAD
OPTIONS
PATCH
POST
PUT

It took a few days of both of us digging and searching, but we found it. Hope this helps someone else avoid this headache!

Below is a sample script for this:

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

var accessToken = {{yourToken}};
var url = 'https://www.exacttargetapis.com/asset/v1/content/assets/{{ContentID}}'

var payload = '{{yourPayload}}';

var auth = 'Bearer ' + accessToken;

  var req = new Script.Util.HttpRequest(url);
  req.emptyContentHandling = 0;
  req.retries = 2;
  req.continueOnError = true;
  req.contentType = "application/json"
  req.setHeader("Authorization", auth);
  req.method = "PUT"; /*** You can change the method here ***/
  req.postData = payload;

  var resp = req.send();
</script>

EDIT - Adding in some info on parsing the results (also is on my blog post in more details)

First thing to note is that the results are returned in a Script.Util.Response object which is in a CLR format. Without going into details, pretty much this means that the current result is something that you cannot interact with directly.

So first things first, we need to change the content returned from CLR to a String. Luckily there is a native Javascript function to do this. String().

var resultString = String(result.content)

As a string, the results are not easily parsed or dissected to gather the info, but there is another function in SSJS that we can use – ParseJSON().

See below for sample of turning the String into a JSON

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

This will then store the content from the results of the request inside of a JSON, which is easily interacted with via SSJS. From here you can treat this result the same as other arrays or objects and request info accordingly.