Orchard Custom Forms and Workflows - syntax of scripts in a decision node

与世无争的帅哥 提交于 2020-01-03 02:31:04

问题


In Orchard 1.7.1.0, I have a Custom Contact form with a drop-down:

Preferred location: North Office | South Office

I need to be able to fire an email to the relevant office based on the selection from the contact form. Simples yes??

I created a decision node in the workflow, but I'm not sure on the rules syntax based on a token..? The token would be something like {Content.Fields.ContactUsForm.PreferredLocation}

The help text is "You can use ContentItem, Services, WorkContext, and T(). Call SetOutcome(string outcome) to define the outcome of the activity." But I can't seem to find much else on the syntax - I assumed it just accepts C# as I've seen this mentioned on blog posts but I can't seem to get a simple conditional statement running.

The problem I'm having? ..Nothing happens.. No outcomes are fired. If I just put something like SetOutcome("North Office"); in the script field that gets actioned.

This is similar to this post, but the author didn't follow up how he got on and he was advised to use rules, so probably not as relevant to 1.7.1 Orchard. Orchard Custom Form DropDownLists

So anyway, this is my workflow and the code is below.

Thanks for any suggestions..

The script I'm using, because I'm using tokens I've prefixed them with #

if (  #{Content.Fields.ContactUsForm.PreferredLocation} == "North Office") { 
SetOutcome("North Office");  }
else if  (  #{Content.Fields.ContactUsForm.PreferredLocation} == "South Office") {
SetOutcome("South Office");  }

回答1:


Found the solution was to add quotes around the tokens.

    if (  "#{Content.Fields.ContactUsForm.PreferredLocation}" == "North Office") { SetOutcome("North Office");  }
else if  (  "#{Content.Fields.ContactUsForm.PreferredLocation}" == "South Office") { SetOutcome("South Office");  }

Or even simpler:

SetOutcome("{Content.Fields.ContactUsForm.PreferredLocation}");



回答2:


Adding quotes around the tokens will fix the problem. Here is why... Your code will eventually be passed to the Mono.CSharp.Evaluator class and be run. But, before that happens, the tokens #{Content.Fields.ContactUsForm.PreferredLocation} are replaced with their values. The values turn out to be of type string. If you don't add the quotes, the string replace will drop the token values right into your code.

This code:

if (  "#{Content.Fields.ContactUsForm.PreferredLocation}" == "North Office") { SetOutcome("North Office");  }
else if  (  "#{Content.Fields.ContactUsForm.PreferredLocation}" == "South Office") { SetOutcome("South Office");  }

Will actually be executed as this code when evaluated:

 if (  "North Office" == "North Office") { SetOutcome("North Office");  }
else if  (  "North Office" == "South Office") { SetOutcome("South Office");  }

One nice benefit of this is you can then tack on any of the string methods you want. ex/ .ToLower(), .Contains(), etc. Here is an example of doing this to help with evaluating the user input. Somewhat frivolous in this case since the data comes from a drop down list.

if (  "#{Content.Fields.ContactUsForm.PreferredLocation}".ToLower() == "north office") { SetOutcome("North Office");  }
else if  (  "#{Content.Fields.ContactUsForm.PreferredLocation}".ToLower() == "south office") { SetOutcome("South Office");  }


来源:https://stackoverflow.com/questions/22215976/orchard-custom-forms-and-workflows-syntax-of-scripts-in-a-decision-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!