Limit characters from collection

hey everyone! I am getting data from an API and now I want to allow the user to Tweet the result.

It’s working (thanks to this community) but I now need to limit the # of characters that can be sent to Twitter since the max is 280.

I’ve posted a screenshot of the JS connected to the button where I am trying to limit the # of characters. It’s not giving me an error but it also doesn’t seem to be working. Any tips?

I am on my phone now but it looks like you are returning the original unchanged value.
Line 3 you are encoding the uri variable and assigning it to the encoded variable.
Line 5 you check the length of uri and assign a different value, but encoded is unchanged by this ooeration.
Line 7,9 your return value is calculated from the variable encoded that is still unchanged.
Try moving the code on line 3 after the code on line 5.
On a side note “uri” and “url” are very easy to misstype, you can try to use more specific and different names.

thanks, I moved the lines but it didn’t seem to have any impact on shortening the text… perhaps it’s an issue with line 5 itself?

I think it should be:
if (uri.length > 250) { uri = uri.slice(0,250) }

1 Like


thanks, that kicked back an error… see screenshot!

From your code the assumption is that uri is a string. You need to decide how to handle the case of uri being undefined as in this case.
You can decide not to run the workflow at all or you can return a default value. You can add this logic with weweb formulas too.
Without knowing the context of your workflow I can suggest only partial solutions. A dirty quick fix can be to add uri = uri || ""

1 Like

Hi @nocodedevs :wave:

You can actually do this with the subText no-code formula:

3 Likes

ok thanks so much!

1 Like