Press "Enter" to skip to content

How to populate dynamic content in the email using Ampscript code

Last updated on August 19, 2019

Dynamic content is a content that displays in a content area according to the rules that you have defined based on the subscriber’s attributes or data extension column values. Use dynamic content in Email Studio to ensure that a relevant message is reaching your targeted audience.

Scenario: How to send email to marketing cloud subscriber(who is also contact in Salesforce) with his owner details(contact owner)

  1. Go to Content
  2.  create a new content
  3. choose freeform
  4. Add below code and save it


var @ContactTable, @contactRow, @ownerTable, @ownerRow, @Id2
var @contactownerFirstname, @contactownerLastname, @contactownerTitle, @contactownerEmail, @contactownerPhone, @contactownerFullPhotoUrl

Set @ContactTable = RetrieveSalesforceObjects(“Contact”, “Id,FirstName, LastName, Email, Owner”, “Id”, “=”, _subscriberKey)
Set @contactId = _subscriberKey
Set @contactRow = Row(@ContactTable,1)

Set @Id2 = FIELD(@contactRow, “Owner”)

Set @ownerTable = RetrieveSalesforceObjects(“User”, Name, FirstName, LastName, Title, Phone, Email, FullPhotoUrl”, “Id” “=”, @Id2)

Set @ownerRow = ROW(@ownerTable,1)

Set @contactownerFirstname = FIELD(@ownerRow, “FirstName”)
Set @contactownerLastname = FIELD(@ownerRow, “Lastname”)
Set @contactownerTitle = FIELD(@ownerRow, “Title”)
Set @contactownerEmail = FIELD(@ownerRow, “Email”)
Set @contactownerPhone = FIELD(@ownerRow, “Phone”)
Set @contactownerFullPhotoUrl = FIELD(@ownerRow, “FullPhotoUrl”)



/** Write an HTML code based on how you want to populate above fields dynamically**/

Note: If you want to store the information that has been sent to individual Subscriber via email, Go to Marketing Cloud, create Data Extension by selecting  Email Studio> Subscribers > Data Extensions, for each dynamic variable, define it as a field

After creating DE and adding fields in the DE, Include following snippet in the dynamic content code block

INSERTDE(“Your DE Name”, “Contact Id”, @contactId, “Owner FirstName”, @contactownerFirstname, “Owner LastName”, @contactownerLastname, “Owner Title”, @contactownerTitle, “Owner Email” @contactownerEmail, “Owner Phone”, @contactownerPhone, “Owner Photo”, @contactownerFullPhotoUrl )

Now create a new email or choose existing email and add dynamic content block in the email

You can add rules in the content block for example if contact is in platinum status show Dynamic content in the email if not send generalized email





One Comment

  1. Elliott Elliott May 2, 2018


    I noticed that there’s an “end if” at the end, does there need to be an IF to start the statement, or does the statement constitute a start “IF”

    Great work!

    Thanks, Elliott

Leave a Reply