Form Field Value determined by another Field

Brendon McCarthy asked on November 17, 2014 18:11

I'm trying to set the value of a hidden form field based upon the value selected in another field. For example, I have a radio field where the user selects whether they are 1) Customer or 2) Evaluating. If Customer, set value of field "NotificationTo" = cust@domain.com, otherwise set value to eval@domain.com. I am then using the value of NotificationTo in the AutoNotifcation.

I know I can set the value of the main field to be the email, but I also need the text value. I also have a more advance scenario where I need to set the NotificationTo field to one of 10+ values depending upon the selection.

According to https://docs.kentico.com/display/K81/Using+macros+with+forms you cannot use Context (Data) macros in the "Default Value" property of a field, but I was trying to do something like this before in the Default Value of NotificationTo:

{% if (CustomerEvaluating == "Customer") { "cust@domain.com" } else { "eval@domain.com" } %}

There is also a field property called "Value access function name", but I cannot find its proper use anywhere in the documentation. I'm open to any other methods to configure a form for the scenario as well.

Thanks.

Correct Answer

Vilém Jeniš answered on December 2, 2014 10:10

Hello again!

@Brendon: I managed to simply enthe this macro

{% IfCompare(TheField, "cust", "eval@domain.com" , "cust@domain.com") |(identity)GlobalAdministrator%}

into the 'To e-mail' field of my form in the E-mail notification section. This way I avoid having the destination address stored in the DB as a column and still have the mail sent to where I want.

The field "TheField" is configured to be a dropdown menu (could be a pair of radio buttons...) and the options are:

eval;Evaluating
cust;Cutomer

That would be my recommendation.

BTW: I did this on 8.1.14. Tested it and it works.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on November 17, 2014 19:46

Try using CustomerEvaluating.Value == "Customer"

0 votesVote for this answer Mark as a Correct answer

Brendon McCarthy answered on November 18, 2014 01:38

That doesn't work either unfortunately. Couple of things I've noticed.

One, existing records if re-saved do not get macros applied, only new records.

Two, the documentation here https://docs.kentico.com/display/K81/Reference+-+Field+editor#Reference-Fieldeditor-Settingdefaultformfieldvaluesthroughmacros says macros can be applied on form submission, this does not appear to be the case as I was unable to get it to work using the example in the documentation {% CurrentUser %}. I was however able to get a default value from a macro applied on initial load using the "Edit Value" method. This however does not work as I need the value to be calculated either on dependent field change or on form submit.

My workaround was to use a drop down list form control and use the macro (or SQL) option in the "Data Source" property to select only a single value (from a table or in-line) conditional upon other form field values).

if (CustomerEvaluating == "Evaluating") { 
  return "eval@domain.com"
}
else {
  return "cust@domain.com"
}

Ideally, the textbox form control should allow for macro expressions to set the value conditionally (not just as default) the same way drop-down/multi-select form controls allow.

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on November 19, 2014 22:41

Are you entering the macro value into the macro editor? I have tried to place the macros in the field itself, without clicking the arrow icon and expanding the macro editor, and it hasn't worked in some cases. The sytax seems like it should work.

0 votesVote for this answer Mark as a Correct answer

Vilém Jeniš answered on November 20, 2014 17:00

Hi Brendon! (And Brenden... ☺)

You are right about the MacroExpressions not being supported in the 'Default values' of fields. Well at least not the ones dependent on another value from the same form.

What exactly are you trying to achieve by that? I mean... Surely the ultimate purpose isn't to have an e-mail stored in the database.

Of what sort is your form? Is it a Custom form, a class' form a page(document) type's form? These have different options. Either way, there's always the option of hooking up a piece of code to a global event fired when the form is submitted, but I'd suggest taking a step back, taking a broader look at what you're trying to achieve here (I understand if you're not willing to share that even if it's just because it's not easy to explain...) and trying to take a different approach to the problem.

If some of I have writen doesn't make much sense to you, it might be because I'm assuming you are working with the latest released version of Kentico. Correct me if that's wrong.

0 votesVote for this answer Mark as a Correct answer

Brendon McCarthy answered on December 1, 2014 21:49

@Joshua - If entering the value into the macro editor, it works, but then it only triggers on form "save". Entering into the non-macro editor, it does not. As you suggest, it appears not all macros work using this method described in the documentation https://docs.kentico.com/display/K81/Using+macros+with+forms

@Vilem - The purpose of this use case is to send the form to a different email address based upon a selection in the form. The only way I found this to work with out-of-the-box forms is to save the value of the field selection (i.e. eval@domain.com) into another field "SendTo" (hidden) and then set the "To email" (under Email Notifications) using a macro with the value of this hidden field {%SendTo%}.

0 votesVote for this answer Mark as a Correct answer

Brendon McCarthy answered on December 2, 2014 15:39

Thanks Vilem. That would work and I've marked as correct as it is the simplest solution and utilizes macros efficiently. For this scenario, we're going to stick with storing the email value in the database (for now) for reference so we have a history of where it went.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.