Jim M
Jim M

Reputation: 177

xpages Trying to get document parentdoc is returning null error

I am trying to get the parent document of a new response document so I can duplicate the functionality of form inheritance in xpages. The following is my code and the error being returned:

Error while executing JavaScript action expression
Script interpreter error, line=3, col=60: 'parentDoc' is null
JavaScript code

   1: if (document2.isNewNote()) {
   2: var parentDoc:NotesDocument = database.getDocumentByID(document2.getParentId());
   3: getComponent("immediateParentSubject1").setValue(parentDoc.getItemValueString("Subject"));
   4: }

Upvotes: 0

Views: 852

Answers (2)

Panu Haaramo
Panu Haaramo

Reputation: 2932

datasource.getParentId() does not return the NoteID as you might expect. It returns the UnID and that's why you need to use database.getDocumentByUNID as Per is doing.

Another way is to get the parent UnID from the URL:

param.get("parentId")

Consider also looking up the parent subject whenever a child is opened instead. That way it is stored only in one place which is always a good thing.

Upvotes: 3

Per Henrik Lausten
Per Henrik Lausten

Reputation: 21709

I usually use a dataContext and getParentDocumentUNID() when I need a handle on the parent document for the variable "document". You can use this for a new document (not saved yet):

<xp:this.dataContexts>
    <xp:dataContext var="parentDoc">
        <xp:this.value><![CDATA[#{javascript:
            try {
                if (document.isResponse()) {
                    return database.getDocumentByUNID(document.getDocument().getParentDocumentUNID());
                } else {
                    return "";
                }
            } catch(e) {
                return "";
        }}]]></xp:this.value>
    </xp:dataContext>
</xp:this.dataContexts>

You can then use parentDoc in other controls and do parentDoc.getItemValueString("Subject") etc.

Upvotes: 4

Related Questions