1
votes

I had declared and used a global variable in ssjs library as below:

var backendDoc:NotesDocument = null;

function savedata () {
  print (backendDoc.getItemValueString("fieldname"));   // crash here  
}

I assigned a document object to it in the Edit button just after changing docuemnt mode from read to edit:

backendDoc = document1.getDocument(); // get backend document from datasource called document1

The code in above function return error NotesDocument.getItemValueString("string")) null. Apparently, the backendDoc is null.

Any ideas how to assign value and use global variable in ssjs library? Thanks in advance

2

2 Answers

5
votes

There are 2 problems with your code:

  • as Michael pointed out: you should use a scoped variable. Global variables in script libraries are actually application global (think applicationScope) and might be unloaded any time if memory gets tight (behavior of them depends on the XPages version)

  • You can't use NotesObjects here. Between the calls the C Object that backs the JS object is released and your object becomes invalid.

You can either store the NoteId in a scoped variable and retrieve the NotesDocument every time or actually use a JSON structure to keep the values you are interested in and only read/write when actually needed (load/save event). Hope this helps

1
votes

I think you have to use a scoped variable in which you store the universalid of the document. This can then be used at any script to initialize the backend document.

From a ssjs you can set a scoped variable using the put method and the get method to read the variable. Example to set and read a scoped variable in session scope :

sessionScope.put(“myvar“,“myvalue“)
sessionScope.get(“myvar“)

To learn more about scoped variables watch this http://notesin9.com/index.php/2009/11/07/episode-4-intro-to-scoped-variables/