Tolbxela
Tolbxela

Reputation: 5183

How to refresh the datasource in XPages, when you change background document?

If I get the NotesDocument object from the datasource, change some fields and then save the datasource, without saving the NotesDocument directly, the changes are not saved.

For example:

var doc:NotesDocument = document1.getDocument(true);
doc.replaceItemValue("TestField", "Test");

I save the datasource with the simple action and the field "TestField" in the document is empty.

How can I update datasource with the changes in the background document?

Upvotes: 0

Views: 1098

Answers (3)

Tolbxela
Tolbxela

Reputation: 5183

I want to add Authors field to the document and I do it in the queryNewDocument event.

document1.replaceItemValue("Authors", authors);
var doc:NotesDocument = document1.getDocument(true);
    if (doc != null) {
        var Authors:NotesItem = doc.getFirstItem("Authors");
        Authors.setAuthors(true);
    }

But If I don't save the with doc.save() and only do document1.save() at the end, then the field 'Authors' stays a usual text field. And I don't want to save the doc before user clicks save button.

Is there any better way to add Authors field to a new document?

Upvotes: 0

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15729

Steve's correct, if you want to update the back-end document and reflect that on the DominoDocument wrapper datasource, you need to save.

But based on your answer, why are you trying to update the back-end document? It would seem to make more sense to update the DominoDocument instead with document1.replaceItemValue("TestField", "test").

In queryNewDocument there no back-end document exists. During document1.save(), the code will effectively call database.createDocument(), then write all fields from document1 onto that newly-created Document object. If a Document object was created earlier, you would hit the problem you're raising in your comment on Steve's answer - needing to delete the Document object if the save is cancelled.

Upvotes: 5

Steve Zavocki
Steve Zavocki

Reputation: 1840

In SSJS, you have to explicitly save your backend document. To do this simply add a line to save the document changes. I am not sure why the simple action didn't work for you, but add this line and your "Test Field" will be updated.

doc.save();

Upvotes: 2

Related Questions