Gerard Cruz
Gerard Cruz

Reputation: 649

Is it possible to display text on a rich text item in a DocumentContext?

I have a form that runs a java agent on the WebQueryOpen event. This agent pulls data from a DB2 database and then puts them into the computed text fields I have placed on the form and are displayed whenever I open the form in the browser. This is working for me. However, when I try to use RichTextFields I get a ClassCastException error. No document is actually saved, I just open the form in the browser using this domino URL - https://company.com/database.nsf/sampleform?OpenForm

Sample code of simple text field - Displayed with w/o problems

Document sampledoc = agentContext.getDocumentContext();
String samplestr = "sample data from db2";
sampledoc.replaceItemValue("sampletextfield", samplestr);

When I tried using rich text field

Document sampledoc = agentContext.getDocumentContext();
String samplestr = "sample data from db2";
RichTextItem rtsample = (RichTextItem)sampledoc.getFirstItem('samplerichtextfield');
rtsample.appendText(samplestr); // ClassCastException error

Basically, I wanted to use rich text field so that it could accommodate more characters in case I pull a very long string data.

Screenshot of the field (As you can see it's a RichText)

Screenshot of the field (As you can see it's a RichText)

Upvotes: 1

Views: 923

Answers (1)

Mario S
Mario S

Reputation: 1984

The problem is that you're trying to access a regular Item as a RichTextItem.

The RichTextItem are special fields that are created with its own method just like this:

RichTextItem rtsample = (RichTextItem)sampledoc.createRichTextItem('samplerichtextfield');      

It's different to the regular Items that can be created with a simple sampledoc.replaceItemValue(etc).

So, if you want to know if a item is RichTextItem and if it does not exist, create it, you can do this:

RichTextItem rti = null;
Item item = doc.getFirstItem("somefield");
if (item != null) {
    if (item instanceof RichTextItem) {
        //Yay!
        rti = (RichTextItem) item;
    } else {
        //:-(
    }
} else {
        rti = doc.createRichTextItem("somefield");
        //etc.
}

Upvotes: 1

Related Questions