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

Variable scope weirdness?

Avatar

Level 6

I'm having a problem with a script component, and I think it might be a bug.  I reproduced the problem with this simplified version:

if (true) {
    int nTest = 1;
    {
        nTest = 5;
        patExecContext.setProcessDataIntValue("/process_data/@nTest", nTest);
    }
}

I think my process variable nTest should come out as 5, but I'm getting 1. The same problem happens with Strings.

Can someone reproduce this and let me know if I'm losing my mind here?  Thanks.

Jared

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Hi Jared,

I can reproduce the behaviour so your mind is ok.

The Custom Script service is a BeanShell implementation and off the top of my head I am not sure what BeanShell version it is. Apparently scoping has changed in BeanShell (see http://www.beanshell.org/manual/syntax.html).

This works...

boolean test = true;

int nTest;

if (test) {

    nTest = 1;

    {

        nTest = 5;

        patExecContext.setProcessDataIntValue("/process_data/@nTest", nTest);

    }

}

Steve

View solution in original post

4 Replies

Avatar

Correct answer by
Former Community Member

Hi Jared,

I can reproduce the behaviour so your mind is ok.

The Custom Script service is a BeanShell implementation and off the top of my head I am not sure what BeanShell version it is. Apparently scoping has changed in BeanShell (see http://www.beanshell.org/manual/syntax.html).

This works...

boolean test = true;

int nTest;

if (test) {

    nTest = 1;

    {

        nTest = 5;

        patExecContext.setProcessDataIntValue("/process_data/@nTest", nTest);

    }

}

Steve

Avatar

Level 6

Thanks for your response Steve.  I've marked it as correct, since I think you're right in confirming that I have not lost my mind.  It was informative to learn that the behaviour of the script object is not necessarily exactly as you would expect in a bona fide Java program, particularly where variable scope is concerned.  In the case if the behaviour that I stumbled upon, I think it's a bug and not just a difference in the way beanshell handles variable scoping.  I was able to find a work-around for the problem along the lines of what you discovered.  It makes no sense, but it works.  Thanks again.

Jared

Avatar

Level 8

Jared;

If you believe that its a bug (and I'm not saying it is or it is not), then make sure it gets filed with Adobe Support.  They can't fix it if they don't know about it.

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] ----