3

I´m showing products automatically by typing in an product ID. But there´s a small challenge left with the productname.

The DE-field may have 1000 characters, but I want to show only 40 charcaters and set a "..." only when it is longer than that.

image

current code:

    /* TRIM Produktname */
set @ln = length(@P_I_Title_a)
set @cutoffPos = length(@P_I_Title_a)
set @done = 0

if @ln > @maxLen then
    for @l = @maxLen downto 1 do
      if substring(@P_I_Title_a,@l,1) == " " or substring(@P_I_Title_a,@l,1) == "-" and @done == 0 then
         set @cutoffPos = subtract(@l,1)
         set @done = 1
      endif
    next @l      
endif
set @P_I_Title_a = CONCAT(substring(@P_I_Title_a,1,@cutoffPos)," ... ")
3

I used the following method to accomplish the same thing in a template recently. It uses the IIF, Length, Concat, and Substring functions and it worked pretty well.

%%=IIF(Length(@P_I_Title_a) > 40,concat(Substring(@P_I_Title_a,1,40), '...'),@P_I_Title_a)=%%
| improve this answer | |
0

Are you setting the @maxLen anywhere?

%%[

    var @productName, @l, @ln, @cutoffPos, @done, @productNameTrimmed

    set @productName = AttributeValue("productName")
    set @ln = length(@productName)
    set @cutoffPos = length(@productName)
    set @done = 0
    set @maxLen = 40

    if @ln > @maxLen then

        for @l = @maxLen downto 1 do

          if substring(@productName,@l,1) == " " and @done == 0 then

             set @cutoffPos = subtract(@l,1)
             set @done = 1

          endif

        next @l

    endif

    set @productNameTrimmed = concat(replacelist(substring(@productName,1,@cutoffPos),"",",","-"),"...")

]%%
%%=v(@productNameTrimmed)=%%
| improve this answer | |

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.