SOLVED

Velocity Script in Webhook

Go to solution
kushal_arya
Level 2

Velocity Script in Webhook

Hi Community,

I need to create a dynamic token that i can pass via an webhook to an external system, in webhooks i have tried inserting tokens including: email script (velocity) as well as  text (static), here static works fine when the call is made but not the velocity script.

for e.g.:

I created an Smart campaign where it triggers the webhook to call an external API with certain details like: Lead Name, Id, program name etc. For that i have created tokens and tried passing onto the webhook, it works but not exactly as expected.

 

Velocity Script:

#if( $lead.FirstName.equals("[Not Defined]") || $lead.FirstName.isEmpty() )
Hi there!
#else
Hi $lead.FirstName!
#end

 

It translated to:

{"codeBlock":"#if( $lead.FirstName.equals(\"[Not Defined]\") || $lead.FirstName.isEmpty() )\nHi there!\n#else\nHi $lead.FirstName!\n#end","scriptingObjects":{"standard":{"2":{"label":"Person","fields":{"28":{"label":"First Name"},"128":{"label":"Country"}}}}}}
 
If i use a normal token like {{program.id}} {{my.staticToken}} etc. directly in the webhook it works fine, and gives the desired output. But if i use a "email script token" it is not translated properly.
 
any help would be appreciated.
Thanks & Regards
Kushal Arya 
KKArya
2 ACCEPTED SOLUTIONS

Accepted Solutions
Jay_Jiang
Level 10

Re: Velocity Script in Webhook

I've replied in your colleagues thread (I'm assuming the other post with the same block of code is from your colleague), but, velocity tokens don't work outside of emails.

 

For you to achieve what you need, which is conditionally changing the greeting depending on the first name value...

can you not code the logic into the receiving service?

alternatively, create a handler in the middle to execute the conditional logic and on-forward the payload to the final endpoint.

 

lastly, depending on how complex your final solution is supposed to be, you can have various versions of hard coded webhooks where a smart campaign can do the routing

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script in Webhook

Yep, like Jay mentions in his (correct) answer, if you already have a remote service expecting a JSON (i.e. non-executable) payload, one would hope that service could do whatever transformation you need.

 

If this isn't possible because the service is operated by a 3rd party, then consider calling an API gateway instead for these types of on-the-fly transforms.

View solution in original post

3 REPLIES 3
Jay_Jiang
Level 10

Re: Velocity Script in Webhook

I've replied in your colleagues thread (I'm assuming the other post with the same block of code is from your colleague), but, velocity tokens don't work outside of emails.

 

For you to achieve what you need, which is conditionally changing the greeting depending on the first name value...

can you not code the logic into the receiving service?

alternatively, create a handler in the middle to execute the conditional logic and on-forward the payload to the final endpoint.

 

lastly, depending on how complex your final solution is supposed to be, you can have various versions of hard coded webhooks where a smart campaign can do the routing

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script in Webhook

Yep, like Jay mentions in his (correct) answer, if you already have a remote service expecting a JSON (i.e. non-executable) payload, one would hope that service could do whatever transformation you need.

 

If this isn't possible because the service is operated by a 3rd party, then consider calling an API gateway instead for these types of on-the-fly transforms.

View solution in original post

kushal_arya
Level 2

Re: Velocity Script in Webhook

Thanks @SanfordWhiteman the service is indeed operated by a 3rd party which accepts the JSON only in a particular format and hence we are now using Mulesoft for the transformations before sending it.

KKArya