Skip to content

Instantly share code, notes, and snippets.

@ejangi
Created November 25, 2021 06:06
Show Gist options
  • Save ejangi/0dd264b36515ce010e2b6c632ac2aa7a to your computer and use it in GitHub Desktop.
Save ejangi/0dd264b36515ce010e2b6c632ac2aa7a to your computer and use it in GitHub Desktop.
<script runat="server">
// You can POST to this endpoint in order to quickly subscribe someone to a Data Extension.
// The request body expects a JSON object with a property labelled "de" that uses the Marketing Cloud
// Data Extension ID (e.g. 6ded84cc-9fbf-4ec5-83e3-06cc82fb380a)
// The rest of the JSON object is just key/value pairs for each field name and field value.
//
// Example:
// POST /subscribe
// {"de":"6ded84cc-9fbf-4ec5-83e3-06cc82fb380a","FirstName": "Joe","LastName":"Bloggs","Email":"[email protected]"}
Platform.Load("Core","1.1.2");
if (Platform.Request.Method() == 'POST') {
var jsonpost = Platform.Request.GetPostData();
var json = Platform.Function.ParseJSON(jsonpost);
var deId = json['de'] != undefined ? json['de'] : "";
delete json.de;
if (deId.length == 0) {
Platform.Response.Write(Stringify({
status: 500,
error: 'INVALID_DE',
message: 'No Data Extension ID provided'
}).replace(/[\n\r]/g, ''));
return;
}
try {
var de = DataExtension.Init(deId);
de.Rows.Add(json);
Platform.Response.Write(Stringify({
status: 201,
error: 'CREATED',
message: 'Record added successfully'
}).replace(/[\n\r]/g, ''));
return;
} catch(e) {
Platform.Response.Write(Stringify({
status: 500,
error: 'INTERNAL_SERVER_ERROR',
message: e.message
}).replace(/[\n\r]/g, ''));
return;
}
} else {
Platform.Response.Write(Stringify({
status: 404,
error: 'NOT_FOUND',
message: 'This resource is not available.'
}).replace(/[\n\r]/g, ''));
return;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment