if you are trying to learn AMPscript, building an integrated custom profile center isn't the best starting point, as you really need to understand core language fundamentals such as rowsets, conditional statements, logical operators and more. However, I've provided and annotated a simple one page example below.
Paste the code below in a CloudPages Landing Page and then use the CloudPagesURL function to link to the page you create — you just need to include the page id as an argument, for example: %%=CloudPagesURL(123)=%%
.
Update: Subscription Management
Regarding your comment on subscription management:
My thought is to create two new custom fields on the contact object called subscription (text) & subscribed (boolean) and retrieve these two fields on a separate cloud page, check the status and subscribe or unsubscribe according to the action.
I wouldn't advise storing subscription data against a Contact record. You are likely to incur technical debt in the long term and it's not good practice. Instead, I would advise you consider creating Campaigns for each 'subscription' and adding the Contact as a Campaign Member. And you can easily send to campaign members from Email Studio or Journey Builder.
However, if you insist on using the Contact record, then I've updated my code with two custom fields with the API names Subscribed__c
and Subscription__c
respectiviely. Subscribed__c
is a checkbox field and Subscription__c
is a picklist with values 'Newsletter' or 'Other'. When the page loads, the selected picklist values are pre-selected on the form.
Additional Notes
I'm not sure how you intend to allow Subscribers to globally opt-out unsubscribe from email, but if this is a requirement, then you would need to call the LogUnsubEvent object from the SOAP API (which you are probably better off calling in SSJS and WSProxy rather than AMPscript) which would unsubscribe the Subscriber and deselect the 'HasOptedOutOfEmail' flag on the Contact record. However, this is outside the scope of this code example.
Also, please be aware that this is code is provided in its simplest form. It's not how I would build this page (I would use AJAX instead and build in more comprehensive exception handling and logging) but I've kept it as simple as I can as you mentioned you are not a developer.
%%[
var @Id, @firstName, @lastName, @email, @subscribed, @subscription, @subscriberRows, @subscriberRow
set @Id = _subscriberKey
/* if form was not sumbitted, get object record */
if RequestParameter("submitted") != true then
/* get rowset */
set @subscriberRows = RetrieveSalesforceObjects(
"Contact",
"FirstName,LastName,Email,Subscribed__c,Subscription__c",
"Id", "=", @Id )
/* get row (there will only be 1) */
set @subscriberRow = Row(@subscriberRows, 1)
/* get fields */
set @firstName = Field(@subscriberRow, "FirstName")
set @lastName = Field(@subscriberRow, "LastName")
set @email = Field(@subscriberRow, "Email")
set @subscribed = Field(@subscriberRow, "Subscribed__c")
set @subscription = Field(@subscriberRow, "Subscription__c")
else
/* form was sumbitted, update object record with form data */
var @updateRecord
set @firstname = RequestParameter("firstname")
set @lastName = RequestParameter("lastname")
set @email = RequestParameter("email")
set @updateRecord = UpdateSingleSalesforceObject(
"Contact",@Id,
"FirstName", @firstname,
"LastName", @lastName,
"Email", @email,
"Subscribed__c", @subscribed,
"Subscription__c", @subscription
)
endif
]%%
<!DOCTYPE html>
<html>
<body>
%%[ if @updateRecord == 1 then ]%%
<p>Record Updated</p>
%%[ elseif @updateRecord == 0 then ]%%
<p>Update Failed</p>
%%[ endif ]%%
<h2>Update Your Details</h2>
<form action="%%=RequestParameter('PAGEURL')=%%" method="post">
<label>First name</label>
<input type="text" name="firstname" value="%%=v(@firstName)=%%">
<label>Last name</label>
<input type="text" name="lastname" value="%%=v(@lastName)=%%">
<label>Email</label>
<input type="text" name="email" value="%%=v(@email)=%%">
<label>Subscribe</label>
<input type="checkbox" value="subscribed" %%[IF @subscribed == true THEN]%%checked="checked"%%[ENDIF]%%>
<select name="subscripiton">
<option value="">Select one…</option>
<option value="Newsletter" %%[IF @subscription == "Newsletter" THEN]%%selected%%[ENDIF]%%>Newsletter</option>
<option value="Other" %%[IF @subscription == "Other" THEN]%%selected%%[ENDIF]%%>Other</option>
</select>
<input name="submitted" type="hidden" value="true" />
<input type="submit" value="Submit">
</form>
</body>
</html>