Patrick Sawyer
Patrick Sawyer

Reputation: 1382

How to implement a "Save as New" feature

I am looking to implement a "Save as new" button. This would essentially take an existing document and create a new copy of the document with all of the existing fields. This would need to be implemented from a document and not a viewPanel.

Upvotes: 0

Views: 162

Answers (4)

Chris Toohey
Chris Toohey

Reputation: 146

  1. Pass your NotesDocument or NotesXspDocument (sourceDoc) in your function call or otherwise define it based on your particular need.
  2. Create a new NotesDocument (newDoc) in the target notesDatabase.
  3. Use sourceDoc.copyAllItems() into the newDoc.
  4. Save and - if desired - redirect to the newDoc.

I have a "services" feature in my CMS that allows the author to create a duplicate of the "service" with the following SSJS:

var service = {
...
  duplicate : function(thisDoc:NotesXspDocument) {
    try {
        var newDoc:NotesDocument = database.createDocument();
        thisDoc.getDocument().copyAllItems(newDoc, true);
        var newId = newDoc.getItemValueString("id") + "-" + session.evaluate("@Unique").elementAt(0) + "-DUPLICATE";
        newDoc.replaceItemValue("id", newId);
        newDoc.save(true);

        context.redirectToPage("/service.xsp?id=" + newId + "&");

    } catch(e) {
        print(database.getTitle() + " SSJS Error for service.duplicate()");
        print(e.toString());
    }
  }
}

Now, I read the IDs in my dataSources, which is why I'm passing through the newId value. If you're using the out-of-the-box dataSource setup, you'll want to change that to getUniversalID() and set the URL Parameters to "?action=openDocument&documentId=".

[edit]- Here is the script that will work for a standard data source

var newDoc:NotesDocument = database.createDocument();
    currentDocument.getDocument().copyAllItems(newDoc, true);

    newDoc.save(true);
    var newId = newDoc.getUniversalID();
    context.redirectToPage("/entry.xsp/%24%24OpenDominoDocument.xsp?     documentId="+@Text(newId)+"&action=editDocument");

[edit] (Chris Toohey): I changed "document1" to "currentDocument" to better support the relative local/current NotesDocument dataSource (as someone could change the default dataSource name from "document1" and the suggested edited code wouldn't work).

Upvotes: 3

andora
andora

Reputation: 1356

Not sure if this would work under XPages, but if the document contains a '$VerOpt' notesItem (field name) with a valid setting in it (eg: "2") the database may create a new copy for you. You will have to arrange to set/clear the value as needed. If you wish to create the 'new version' on every save, the form has a default properties has a setting you can set. Search for this reserved fieldname for details.

Upvotes: 0

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15739

NotesDatabase.createDocument() and NotesDocument.copyAllItems(destDoc) should provide the functionality (or the corresponding Java equivalents). It's better performing than NotesDocument.copyToDatabase()

Upvotes: 2

Related Questions