Unfortunately code "disappear" as you said.
Maybe you have another solution / some tip for this bug?
As I said, it should still work, even if it disappears.
The only solution is to place the code in a doc-level function and then call that function from the event.
So like this:
// Doc-level script
function validateFieldsLength() {
var newValue = AFMergeChange(event);
var otherText = this.getField("Field2").valueAsString;
var maxTextLength = 100;
if ((newValue.length + otherText.length) > maxTextLength) {
app.alert("The combined text may not exceed " + maxTextLength + " characters.");
event.rc = false;
}
}
// Field-level script - Keystroke event
validateFieldsLength();