Cumhur Ata
Cumhur Ata

Reputation: 809

XPages function in SSJS Library does not return as document

I have created 2 functions. I call "findTitleNew" from "createNewOne". I reach a document in "createNewOne" function but when i return to function "findTitleNew" I lost the document that was found in "findTitleNew" How to continue without losing that document? NOTE: This functions are generic because I use those functions more than once in applications.

<xp:button value="Create" id="btnCreate">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete" immediate="false" save="true">
                <xp:this.action><![CDATA[#{javascript:createNewDoc(document1)}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>


function findTitleNew(currDoc:NotesXSPDocument)
{
    try
    {
        var dbOther1:NotesDatabase = session.getDatabase(database.getServer(),sessionScope.kontak_db_Path);
        if (currDoc.getItemValueString("UNID")!="")
        {
            var otherDoc:NotesDocument = dbOther1.getDocumentByUNID(currDoc.getItemValueString("UNID"))
        }
    }
    catch (e) 
    {
        requestScope.status = e.toString();
    }
}

function createNewOne(docThis:NotesXSPDocument)
{
    try
    {
        //do stafff
        findTitleNew(docThis)
        //do stafff
    }

    catch (e) 
    {
        requestScope.status = e.toString();
    }
}

Any suggestion is appreciated.
Cumhur Ata

Upvotes: 0

Views: 180

Answers (2)

Knut Herrmann
Knut Herrmann

Reputation: 30960

It's about the scope of your variable otherDoc.

You defined the variable as var otherDoc. The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.
var otherDoc is defined within a function so it "lives" only within the function. This means that otherDoc is not available outside the function.

You could assign a value to otherDoc without declaring it. In this case it would be globally available. But this is not recommended because code can get pretty messy.

The best way is to return the variable with return otherDoc like David shows in his answer.

Upvotes: 0

David Leedy
David Leedy

Reputation: 3593

My SSJS is really rusty and it's a little hard for me to tell exactly what you want BUT you say : "I lost the document that was found in "findTitleNew" How to continue without losing that document? "

your function "findTitleNew" doesn't return anything. So if you get a document there you can work with it, but if you want to do move in the "createNewOne()" function you need to return the found document

 if (currDoc.getItemValueString("UNID")!="")
        {
            var otherDoc:NotesDocument = dbOther1.getDocumentByUNID(currDoc.getItemValueString("UNID"))
return otherDoc;
        }

Then :

function createNewOne(docThis:NotesXSPDocument)
{
    try
    {
        //do stafff
        var returnDoc = findTitleNew(docThis);
        if (null != returnDoc) {
            // do stuff with returnDoc here...
        }
        //do stafff
    }

    catch (e) 
    {
        requestScope.status = e.toString();
    }
}

Upvotes: 1

Related Questions