Copy link to clipboard
Copied
Hi
I have a form where I want to pre-fill the e-mail field based on firstname and lastname fields. I have this script in the email field:
var fornavn = this.getField("Fornavn").valueAsString;
var etternavn = this.getField("Etternavn").valueAsString;
fornavn = fornavn.replace(" ","");
etternavn = etternavn.replace(" ","");
event.value = fornavn + "." + etternavn + "@company.com";
But how can I allow the user of the form to override this and enter their own email address if they don't want to have the one suggested by the script?
Copy link to clipboard
Copied
Hi,
You could just check if the field has anything in it and if it does then don't run your script, that way if it is empty, you can populate it, and if the user overwrites it it will stay.
// something like.
if (event.value == "")
{ // populate; }
Regards
Malcolm
Copy link to clipboard
Copied
I'm not sure if that would help as the field is populated already when you come to it. If you enter something in the first field (fornavn/firstname) then the event automaticly triggers the email field to change when you exit the firstname field.
Copy link to clipboard
Copied
This is a bit complicated to implement. Also, there's the question of when it should update and when it shouldn't.
If you set it up to update the value when either of the name fields are edited, it could happen that the user entered an email address and then changed one of those fields and it overwrites the value they already entered, which might not be desired.
Because of that I would use a button field to generate the automatic email address when clicked, instead of a calcuation script. That way the user can control when it happens.
Copy link to clipboard
Copied
Use a checkbox to override the calculation.
In a calculation script, "event.rc = false" will block the field from being updated from the scirpt.