Sep 1, 2011, 2:23 PM
66 Posts
topic has been resolvedResolved

Update multiple documents in view by key(s)

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags:
  • Replies: 6
I have a button on a XPage that needs to update a field and save the document(s) based on the key number(s) associated with the user. The first column of the view I'm working with is sorted by a key number (e.g. 1234, 4321, etc.). How can I use SSJS to loop through the key numbers and save the backend Notes document(s)?
 
 What I have right now
var db:NotesDatabase = sessionAsSigner.getDatabase("","Database.NSF");
var view:NotesView = db.getView("ViewName");

var keyV = new java.util.Vector();
keyV.addElement("1234");
var LookupDoc:NotesDocument = view.getDocumentByKey(keyV);
 
LookupDoc.replaceItemValue("BackendField", newValue);
LookupDoc.save();
 
What I need to do is replace the "1234" with an array/vector of key(s) and have that update and save each document. Is this possible?
 
Any help is appreciated. 
Sep 1, 2011, 3:40 PM
272 Posts
Re: Update multiple documents in view by key(s)
Hi,
 
please have a look at the stampMulti-method:
 
http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp?topic=/com.ibm.designer.domino.main.doc/H_STAMPALLMULTI_METHOD_DOCCOL.html
 
To get a collection, you can use the getAllDocumentsByKey method.
 
Sven
Sep 1, 2011, 4:26 PM
66 Posts
Re: Update multiple documents in view by key(s)
Thanks for the reply Sven...I think I'm close...I changed the following in the code below and if I have just key it works. If I try to add more than one key/element (commented out below) the backend document doesn't get updated/saved on either. Any ideas?
 
var db:NotesDatabase = sessionAsSigner.getDatabase("","Database.NSF");
var view:NotesView = db.getView("ViewName");

var keyV = new java.util.Vector();
keyV.addElement("1234");
//keyV.addElement("4321");
var LookupDoc:NotesDocument = view.getAllDocumentsByKey(keyV);
 
LookupDoc.stampAll("BackendField", newValue);
Sep 1, 2011, 4:44 PM
291 Posts
you got some code issues here
see this color, I am also assuming your LookupDoc is not getting set, put in a if statement or debug print statements to test this. I am not sure using a vector of more than one object will work like you think. A vector is used to search in multiple sorted columns. IE, the first element searches the first sorted column and then the second element searches the next sorted column for that row. It will not get a collection of all documents that match the Vector's elements in the first sorted column. From Help: Finds documents based on their column values within a view. You create a key or vector of keys, where each key corresponds to a value in a sorted column in the view. The method returns all documents whose column values match the keys. Defined in View Syntax public DocumentCollection getAllDocumentsByKey(java.util.Vector keys) throws NotesException public DocumentCollection getAllDocumentsByKey(Object key) throws NotesException public DocumentCollection getAllDocumentsByKey(java.util.Vector keys, boolean exact) throws NotesException public DocumentCollection getAllDocumentsByKey(Object key, boolean exact) throws NotesException Parameters java.util.Vector keys String, Number, DateTime, or DateRange objects that are compared to sorted columns in the view. The first element in the vector is compared to the first sorted column in the view; the second element is compared to the second sorted column; and so on. Also, see my comments in your code below. var db:NotesDatabase = sessionAsSigner.getDatabase("","Database.NSF"); var view:NotesView = db.getView("ViewName"); var keyV = new java.util.Vector(); keyV.addElement("1234"); //keyV.addElement("4321"); //the next line returns a NotesDocumentCollection object not a document var LookupDoc:NotesDocument = view.getAllDocumentsByKey(keyV);  //stamp all only works on collections not a NotesDocument class LookupDoc.stampAll("BackendField", newValue); Howard
Sep 2, 2011, 8:41 AM
66 Posts
Re: Update multiple documents in view by key(s)
Is there a method I can use to get a collection of all documents that match an array/vector's elements in just the first sorted column only?
Sep 2, 2011, 9:52 AM
272 Posts
Re: Update multiple documents in view by key(s)
No. But you could change the search in the first column to solve this.
 
For example
 
var keyV = new java.util.Vector();
keyV.addElement("1234");
keyV.addElement("4321");

keyV.toArray().join("#");
 
then you could use this as the lookup value, and the first column has to be changed.
 
 
Otherwise you could use th FTSearch method to seek the docs you want to change.
 
 
Sven
 
Sep 2, 2011, 1:43 PM
66 Posts
Re: Update multiple documents in view by key(s)
 Thanks for the help! I got it to work by using this:
 
var keys = new java.util.Vector();
keys.addElement("5314");
keys.addElement("8698");
 
                //Loop through documents and update with newValue
for ( var i=0; i<keys.length; i++ ){
var dcColl:NotesDocumentCollection = view.getAllDocumentsByKey(keys[i], true);
dcColl.stampAll("BackendField", newValue);
}
 
Now all I need is to populate the "keys" vector with an array of sub-master keys. The first column of the view is categorized by a "Master" key. I need to get all the "Sub-master" keys associated with the master key I pass in. Would the getColumnValue() method be best for this?
 
View: 
Master       Sub-master
1234
                   4321 
                   5678
 
Update: Just ended up using a DbLookup. Thanks for all the help.