How do only show the first X amount of characters from a message

Hi,
I’m using the marketingmessages and my client doesnt want to fill in the “short message” area for a summary but instead wants the first X amount of characters and a “read more” button.
My thought process was to add a field to my data definition and use “this.content.length().islowerequal(150)” but this doesnt slice off the message, instead only shows messages that have 150 characters or less. This way the front-ender can just use that message to display the summary.
Does sombody have an idea how to split/select only the first 150 characters for example?

~Lode

Hi Lode,
Maybe for the expression that determines the value of the data definition field you can use the substring function ?

Here is an example that i did in the Expression debugger:

What does this expression do:

  • i defined a placeholder for clarity - 6 characters long, this is the %str
  • if my “str” value has more than 3 characters, using substring i grab the characters from the 0th index to the 3rd
  • else ill return the entire string
    If i change the length is greater than 150:

it sees that my string is not more than 150 characters long so it returns it completely.

Does this answer your question ?

Greetings,
Miroslav

Hi Miroslav,
That’s definitly the right way of thinking! But I fail at the last step and I can’t really understand why, maybe you have some idea? To me it looks good, maybe you see the issue?

Hi Lode,

The issue that we can see here is the following:

  • your data definition field expects to have an expression that resolves to a string
  • the expression you entered, states that it will return a boolean, yet within it you return a string.
  • then you have which also indicates that it will be wrapped in a list…

Here is explanation of the expression structure

1data_type:if(condition, value_of_1data_type_to_return_if_condition_is_true, value_of1data_type_to_return_if_condition_is_false)

Judging by the config i see (of course i could be wrong as i don’t know the data model you are dealing with), i think you want to return a single string, that is a text field on some record, so you need to change the Boolean:if to string:if(…), so your expression should be like

string:if( <-- I am returning a string
content.length().isgreaterequal(150), <--- condition
 content.substring(0,150), <-- if condition is true
content <-- if condition is false
) 

Does this help ?

Greetings,
Miroslav

1 Like

Hi Miroslav,
Thank you so much for the explanation. So first you tell the system what kind of datatype I expect to return and afterwards I can make the expression, that clears up so much!
The explanation was very clear!

~Lode

Hi,

Yes, that is if you use static functions (in this case IF is a static data function)

Of course, your expression could’ve been like:
“content.substring(0,150)” - if you type the expression like that, then the field would always return a string of length 150 characters.

With what we set-up above, now you return a substring if its larger then certain value, else the full one.

Depending on your use case - for example if i understood correctly, you want to show something like “Some content… See more…” that expands to the full text. So i can imagine your front-end developer could also make use of the following setup for your data definition - you could have 2 fields:
-short_content - string -content.substring(0,150)
-full_content - string - content
Maybe even a boolean field:
-has_really_long_content - content.length().isgreater(150) (the isgreater
evaluates to a boolean, as you saw above)

Now in the same data definition result you have both results which your front-end developer can utilise further…

But i leave the further setup to your imagination :smiley:

To find out more about expressions you can also refer to this Wiki:

It explains the above in more detail.

1 Like