AddEntry to ViewEntryCollection through java code

Hi,

Have been struggling with this issue for a while now and am not sure what I am doing wrong.

I would like to add a document to a view, through java agent code.

The following is my code:

Session session = getSession();

AgentContext agentContext = session.getAgentContext();

Database db = agentContext.getCurrentDatabase();

View tView = db.getView(“blah”);

ViewEntryCollection vecSPSS = tView.getAllEntries();

System.out.println("Number of entries before= " +vecSPSS.getCount());

//create new document

Document d = db.createDocument();

d.replaceItemValue(“beach”,“true”);

if(d.save(true, true))

{

System.out.println("the doc is saved");

System.out.println();

}

//add document to the viewentrycollection

vecSPSS.addEntry(d);

//try to retrieve entry

ViewEntry entry;

entry = vecSPSS.getFirstEntry();

System.out.println(“Parent view:” + vecSPSS.getParent());

System.out.println("Number of entries after = " + vecSPSS.getCount());

The number of entries in the collection before and after the document has been added as an entry is 0.

I have a view called blah, and a column in the view called beach, which has an underlying field called beach on a document. I am trying to create a document and populate that field called beach. But no document is getting saved in the view.

Please help! Am not sure what am doing wrong.

Many thanks,

Marise

Subject: AddEntry to ViewEntryCollection through java code

The output from the code below is as follows:

Number of entries before = 8

the doc is saved

Parent view: All by FldAlpha

Number of entries after = 9

public void NotesMain() {

Session session = null;

try {

session = super.getSession();

AgentContext agentContext = session.getAgentContext();

Database db = agentContext.getCurrentDatabase();

View tView = db.getView(VIEW_NAME);

ViewEntryCollection vecSPSS = tView.getAllEntries();

System.out.println("Number of entries before = " + vecSPSS.getCount());

// create new document

Document d = db.createDocument();

d.replaceItemValue(“form”,“TestForm”);

if (d.save(true, true)) {

System.out.println(“the doc is saved”);

tView.refresh(); // refresh the view (and the ViewEntryCollection)

}

//add document to the viewentrycollection

vecSPSS.addEntry(d);

//try to retrieve entry

ViewEntry entry;

entry = vecSPSS.getFirstEntry();

System.out.println("Parent view: " + vecSPSS.getParent().getName());

System.out.println("Number of entries after = " + vecSPSS.getCount());

} catch (Throwable th) {

th.printStackTrace();

} finally {

try {

if (session != null) {

session.recycle();

}

} catch (NotesException ne) {

}

}

}

Subject: RE: AddEntry to ViewEntryCollection through java code

Dear Dallas,

Many thanks for your reply. I tried out the code you posted and it does increment the number of items in the view.

However, when the output is printed, the same numbers before and after the entry is added are indicated. Am not sure why this is the case. I tried to troubleshoot, but have not been successful.

Also, can someone please explain how ‘replaceitemvalue’ works? I created a field on a form, and called it ‘input’, i tried to replace the field with a value through the agent code previously. The count indicates that documents are being added to the view, but I cannot see the items that I am replacing, as I try to pull the field ‘input’ as one of the columns in the view.

Many thanks,

Marise

Subject: RE: AddEntry to ViewEntryCollection through java code

Hmmm . . . works for me in my 7.02 client.

You are doing this from within the Notes client, right? In other words, you’re not writing it in Eclipse or some other IDE and testing it there, are you? You can try explicitly calling the setAutoUpdate() method on the View with a parameter of true, but I believe that’s the default behavior from within Notes. It’s not good for performance but may be necessary.

The replaceItemValue() method returns an Item . . . it works. See Designer help for more information.

public class JavaAgent extends AgentBase {

private static final String VIEW_NAME = “Test View”;

public void NotesMain() {

Session session = null;

try {

session = super.getSession();

AgentContext agentContext = session.getAgentContext();

Database db = agentContext.getCurrentDatabase();

View tView = db.getView(VIEW_NAME);

tView.setAutoUpdate(true); // force view to update

ViewEntryCollection vecSPSS = tView.getAllEntries();

System.out.println("DEBUG: Number of entries before = " + vecSPSS.getCount());

// create new document

Document doc = db.createDocument();

doc.replaceItemValue(“form”,“TestForm”);

if (doc.save(true, true)) {

System.out.println(“DEBUG: The doc is saved”);

tView.refresh(); // refresh the view (and the ViewEntryCollection)

}

//add document to the viewentrycollection

vecSPSS.addEntry(doc);

//try to retrieve entry

ViewEntry entry;

entry = vecSPSS.getFirstEntry();

System.out.println("DEBUG: Parent view: " + vecSPSS.getParent().getName());

System.out.println("DEBUG: Number of entries after = " + vecSPSS.getCount());

} catch (Throwable th) {

th.printStackTrace();

} finally {

try {

session.recycle();

} catch (Exception e) {

}

}

}

}