Bruce Stemplewski
Bruce Stemplewski

Reputation: 1343

Setting a document field with replaceItemValue from a rich text control?

How do you set a richText value with replaceItemValue from a rich tect control?

I found this bit of code here: http://www.bleedyellow.com/blogs/martin/entry/save_a_richtext_field_from_a_xpage_to_a_document?lang=en_us

var doc = configuratieformulieren.getDocumentByKey("ConfiguratieIVNL", true);
if(doc == null){
    return;
}else{    
    var titel = getComponent("inputTextIBPTitelIVNL").getValue();
    doc.replaceItemValue("IBPTitel",titel);    
   var inhoud = getComponent("inputRichTextIBPInhoudIVNL").getValue();
   if (inhoud != null){ 
   var contentType = doc.getMIMEEntity("IBPInhoud").getContentType();
   var encoding = doc.getMIMEEntity("IBPInhoud").getEncoding();
   var str = session.createStream();
   inhoud.toString();
   str.writeText(inhoud.toString());
   doc.getMIMEEntity("IBPInhoud").setContentFromText(str, contentType, encoding);    
 }

 doc.save(true, true);
}
sessionScope.put("FormulierIVNLInfoBeschPG","Lezen");

Is it correct? It looks like this code depends on the fact that the field already exists. How id this handled if the field does not exist? Is there and easier way to set a field value to the contents of a rich text control?

Upvotes: 0

Views: 1994

Answers (2)

Bruce Stemplewski
Bruce Stemplewski

Reputation: 1343

I was able to solve my orginal issue. To expand on my issue I was having problems with using a dialog box to create Form / Document B from Form / Document A using a dialog box on Form A. What was happening was any changes to Form B would be saved to Document A's datasource.

I found the ingoreRequestParams on Form B's datasource, set it and that solved my problem with Form B writing to Form A document.

Upvotes: 0

Tim Tripcony
Tim Tripcony

Reputation: 8086

Let data sources do the heavy lifting. For a long and boring (but thorough) explanation of why, read this article. But here's the quick version:

Don't use:

getComponent("someID").getValue()

Instead, use:

someDataSource.getValue("someFieldName")

This is always a more efficient way to access data: instead of having to spider through the component tree to locate a match, it goes straight to the data source, which the component would have to ask anyway if you asked it what its value is.

Similarly, don't use:

someDataSource.replaceItemValue("someFieldName", someValue)

Instead, use:

someDataSource.setValue("someFieldName", someValue)

The latter is much more flexible on input type. The data source already contains all the logic for determining what to do based on whether the value is text, date, number, rich text, file upload, etc. No need to duplicate any of that logic in your own code.

So if the goal is to update a separate document based on data in the current document, just define a separate document data source that points to the document you want to update. Then it's literally this simple:

configData.setValue("RichTextData", currentDocument.getValue("RichTextData"));
configData.save();

With the above code, if the field you specify on the current document is rich text, then the item it creates on the other document will be rich text. If it's any other type on the current document, it will be the same type on the other document. With getValue() and setValue(), you don't have to pay attention to the data type... the data source handles all of that for you.

For bonus points, scope configData to applicationScope so that any updates to it are immediately cached for all users... or sessionScope if the document you're updating is user-specific.

Upvotes: 3

Related Questions