Best answer

How to write the conditional Python code that always outputs URLs for Slack?

  • 20 June 2023
  • 1 reply

Userlevel 1

Hi @SamB, could you maybe help me how I could write the Python code and still use URLs? Would I then only include a string variable ”<|Link text>”? 

Many thanks!


Best answer by SamB 21 June 2023, 11:45

View original

1 reply

Userlevel 7
Badge +11

Hi @greenhouseautomation! 👋


Hope you don’t mind but I moved your follow up question out into a new Topic since it’s regarding the writing of Python code, not the formatting of links in a Slack message specifically.

You asked in the previous topic “Also, is there an easier way than the “Zapier Paths” to use conditions and not show “UTM:” if all following variables are empty?​​​​​​. So am I correct in thinking that you want help writing Python code that creates some conditional logic for the Zap (instead of using Paths) to only output UTM fields if they all contain a value, but always output the link?

If yes, you’d just put the link in the Slack message itself so that it’s always output. It wouldn’t need to be included in the code. Then you’d set up the Code action to only output “UTM: ” followed by the values from the UTM fields if they all contained a value. I can’t write the code for you but if you get stuck please share a screenshot of the Input Data and Code fields in of the Code by Zapier action in your Zap. That’ll help to give folks in Community more context on what might need to be tweaked to get it working.

That being said, if one of the UTM fields is empty, will all the other UTM fields be empty? 

If so, then it might be easier to use a Formatter (Text > Spreadsheet-Style Formula) action. In that action you could use a standard IF formula to check whether a specific UTM field contains a value, if it doesn’t it should output nothing, and if it does then output the values for all the UTM fields. For example:

IF("{{tracking__utm_campaign_field_goes_here}}" = "", "", "UTM: {{tracking__utm_campaign_field_goes_here}},{{tracking__utm_source_field_goes_here}},{{tracking__utm_content_field_goes_here}},{{tracking__utm_medium_field_goes_here}},{{tracking__utm_term_field_goes_here}}")

You can learn more about using this kind of formula here: 

Then in the Slack action you’d select the output field from that Formatter action, which would only add the UTM values to the Slack message if the Tracking UTM Campaign field contained a value.

Hope that helps to point you in the right direction here. Let me know if you run into any issues on that, happy to help further!