Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

AEM Adaptive Form Calculate Expression Script

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Employee

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

View solution in original post

10 Replies

Avatar

Correct answer by
Employee

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

Avatar

Level 2

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.

Avatar

Level 2

Thanks Deepak. Your solution works well for me.

Cheers,

Afam.

Avatar

Level 2

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

Employee

Can you try below:

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

Avatar

Level 2

Hi Deepak,

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

Avatar

Employee

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

Employee

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

Level 2

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

Level 2

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.