Tuesday, August 16, 2011

Working with In Memory Documents in XPages

Following is a simple demonstration of how documents in memory can be used to communicate between XPages and Agents.The beauty in the following description is that the document in memory is never being saved. That is an awesome stuff that I feel is really helpful for developers in many occations

Create an XPage with a couple of input fields named "boxA" and "boxB" as shown in the following image
Now add a button and label it as you desire and add the following code to it

The code displayed in the above mentioned image does the following,
  1. it simply creates a document on the fly named "inMemoryDoc"
  2. Handle to an agent present in the backend is obtained
  3. The agent is asked to be executed with the inMemoryDoc created in the first step as documentcontext
  4. The inMemory document gets updated (not saved) in the backend and the updated values are asked to be displayed on the UI using the getComponent("").setValue() methods.

The code fragment in the agent that resides on the backend is as follows

So as a result when you preview the XPage on the browser and hit the button the out put will be as follows

If you get any errors ensure that the credentials mentioned in the following post are met,
http://ozinisle.blogspot.com/2011/02/agentrunwithdocumentcontextnotesdocumen.html

Hope this helps :)

"onbeforeunload" An Illustration/Demo

I recently happened to work with a stuff that required javascript operations to be performed, when a user navigates away from a page or closes it on a browser.

So I ended up discovering the presence of this event "onbeforeunload" for html pages. Though this seems to have some issues with the Opera browser, It seems to work fine with IE, Firefox and Chrome. Haven't tested on safari. The code that helps me do it is quite fascinating as well.

A function named confirmExit() exists that just works like the confirm prompt, which provides with default options asking your opinion on leaving or staying on the same page. The code is as follows

<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>

Following is a  screen shot of how these pages look on various browsers














 
Hope this helps :)



Wednesday, August 3, 2011

Force UNID on a new document - UNID Hack

Recently I happened to do some thing that required me to force unid on a newly created document. The use cases that I could think about for this kind of scenario are very use ful. Say for example deleting a document by accident may lead to orphan response documents if not taken care of. And if you want to bring them back on association and not screw up things we can use this little tip.

The use is very simple,Following is a lotus script code that will do it for you. Note the way document items are being copied using CopyAllItems command.

Set selDoc=currDB.UnprocessedDocuments.GetFirstDocument

Set newDoc=trgDB.Createdocument
newDoc.UniversalID=selDoc.UniversalID
Call selDoc.CopyAllItems(newDoc)
call newDoc.save(true,false)
 
And the normal rules apply, a multiple documents can't have same unid with in the same database.
 
Excersice caution while using this cos, you might just screw up things if you are not cautios. After all we know the importance of the UNID