[SalesForce] Query and Update Data Extension from within Landing Page built in Code View – syntax

I'm working on a landing page that will function as a Subscriber Preferences form for email marketing. It needs to query a particular Data Extension using a given email and populate fields automatically if that email exists, and obviously input the form contents to said Data Extension when submitted.

The form is built in HTML and CSS as the formatting limitations in the content builder were too restrictive, and I have some Javascript to pull the email address from a URL query so I don't need to handle live input there. I don't have much experience with SFMC but as far as I've been able to determine I should be able to embed some Ampscript within the page to handle pre-populating the form, but I'll want to handle form submission using JS and send the data off to a separate page (presumably a code resource?) that will handle updating the DE.

What I'm unsure of is how the syntax is done in a case where I'm writing the HTML directly? Does it need to be in a Script tag of some description?

Best Answer

Typically, there are two ways to go about this, and they're both reasonably sufficient. AMPScript's web services API can be leveraged to update the data extensions, and so can server-side Javascript and their custom Javascript platform functions.

If you are to go the route of using AMPScript then your form page will also contain the processing code. Since AMPScript runs at page load time the page will need to pass information along in the form post and your ampscript can run the processing functions to update the Data Extension when the form data is present. You'll need to build a web service API object using AMPscript and then insert or update that object with a command.

The other way to go about it is to asynchronously post the data to a separate server-side javascript page and send the response back to the form page for confirmation. You can load js libraries on cloud pages / microsites so you should be able to leverage frameworks like jquery. Your form page then listens for the response and updates appropriately when the success response is passed.

There's plenty of documentation on these methods out there, and here's some initial links to get your wheels turning:

SSJS: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/ssjs_serverSideJavaScript.htm

Ampscript Functions: https://developer.salesforce.com/docs/atlas.en-us.mc-programmatic-content.meta/mc-programmatic-content/functions.htm

Related Topic