AEM Adaptive Form Calculate Expression Script

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

How can I assign the value of a field based on the value of another field. For example if TextField1.value == 1 then this.value = 2.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

mandeepg6805152

Employee

Avatar

mandeepg6805152

Employee

mandeepg6805152
Employee

15-10-2015

This can be achieved by writing an expression like this.value =   (TextField1.value ==1) * 2 . However if your case is so complex that you cannot derive an expression relating the fields then  you can write a service, make an ajax call to it passing the value of TextField1, and return desired value of the field via service and assign the responseText of the field to the value  in the calculate expression  like

this.value =    $.ajax (url: "service_end_point"
                         data: {valueOfTextField: TextField1.value}

                     ).responseText

Answers (9)

Answers (9)

Avatar

Avatar

deepak_k_

Employee

Avatar

deepak_k_

Employee

deepak_k_
Employee

15-10-2015

Let's say this in your example point to field2 for which you want to write calculate expression. Open the propertiies(or edit) dialog of field2 in edit mode and in Calculate Expression input box, enter expression:

( TextField1.value == 1) ? 2 : 0

Calculate Expression should be basically a valid javascript expression and should return a value which would be automatically assigned to current field(field2 in above case)

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

Hi Deepak,

Apologies, It does work!

My stupidity. My First Name field was called name. Once I changed it to firstname, code worked!

Thanks mate.

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

Hi Deepak,

That is exactly what I expect. When I copy the code you sent in your earlier post and paste it in my last name calculate expression field, I don't get any values for charles, susan or tim. Could there be an error in the code I'm pasting? please see code below. 

( firstname.value == "charles" ) ? "John" : (( firstname.value == "susan" ) ? "morris" : (( firstname.value == "tim" ) ? "michaels" : ""))

Avatar

Avatar

mandeepg6805152

Employee

Avatar

mandeepg6805152

Employee

mandeepg6805152
Employee

15-10-2015

Hi Afam,

This works for me. It gives me "john"  in the other field if it inscribe "charles"  in the field firstname  , "morris" for "susan", "michaels" for "tim" and nothing for any other value. Is it not what you are expecting? 

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

Hi Deepak,

It didn't work for me. Does it work for you?

Avatar

Avatar

deepak_k_

Employee

Avatar

deepak_k_

Employee

deepak_k_
Employee

15-10-2015

Can you try below:

( firstname.value == "charles" ) ? "John" : (( firstname.value == "susan" ) ? "morris" : (( firstname.value == "tim" ) ? "michaels" : ""))

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

Hi,

I'm wondering if it is possible to have an expression like ( firstname.value == "charles" ) ? "John" : " "; ( firstname.value == "susan" ) ? "morris" : " "; ( firstname.value == "tim" ) ? "michaels" : " "; in my last field calculate expression?

At the moment, it only works correctly for the first expression set i.e. charles but dosen't work for any of the rest.

Thanks Afam.

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

Thanks Deepak. Your solution works well for me.

Cheers,

Afam.

Avatar

Avatar

famruch

Avatar

famruch

famruch

15-10-2015

Thanks Mandeep. For now, Deepak solution below works for me but I think when I start to implement more complex solutions, I'll have to refer to your ajax solution.