Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM Adaptive Form Calculate Expression Script

famruch
Level 2
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
mandeepg6805152
Correct answer by
Employee
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
mandeepg6805152
Correct answer by
Employee
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

famruch
Level 2
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.

famruch
Level 2
Level 2

Thanks Deepak. Your solution works well for me.

Cheers,

Afam.

famruch
Level 2
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.

deepak_k_
Employee
Employee

Can you try below:

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

famruch
Level 2
Level 2

Hi Deepak,

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

deepak_k_
Employee
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)

mandeepg6805152
Employee
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? 

famruch
Level 2
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" : ""))

famruch
Level 2
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.