Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

User-defined function or script

Avatar

Level 3

I have a procedure I want a form to do every time the user enters any field.  There are a lot of fields, and the code is moderately long, so I'd prefer to not repeat the code in every enter event for every field.  Is there a way to define a function or a script that I can call?

This will not only reduce my file size (which is already larger than I'd like), but also obviously help me keep things simple if I need to revise or fix this script in the future.

Thanks

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

You can create a script object by right-clicking in the hierarchy and selecting 'Insert Script Object'. Then rename the script object to something useful like masterScriptObject. You can then put your code in there e.g. function myFunction(){...insert code...}. Then you call the function from the fields like this:

masterScriptObject.myFunction();

You can call the function from any event you like.

Hope this helps you.

View solution in original post

2 Replies

Avatar

Correct answer by
Former Community Member

You can create a script object by right-clicking in the hierarchy and selecting 'Insert Script Object'. Then rename the script object to something useful like masterScriptObject. You can then put your code in there e.g. function myFunction(){...insert code...}. Then you call the function from the fields like this:

masterScriptObject.myFunction();

You can call the function from any event you like.

Hope this helps you.

Avatar

Level 3

That worked great!

For those of you trying this out, I might save you a few minutes of scratching your head (if you're as slow as I am) by telling you to make sure that you have the script where you call the function set to JavaScript instead of FormCalc.  Kept throwing me errors.

And the function script will have to be in JavaScript.  It doesn't give me FormCalc as an option.  Was not a problem for me--just took a minute or two to convert.

Thanks.  Big help.

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----