Add label on existing cards based on text
Asked by Ken K:

I would like to find existing cards that contain a particular word in the description (ie. triads, scales) and assign a label based on the word found - so cards with the word "triad" are assigned the "orange triad" label.

Seems easy enough... search/ filter based on a word, then add the label to the card - however I can't figure out how to do this in Butler.

I am able to use Butler to do this for new cards, however not sure how to do it on existing cards. Thanks in advance for you help.


Answers:
author: David Bakkers
body: Hello [@Ken K](/t5/user/viewprofilepage/user-id/4513762)

No, you can't do that with Butler. If you create a rule with a *Card Content* trigger where the description contains certain text, on the page you will see the following warning message:
> ++Note:++ This will only trigger when you create or modify a card's name or description. It **will not work retro-actively for existing cards on the board**, or for other members on the board (unless they create and enable their own rule).

This sort of advanced action might be possible with [Benkobot.](https://benkoworks.com/benkobot/)

comments: author: Ken K
body: Yes, thank you for the reply [@David Bakkers](/t5/user/viewprofilepage/user-id/1575730)

I have seen that message before and wondered if there was a workaround. I've never heard of Benkobot, but I will check it out. I appreciate your help.


author: David Bakkers
body: No problem. If you feel the answer was correct, please mark it as such, so that the next reader of the topic knows that.

PS. The community member who can answer most questions about BenkoBot's capabilities is [@Iain Dooley](/t5/user/viewprofilepage/user-id/1416112)


author: Iain Dooley
body: [@David Bakkers](/t5/user/viewprofilepage/user-id/1575730) thanks :)

[@Ken K](/t5/user/viewprofilepage/user-id/4513762) this would do it in BenkoBot you can save it as an "ad hoc" function and then edit the word to run it for each time, you'll also need to update your board name:

<https://gist.github.com/iaindooley/fd22d1a675199e2ba5d70021503923d9>


author: Ken K
body: Thanks for the note [@Iain Dooley](/t5/user/viewprofilepage/user-id/1416112)

I don't know much about benkobot yet but your code looks pretty straight forward. I put the board name "Lesson Log" in the quotes.

I added the code in the AdHoc entry box and clicked Run. It goes for a bit, but returns without actually updating any cards.

Can you explain how the code knows which label to apply? Is there something else I might be overlooking (I've never used this).

I was expecting to see the cards that have "Triad" in them with a new Triad label.

Sorry for the total newbie experience. I appreciate your help.


author: Iain Dooley
body: [@Ken K](/t5/user/viewprofilepage/user-id/4513762) looks like I didn't lower case the search ;) try this:

<https://gist.github.com/iaindooley/fd22d1a675199e2ba5d70021503923d9>


author: Ken K
body: Thanks [@Iain Dooley](/t5/user/viewprofilepage/user-id/1416112) - I totally appreciate your help...

Awesome - it worked! You rock!!

I noticed it created a new colorless label. Is there a way in the code to assign a color to the label? Or is there a way to assign an existing label?

If it's in the docs, just tell me and I'll dig in (just figured you'd know off the top of your head).

Great stuff!! Thanks again!! Saved me hours!!!


author: Iain Dooley
body: [@Ken K](/t5/user/viewprofilepage/user-id/4513762) yeah it just defaults to no colour, you can easily edit the labels to add colours and later it will re-use the same one.

Of course it just uses the Trello API under the hood so you could craft the API call you want yourself to add a label with a colour, eventually it will make it into the Trellinator library ;)

Note that you get 10 free execution minutes per month on the free plan (which you're automatically subscribed to).

Scheduled adhoc functions, library functions and global command groups coming soon to a bot near you!


author: Iain Dooley
body: [@Ken K](/t5/user/viewprofilepage/user-id/4513762) there are also examples of "listeners" and other adhoc code snippets on this board, as well as a developer crash course and we will keep adding stuff here:

<https://trello.com/b/IoHmhz5c/benkobot-community-board>


author: Ken K
body: [@Iain Dooley](/t5/user/viewprofilepage/user-id/1416112) Thank you so much. This was exactly what I needed. My board just became 10x more valuable to me!

I was able to edit the label easily - it just threw me at first because I didn't realize the label had been applied. Killer stuff.

I will definitely check out the resources and try to get my head around the whole benkobot interface. Incredibly helpful and I'm very grateful Iain.


author: Ken K
body: [@Iain Dooley](/t5/user/viewprofilepage/user-id/1416112)

Sorry to bother you Iain... I tried running the same code using an existing label.

Rec'd this error in the console...

<https://api.trello.com/1/cards/6107939cfe2a253a3162e1d2/idLabels> with status code 400:

Should I be able to use an existing label? Perhaps I misunderstood your comment about "re-using the same one".

My intent is to apply the same label on cards containing different text strings (ie. Triads, 351, 513, etc) all getting the same label. To do this, I'm running the script multiple times (with new search strings). It chokes when trying to use the same label name.

Thanks in advance!


author: Iain Dooley
body: [@Ken K](/t5/user/viewprofilepage/user-id/4513762) hey sorry for the delayed response. 400 is a "bad request" error, did you receive any further text or information in the logs?

You should def be able to use existing labels...


author: Ken K
body: Hi Iain - finally getting back to this - thanks for the reply. I still get the error but the script is definitely working. There's not much else than what I wrote above. I wonder if I'm not letting it finish and that's why I'm getting the error.

Either way things are great - thanks again!


Tags: butler