{"_id":"59ceb930dedc7c001c9baf93","project":"5746675a5aa03c0e00fce9bc","version":{"_id":"5746675a5aa03c0e00fce9bf","__v":8,"project":"5746675a5aa03c0e00fce9bc","createdAt":"2016-05-26T03:02:50.849Z","releaseDate":"2016-05-26T03:02:50.849Z","categories":["5746675a5aa03c0e00fce9c0","574899e7e8c6a420000b7314","57489c11d929e919006ed4ae","57489c82d929e919006ed4b1","57489ce91b345d0e004e5231","5748a82e1b345d0e004e5256","59ce5c88e8c105001a4bedd7","59f96cd129b4d600246c2899"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"59ce5c88e8c105001a4bedd7","project":"5746675a5aa03c0e00fce9bc","version":"5746675a5aa03c0e00fce9bf","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2017-09-29T14:45:28.177Z","from_sync":false,"order":1,"slug":"blockspring-scripts","title":"Blockspring Scripts"},"user":"545988354f86a50800f9cce4","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-09-29T21:20:48.554Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"examples":{"codes":[]},"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"Let's learn how to setup workflows. These use event triggers and will be getting and setting data from an object.\n[block:api-header]\n{\n  \"title\": \"Usecase: Descriptive Card Names\"\n}\n[/block]\nIsn't it annoying when teammates give Trello cards short names like \"todo\" or \"tbd\"? In this tutorial, we'll write a script that asks for more descriptive names, and along the way, we'll learn how to:\n\n- Setup event triggers. This will be used to run a function whenever a new card is created.\n- Read data. This will be used to read a card's name.\n- Set data. This will be used to update a card's name with an alert (eg \"be more descriptive!\")\n\n## ADD AN EVENT TRIGGER\nOften, you'll want your script to run when something happens in your app. These are called event triggers and can be setup from the editor like so:\n\n1) Click **Add+** next to **Triggers** in the right sidebar.\n2) Choose your trigger, in this case **New Card** .\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/d1044e1-event.gif\",\n        \"event.gif\",\n        796,\n        598,\n        \"#1076b1\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\n## GET FIELDS\nEdit the code to print out a card's name when it is created. We'll use the `get_field` function this time.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import trello_app\\n\\ndef on_create_card():\\n    card = trello_app.get_current_card()\\n    print(card.get_field(\\\"name\\\"))\\n    \",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"How do I know what fields I can get?\",\n  \"body\": \"Every object (like a card, board, project, book, etc) has its own fields.\\n\\nFor instance, if you'd like to see a card's fields - just run `print(card.fields)`.\"\n}\n[/block]\n## TEST AN EVENT TRIGGER\nTo test your event trigger, create a new card. You should see the name print out in your logs.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/d54e03c-test.gif\",\n        \"test.gif\",\n        1159,\n        604,\n        \"#2a8f8b\"\n      ]\n    }\n  ]\n}\n[/block]\n## SET FIELDS\nIf a card name is too short, let's overwrite it with an alert for your teammate. This time we'll use the `set_field` function.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import trello_app\\n\\ndef on_create_card():\\n    card = trello_app.get_current_card()\\n    name = card.get_field(\\\"name\\\")\\n    if len(name) < 5:\\n        alert = \\\"[FIX NAME] %s\\\" % name\\n        card.set_field(\\\"name\\\", alert)\\n\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\n## USE THE SCRIPT\nNow that we have our script complete - with an event trigger, getting data, and setting data - we can try it out.\n\nCreate a card with less than 5 characters in its name, and you'll see it instantly alert you to fix the name.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/b3f3245-short.gif\",\n        \"short.gif\",\n        1151,\n        598,\n        \"#288a8c\"\n      ],\n      \"border\": true\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Need Help?\"\n}\n[/block]\nIf you run into any issues, or what to chat with others, [connect to the Blockspring community](https://blockspring-help.readme.io/docs/getting-help) right from the Scripts editor.","excerpt":"","slug":"quickstart-1","type":"basic","title":"Tutorial: Workflows, Get Data, Set Data"}

Tutorial: Workflows, Get Data, Set Data


Let's learn how to setup workflows. These use event triggers and will be getting and setting data from an object. [block:api-header] { "title": "Usecase: Descriptive Card Names" } [/block] Isn't it annoying when teammates give Trello cards short names like "todo" or "tbd"? In this tutorial, we'll write a script that asks for more descriptive names, and along the way, we'll learn how to: - Setup event triggers. This will be used to run a function whenever a new card is created. - Read data. This will be used to read a card's name. - Set data. This will be used to update a card's name with an alert (eg "be more descriptive!") ## ADD AN EVENT TRIGGER Often, you'll want your script to run when something happens in your app. These are called event triggers and can be setup from the editor like so: 1) Click **Add+** next to **Triggers** in the right sidebar. 2) Choose your trigger, in this case **New Card** . [block:image] { "images": [ { "image": [ "https://files.readme.io/d1044e1-event.gif", "event.gif", 796, 598, "#1076b1" ], "border": true } ] } [/block] ## GET FIELDS Edit the code to print out a card's name when it is created. We'll use the `get_field` function this time. [block:code] { "codes": [ { "code": "import trello_app\n\ndef on_create_card():\n card = trello_app.get_current_card()\n print(card.get_field(\"name\"))\n ", "language": "python" } ] } [/block] [block:callout] { "type": "info", "title": "How do I know what fields I can get?", "body": "Every object (like a card, board, project, book, etc) has its own fields.\n\nFor instance, if you'd like to see a card's fields - just run `print(card.fields)`." } [/block] ## TEST AN EVENT TRIGGER To test your event trigger, create a new card. You should see the name print out in your logs. [block:image] { "images": [ { "image": [ "https://files.readme.io/d54e03c-test.gif", "test.gif", 1159, 604, "#2a8f8b" ] } ] } [/block] ## SET FIELDS If a card name is too short, let's overwrite it with an alert for your teammate. This time we'll use the `set_field` function. [block:code] { "codes": [ { "code": "import trello_app\n\ndef on_create_card():\n card = trello_app.get_current_card()\n name = card.get_field(\"name\")\n if len(name) < 5:\n alert = \"[FIX NAME] %s\" % name\n card.set_field(\"name\", alert)\n", "language": "python" } ] } [/block] ## USE THE SCRIPT Now that we have our script complete - with an event trigger, getting data, and setting data - we can try it out. Create a card with less than 5 characters in its name, and you'll see it instantly alert you to fix the name. [block:image] { "images": [ { "image": [ "https://files.readme.io/b3f3245-short.gif", "short.gif", 1151, 598, "#288a8c" ], "border": true } ] } [/block] [block:api-header] { "title": "Need Help?" } [/block] If you run into any issues, or what to chat with others, [connect to the Blockspring community](https://blockspring-help.readme.io/docs/getting-help) right from the Scripts editor.