0

I'm super new to using AMPSCRIPT and apologize if my code is terrible. I'm tasked with trying to embed Ampscript at the Template level, combined with html to have a dynamic header written into the template, followed by space in the template for an email body, a custom button, then email icon.

The desired output of the dynamic header is to have it dependent on the value of Attribute "Account ID," which maps to our integration with Salesforce Account ID though I've replaced the actual IDs in my code below with placeholders to protect the guilty ;-). Essentially the logic of the header should be: if Account ID = A, Content Area "A", else if Account ID = B, Content Area "B", else if Account ID = C, Content Area "C", else Content Area "D."

Updated code with Adam Spriggs great input. I'm ALMOST THERE, but for some reason the code below keeps returning the "could not find header" error.

%%[ /* Template AMPScript */
var @accountID, @header set @accountID = AttributeValue("Account ID") /*     handles null values */
if @accountID == "0013000000cHWiRAAW" then
set @header = ContentAreaByName('my contents\headers\headerA')
elseif @accountID == "00130000015FHUtAAO" then
set @header = ContentAreaByName('my contents\headers\headerB')
elseif @accountID == "0013000000cHH7sAAG" then
set @header = ContentAreaByName('my contents\headers\headerC')
else
set @header = ContentAreaByName('my contents\headers\headerD')
endif
if empty(@header) then raiseError("could not find header") /* abort the send      */ endif
// ]%%
//other html//<img src="%%=v(@header)=%%">//other html//

Content Area with Headers

  • Can you post a screen shot of your Content Area folder -- one that includes the list of the Content Areas within it? – Adam Spriggs May 6 '16 at 16:57
  • Hi @AdamSpriggs - I added the screen shot you requested to my original post. Appreciate your help so much! – Faith Kindle May 9 '16 at 19:37
  • Does @accountID have a valid value in your test audience? You can test by commenting out the raiseerror() conditional and outputting @accountID with %%=v(@accountID)=%% after your AMPScript block. – Adam Spriggs May 9 '16 at 19:43
  • Hi @AdamSpriggs - the account identified returned fine, & when I comment out the error, the images work as hoped! They do include the standard broken image icon i.stack.imgur.com/r390N.png before the image and "> after the image. I'll need to iron those wrinkles out. But well on my way now & could not have done this without you. Thank you so much! – Faith Kindle May 11 '16 at 20:07
  • 1
    Thank you @AdamSpriggs! You've put me on the right path, but I'm hesitant to accept the answer until I can edit the code to get it to work. Img src doesn't quite do it - it consistently causes the broken icon prior to the image & shows syntax (including >) after. (I suspect that's why the error was thrown.) I'm going to try pointing @header to a url rather than content & see if img src plays better? Or I will try another ampscript tag to get to the desired result. Once I truly "get it" & can include needed edits, I'll certainly accept. I appreciate all your time & help with this! :-) – Faith Kindle May 16 '16 at 18:06
1

Here's how I would do it. The header HTML would reside in Content Areas in My Contents > Headers

%%[ 
/* Template AMPScript <div style="display:none;">*/

var @accountID, @header
set @accountID = AttributeValue("Account ID") /* handles null values */

if @accountID == "18digitSFDCIdA" then

 set @header = ContentAreaByName("my contents\headers\headerA")

elseif @accountID == "18digitSFDCIdB" then

 set @header = ContentAreaByName("my contents\headers\headerB")

elseif @accountID == "18digitSFDCIdC" then

 set @header = ContentAreaByName("my contents\headers\headerC")

else 

 set @header = ContentAreaByName("my contents\headers\headerD")

endif

if empty(@header) then
  raiseError("could not find header") /* abort the send */
endif

/*</div>*/
]%%
<!-- (other HTML) -->
<img src="%%=v(@header)=%%">
<!-- (other HTML) -->

You could also simplify it like this if you name your Content Areas as the AccountID values.

%%[ 
/* Template AMPScript <div style="display:none;">*/

var @accountID, @header
set @accountID = AttributeValue("Account ID") /* handles null values */

set @header = ContentAreaByName(concat("my contents\headers\" ,@accountID))

if empty(@header) then
  raiseError("could not find header") /* abort the send */
endif

/*</div>*/
]%%
<!-- (other HTML) -->
<img src="%%=v(@header)=%%">
<!-- (other HTML) -->
  • Adam you are awesome & thank you so much. You have to use content, else you end up with a string of gibberish from the GetHTTP. – Faith Kindle May 4 '16 at 20:53
  • Hit send too soon. Here's my version of the start of your beautiful code. Unfortunately I'm getting the "could not find header" error. I must still be missing something! – Faith Kindle May 4 '16 at 20:58
  • I'm going to paste in the code as an answer since the at symbol is confusing in comments. – Faith Kindle May 4 '16 at 21:00
  • Also the Contents I'm pointing to definitely exist. I can't think of why they would throw that error. – Faith Kindle May 4 '16 at 21:01
  • Please update your question with the additional code. Did you run Validate from the toolbar? It'll give you more details on any syntax errors. – Adam Spriggs May 4 '16 at 21:24
0

/* Template AMPScript */

var @accountID, @header set @accountID = AttributeValue("Account ID") /* handles null values */

if @accountID == "0013000000cHWiRAAW" then

set @header = ContentAreaByName('my contents\headers\headerA')

elseif @accountID == "00130000015FHUtAAO" then

set @header = ContentAreaByName('my contents\headers\headerB')

elseif @accountID == "0013000000cHH7sAAG" then

set @header = ContentAreaByName('my contents\headers\headerC')

else

set @header = ContentAreaByName('my contents\headers\headerD')

endif

if empty(@header) then raiseError("could not find header") /* abort the send */ endif

// ]%%

  • Its better to include some explanation rather than just posting the code – Vigneshwaran G May 4 '16 at 21:53
  • Thank you, Vigneshwaran. I pasted this code as a reply (to my own post) as part of the discussion I am having with Adam Spriggs. All the @ signs in the code got messy when I tried to paste the code in a comment response to his post. – Faith Kindle May 5 '16 at 12:21
0

I ended up modifying Adam's great code to this & it is working for our purposes:

%%[ /* Template AMPScript */

var @accountID, @header set @accountID = AttributeValue("Account Code") /* handles null values */

if @accountID == "ClientA" then

set @header = ContentAreaByName("My Contents\HeaderA")

elseif @accountID == "ClientB" then

set @header = ContentAreaByName("My Contents\HeaderB")

elseif @accountID == "ClientC" then

set @header = ContentAreaByName("My Contents\HeaderC")

elseif @accountID == "ClientD" then

set @header = ContentAreaByName("My Contents\HeaderD")

else

set @header = ContentAreaByName("My Contents\HeaderGeneric")

endif

/if empty(@header) then raiseError("could not find header") / abort the send / endif/

// ]%%

html... %%=v(@header)=%% ...more html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.