1
votes

I have a notes document that has two attachments. attachFile and attachFile_1. After I upload an pdf to attachFile_1 I want to delete that item if it fails a manual verification by the user. I have it such that the item does delete but the document still retains the $FILE property when I open the document properties.

See code below.

Xpages Code

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.resources>
        <xp:script src="/scriptsFile.jss" clientSide="false"></xp:script>
    </xp:this.resources>
    <xp:panel id="pnlDocData">
        <xp:this.data>
            <xp:dominoDocument var="document1" formName="frmA" action="openDocument"
             documentId="#{javascript:sessionScope.id}" scope="request">
            </xp:dominoDocument>

            <xp:table style="width:99%">
                <xp:tr>
                    <xp:td align="center" valign="middle">
                        <xp:label id="label1" value="Verify Client Submission"></xp:label>
                    </xp:td>
                    <xp:td>
                        <xp:fileDownload rows="30" id="fileDownload1" displayLastModified="false" value="#{document1.attachFile_1}">
                        </xp:fileDownload>
                    </xp:td>
                    <xp:td>
                        <xp:button value="Decline" id="button">
                            <xp:eventHandler event="onclick" submit="true" refreshMode="complete" id="eventHandler7">
                                <xp:this.action><![CDATA[#{javascript:decline()}]]></xp:this.action>
                            </xp:eventHandler>
                        </xp:button>
                    </xp:td>    
                </xp:tr>
          </xp:table>
    </xp:panel>
</xp:view>

Javascript File

function decline()
{
    var doc:NotesDocument = document1.getDocument();
    doc.replaceItemValue("attachFile_1", null);
    var item:NotesItem = doc.getFirstItem("attachFile_1");
    item.remove();
    doc.save();
}

When I run the code below I'm still getting an attachment size of 2 when I should get a size of 1.

var doc:NotesDocument = database.getDocumentByUNID(sessionScope.id);
var attachments:java.util.Vector = session.evaluate("@AttachmentNames", doc);
print(attachments.size());
2

2 Answers

0
votes

In order to delete an attachment properly, you need to use the Remove method of the NotesEmbeddedObject class. You can get this through the EmbeddedObjects property of the NotesRichTextItem class, but there are some cases that can pop-up where the attachment isn't in a rich text field but it is still in the document. In fact, it appears that you are creating these cases with your current code! To delete the attachment properlin in a document where this is already the case, you need to get your handle to the NotesEmbeddedObject using the NotesDocument.EmbeddedObjects property.

0
votes

Removing the RichTextItem attachFile_1 does not remove the attachement itself. If you only remove the rich text field the attachment will be attached to the document itself; therefore you still have 2 attachments. To properly remove the rich text field you have first to remove the attachment via the EmbeddedObjects properties of the RichTextItem class. After successfully removing the attachment you can remove the notes item.