0
votes

I have an xPages application that use a global variable in javascript. The global variable is a NotesDocument. When the xPage loads, it calls some Java code which populates the global Javascript var.

A bit later I extract some information from that global var and all is well. A bit later in the code I run a function from a Javascript library that tries to access that global var and it's null. I'm not sure why. Nothing else updates that global var.

Can someone point me in a direction to figure this out?

    //This is the script library function that throws the null error:
    var revoPayments = 
    {
        //  Calculate if the occupant should see the Revo Pay My Bill Link in the occ navigator
        "renderPayMyBillRevo" : function() 
        {
            var result = false;
            try 
            {
                //  THIS IS WHERE THE GLOBAL VAR IS NULL
                if( parseInt( gUserOccupantProfile.getShowRevoLink() ) == -1 )
                    result = true;
            } catch (e) 
            {
                print(e.message);
            }
            print( "result: " + result );
            return result;
        }
    }
    //This is the calling javascript from the xPage which works fine pulling all the values etc.
    if( gUserOccupantProfile && gUserOccupantProfile !== "null" && gUserOccupantProfile !== "undefined" )
    {
        //  ALL THESE VALUES ARE SET BECAUSE IT'S A VALID OBJECT
        sessionScope.put( "OccupantUNID", gUserOccupantProfile.getDocUNID());
        sessionScope.put( "Tenant_ID", gUserOccupantProfile.gettenant_id() );
        sessionScope.put( "UnitRefNo", gUserOccupantProfile.getUnitRefNo() );
        sessionScope.put( "Resident", gUserOccupantProfile.getResident() );
        sessionScope.put( "FirstName", gUserOccupantProfile.getFirstName() );
        sessionScope.put( "LastName", gUserOccupantProfile.getLastName() );
        sessionScope.put( "CurrentUser", gUserOccupantProfile.getFirstName() + " " + gUserOccupantProfile.getLastName() );
        sessionScope.put( "PropertyAddress", gUserOccupantProfile.getPropertyAddress() );
        var tempStr = gUserOccupantProfile.getcurrent_balance();
        tempStr = tempStr.replace( "$", "" );
        tempStr = tempStr.replace( ",", "" );
        sessionScope.put( "Current_Balance", tempStr );
        sessionScope.put( "PropertyNo", gUserOccupantProfile.getPropertyNo() );
        sessionScope.put( "PropertyName", gUserOccupantProfile.getPropertyName() );
        sessionScope.put( "LastPaymentDate", gUserOccupantProfile.getLast_payment_date() );
        sessionScope.put( "LastPaymentAmount", gUserOccupantProfile.getPayment_amount() );

        gAPropertyProfile = eStarService.getPropertyProfile( sessionScope.get( "PropertyNo" ) );
        //  09.15.2014 - Steven Rieger  :  added code to disable pay my bill for separate properties
        //  Default is to always display the option.
        if( gAPropertyProfile.getDisablePayMyBill().toLowerCase() == "yes" )
            sessionScope.put( "renderPayMyBill", false );
        else
            sessionScope.put( "renderPayMyBill", true );

        sessionScope.put( "renderStatements", myEStatements.renderStatements() );
        print( "Before RenderRevo" );
//  THIS LINE FAILS BECAUSE IN THE SCRIPT LIBRARY CODE ( SEE ABOVE ) THE 
//  GLOBAL VAR IS NULL
        sessionScope.put( "renderPayMyBillRevo", revoPayments.renderPayMyBillRevo() );
        print( "After RenderRevo" );

    //  sessionScope.put( "dialogOopsTitle", "Debug!" );
    //  sessionScope.put( "dialogOopsMessage", "gotLogEntry: " + gotLogEntry );
    //  var dialogOops = getComponent( "dialogOops" );
    //  dialogOops.show();

    }
1
Quick note: you probably want to change gUserOccupantProfile !== "null" && gUserOccupantProfile !== "undefined" to gUserOccupantProfile !== null && gUserOccupantProfile !== undefined.Mike Cluck
gUserOccupantProfile is the global defined at the top of my script library ( not shown in the code )Bitwyse1
I read something on line those two would catch issues if they were string values "null" or "undefined" and if( gUserOccupantProfile ) would catch null or undefined objects?Bitwyse1
Only if you compare against the actual null and undefined values. Comparing against the strings won't help. That would be true if you did this: gUserOccupantProfile = "null", not if you did this: gUserOccupantProfile = null.Mike Cluck

1 Answers

4
votes

I'm pretty sure you can't do this do to Serialization. You can't have have global SSJS variable that includes method functions with code. In can include functions with just "static" values I believe. Suggest you look at this post: http://www.notesin9.com/2014/05/20/tim-explains-ssjs-object-persistence/
And focus on what Tim Tripcony answered.

I should add that the BEST solution is to move this code to a Java Managed bean. An alternative SSJS solution might be to instead of putting all those values in different scoped Variables... create a HashMap and use that to replace all the Scoped Variables. then that whole map could be passed to a SSJS function in a script library to inspect the map and spit out the answer you're looking for. Just thinking out loud there...