Do you know LotusScript. I often found that writing out what you want to do in Lotuscript is helpful to then translate it to SSJS. Since it is similar when you're working with the Domino Object Model.
First I guess I should ask... is this solely an xpages application? or is the notes client involved. I'm going to assume that the notes client is involved otherwise I'm not sure why a true doclink is needed rather then manually building a url link. If it's a pure Web application then you probably don't want to use RichText at all, The web is all about MIME I THINK.
I've not done a lot with Rich text in XPages, and certainly never tried creating a doclink. Though this should be doable I guess but I'd have to try it. Instead of doing that as I don't have my designer handy, I'll through out some ideas and discussions points...
I'm not sure where you're calling this from.... Is this a button? is it happening on querySave? This code looks a fair bit different the the first one you posted.
I'm not sure why this is a function. Don't get me wrong I like functions, but you're not passing anything in, and there's in issue with what you're attempting to pass back.
Here's the function you pasted with line numbers:
1: function RThelper () {
2: var docRT:NotesDocument = database.createDocument();
3: var rtitem:NotesRichTextItem = docRT.createRichTextItem("TEMP");
4: sessionScope.info = rtitem.appendDocLink(sessionScope.docLink, "LINK");
5: docRT.save();
6: return sessionScope.info;
I think one if your big issues is understanding scoped variables better. Typically you never, NEVER put a domino object like a "doc". or "item" or anything into a scoped variable. They're just not for that. You seem to really want to do that so I'd focus there. scoped variables are for strings, and numbers, and even full blown java objects but NOT domino objects.
Line 1. Nothing wrong here... but functions should be reusable typically. So it COULD look something like this:
function RThelper (doc1:NotesDocument, doc2:NotesDocument) {
so MAYBE you want to just pass both documents into the function for it to do the work. you can get a handle on the documents if they are either bound to the xpage or the code that calls the function gets it via the domino object model... just like in lotusscript... "getFirstDocument()"... whatever...
Line 2... you've made a new document from the global database object. nothing wrong there really.
Line 3... still good... you now make a var for the rtitem
Line 4... ok. here it goes off track a bit I think... this assumes you have a docLink already in scope... that's looking for an actual notesdocument though and you don't want to put that into scope. In lotusScript the lines might be something like this:
Call rtitem.AppendDocLink( db, db.Title )
in SSJS MAYBE... and I'm guessing here but Maybe it should look like this:
rtitem.appenddocLink(doc2, "LINK")
so no need to assign that to a variable and certainly no need to try and assign that to something in scope.... that will just ruin your day. :)
Line 5... that looks good. that's how you save a doc called docRT... though sometimes you need something like docRT.save(true, true) or whatever... HOWEVER, this goes back to where you're calling this code from... since you're saving it here you want to make sure you're not creating a rep conflict or something if you're actually calling it from a save event...
Line 6. this is unneeded... there really isn't anything to return so it could be just "return" with nothing else.... you never want to return something from sessionScope I don't think because you can always access sessionScope from any code blog.... it's just available.. so once sessionScope gets set, any where later I can access it... I THINK you're meaning for "info" to be a notesDocument? I'm not sure.
So let's look at this in pseudo code... let's say we have a simple button and you're using ssjs onclick... MAYBE what you would do is something like this...
<xp:button>
// inside the click event
// assume that document1 is currently bound to the page and available globally.
// So you need to get the document you want to link to - maybe you have the UNID of that document... UNIDS go into scope nicely
linkDoc:NotesDocument = database.getDocumentByUniversalID(sessionScope.get("linkUNID"));
// so in theory we have the current doc and the doc to append to... BUT current Doc is NOT a notesdocument but a notesXSPDocument
var workingDoc:NotesDocument = document1.getdocument() // not sure if true is needed or not
// so now workingDoc is an actual notesDocument and not a NotesXSPDocument. NotesXSPDocument is like a wrapper for convenience...
var rtitem:NotesRichTextItem = workingDoc.createRichTextItem("body") // or whatever the fieldname is...
rtitem.appendDocLink(linkDoc, "LINK")
workingDoc.save(true, true)
</xp:button>
Anyway - sorry I couldn't give you a definitive answer but hopefully there is value in the explanation. Just keep in mind that I don't know what you're trying to do... I'm not an expert... so I could be wrong in some of my assumptions.
Dave
There's a relevant question on StackOverFlow...
http://stackoverflow.com/questions/9332837/getting-an-error-message-when-trying-to-appenddoclink-is-ssjs