1

The concat string seems to be incorrectly evaluating string in ampscript.

AMP Script - Works

%%[/* tag - test 1 <div style="display:none"> */

SET @text = "You are <0>amazing</0>."
SET @closeTag = "</0>"
SET @tag = @closeTag
SET @tagLen = LENGTH(@tag)
SET @idxTag = indexOf(@text, @tag)

/* </div> */ 
]%%

Text: %%=TreatAsContent(@text)=%%
Tag: %%=TreatAsContent(@tag)=%%

Tag Start: %%=TreatAsContent(@idxTag)=%%
Tag Len: %%=TreatAsContent(@tagLen)=%%

Text: %%=v(@text)=%%
Tag: %%=v(@tag)=%%

Tag Start: %%=v(@idxTag)=%%
Tag Len: %%=v(@tagLen)=%%

Note: I wasn't sure if it should be TreatAsContent or v so i had both

Output

Tag Start Pos: 19
Tag Length: 4

--

When i try to parameterize the creation of the closeTag, there seems to be an issue with how the string is evaluated.

AMP Script - Doesn't work.

%%[/* tag - test 2 <div style="display:none"> */

SET @i = 0
SET @text = "You are <0>amazing</0>."
SET @closeTag = concat("</", @i, ">")
SET @tag = @closeTag
SET @tagLen = LENGTH(@tag)
SET @idxTag = indexOf(@text, @tag)

/* </div> */ 
]%%

Text: %%=TreatAsContent(@text)=%%
Tag: %%=TreatAsContent(@tag)=%%

Tag Start: %%=TreatAsContent(@idxTag)=%%
Tag Len: %%=TreatAsContent(@tagLen)=%%

Text: %%=v(@text)=%%
Tag: %%=v(@tag)=%%

Tag Start: %%=v(@idxTag)=%%
Tag Len: %%=v(@tagLen)=%%

Output

Tag Start Pos: 0
Tag Length: 0

Does anyone know how i can resolve this or what the cause of the issue is? Thank you.

Issue Summary: Code Snippets do not work as expected from within Email Editor in Email Studio.

enter image description here

2
  • You might try using the long-form AMPscript block tags instead of using commented-out HTML AMPscript comments to suppress the code: <script runat="server" language="ampscript"></script> – Adam Spriggs Apr 13 at 22:12
  • Thanks, it doesn't change the functionality with it there or not. The div is just to prevent it from showing in the editor but it will still execute. – Jordan Apr 13 at 22:20
2

It looks as though replacing the shorthand opening/closing tags (%%[ ]%%) with the longform does fix the issue for some strange reason.

This is within the context of a Code Snippet block in the email editor.

<script runat=server language=ampscript>

SET @i = 0
SET @text = "You are <0>amazing</0>."
SET @closeTag = concat("</", @i, ">")
SET @tag = @closeTag
SET @tagLen = LENGTH(@tag)
SET @idxTag = indexOf(@text, @tag)

</script>

Text: %%=v(@text)=%%<br>
Tag: %%=v(@tag)=%%<br>
<br>
Tag Start: %%=v(@idxTag)=%%<br>
Tag Len: %%=v(@tagLen)=%%<br>

Result:

Text: You are <0>amazing.
Tag:

Tag Start: 19
Tag Len: 4
1
  • That is the first time I see them improving something. Nice! – Jonas Lamberty Apr 14 at 17:10
1

I took your code that "doesn't work", and it does indeed work.

copy / pasted example two (yes, I made sure) onto a cloudpage, added the AMPscript tags and wrote the output:

%%[

SET @i = 0
SET @text = "You are <0>amazing</0>."
SET @closeTag = concat("</", @i, ">")
SET @tag = @closeTag
SET @tagLen = LENGTH(@tag)
SET @idxTag = indexOf(@text, @tag)

]%%
Tag Start Pos:%%=v(@idxTag)=%%
<br>Tag Length: %%=v(@tagLen)=%% 

result:

Tag Start Pos:19
Tag Length: 4 

I am assuming you did not share the full code and the problem is elsewhere (surrounding loop?)

9
  • That is pretty much the full code. I will update in a moment. I don't have cloud pages, do you have access to Email Studio by any chance to try and generate there? – Jordan Apr 13 at 16:52
  • Should make no difference in my opinion, it's "pure" ampscript and html, regardless of context. Make sure to put it in an html block and it should work exactly the same. – Jonas Lamberty Apr 13 at 16:56
  • Even your snippet does not work in a brand new email (no other code in there). I created a code snippet for the amp script and text component for the output. – Jordan Apr 13 at 17:00
  • 1
    So apparently something is definitely wrong with Code Snippet. Appears that having </ inside of it makes it go crazy. If you change your two vars to concat to separate them out, it resolves the issue: SET @text = CONCAT("You are <0>amazing<","/0>.") and SET @closeTag = concat("<","/", @i, ">"). It gives the correct value. I have not seen any known issues on this anywhere so its an odd quirk indeed! – Gortonington Apr 13 at 17:54
  • 1
    Nice catch. Unfortunately i can't do that since the text is dynamically populated from a DE and the tag <0></0> are part of i18 translations. I used a static string to illustrate the example. I already reached out to SFMC support. Thank you both. – Jordan Apr 13 at 17:59

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.