Archive for August, 2007

(User) Profile documents & mandatory fields

It has been a while since I wrote here something so I will just write something I did today. Right now I am doing some prototyping for a demo-application (web) which generally will be an ordering system.

The application uses currently (user) profile documents. It was a bit hard to find code to copy & paste, because most examples where not so detailed (just words, no code) or they used actual Notes documents, not profile documents.

So how does it work? When opening the form via an url (form_name?OpenForm) a WebQueryOpen Agent is initiated which runs the following simplified code:

Sub Initialize
 Dim s As New NotesSession
 Dim db As NotesDatabase
 Dim profiledoc As NotesDocument
 Set db = s.CurrentDatabase
 Set profiledoc = db.GetProfileDocument(“UserProfile”, s.UserName) 
 Dim currentdoc As NotesDocument
 Set currentdoc = s.documentcontext 
 currentdoc.Tx_UserLanguage = profiledoc.Tx_UserLanguage(0)
 currentdoc.Tx_UserCostCenter = profiledoc.Tx_UserCostCenter(0)
 currentdoc.Tx_UserGLAccount = profiledoc.Tx_UserGLAccount(0) 
 currentdoc.Tx_UserTechName = profiledoc.Tx_UserTechName(0)
 currentdoc.Tx_UserTechPhone = profiledoc.Tx_UserTechPhone(0)
 currentdoc.Tx_UserDeliveryStation = profiledoc.Tx_UserDeliveryStation(0) 
End Sub

So basically it picks (when available) the profiledoc and copies values from it to the opened form. I could have used IsProfile to check if we actually working with a profile document…

In the WebQuerySave event I run a different code which writes away information entered in the form to the profile document:

Sub Initialize
 Dim s As New NotesSession
 Dim db As NotesDatabase
 Dim profiledoc As NotesDocument
 Set db = s.CurrentDatabase
 Set profiledoc = db.GetProfileDocument(“UserProfile”, s.UserName) 
 Dim currentdoc As NotesDocument
 Set currentdoc = s.documentcontext 
 profiledoc.Tx_UserLanguage = currentdoc.Tx_UserLanguage(0)
 profiledoc.Tx_UserCostCenter = currentdoc.Tx_UserCostCenter(0)
 profiledoc.Tx_UserGLAccount = currentdoc.Tx_UserGLAccount(0) 
 profiledoc.Tx_UserTechName = currentdoc.Tx_UserTechName(0)
 profiledoc.Tx_UserTechPhone = currentdoc.Tx_UserTechPhone(0)
 profiledoc.Tx_UserDeliveryStation = currentdoc.Tx_UserDeliveryStation(0) 
 Call profiledoc.Save( True, False )
End Sub

Later I think will use a similar functionality to write information back to the profile document, that hasn’t been entered before in the user profile, from any sensible point in the application (like in a final order form)…

The first ‘problem’ i faced was that a ‘normal’ JavaScript submit would give me a ‘form processed’ message.  Since I had a nice ‘update’ button in mind so the user should stay in the form opened I was ‘forced’ to use a common known Domino ‘work around’.

updatebuttons

The reference under the ‘Save Setting’ link inititates a JS function which actually initiates the function under a Notes button, that is being stored in a hidden <div>

function submitForm(){
 document.all.MySaveButton.onclick();
}

hidebutton

So the JS function initiates the @Command([FileSave]) function which initiates the WebQuerySave event and thereby the user profile is being updated on the background.

Since the document will be re-opened due to a refresh (don’t ask me why what forces this) the WebQueryOpen events initiates again and the saved information is being re-placed on the refreshed form…

So far so good, where is the news?

I wanted to make visible which fields are supposed to be mandatory and highlight them like in the next example:

mandatory empty

Here you have ‘normal’ fields with a gray border, and ‘normal’ entry fields that are supposed to be also ‘mandatory’. So for these fields I add both classnames.

field properties

On the JS onload event I initiate the following function:

checkISValid();

The code for this function is pretty basic and contains (in this example) no actualy validation, just a check if the field is empty or not:

function checkISValid(){
 var checkFields = new Array();
 checkFields = getElementsByClassName(document, “input”, “mandatoryField”)
 for (x in checkFields){  
  if (checkFields[x].value != “” ){
   checkFields[x].className = “entryField”; 
  }
 }
}

So if the field with classname mandatoryField is empty it will get the mandatoryField class ‘removed’ (overwritten) and it will look like this:

mandatory field entered

I think in this way it is pretty clear for the user which information is still missing…

Here is the ‘getElementsByClassName’ function borrowed from this site:

function getElementsByClassName(node,tag,searchClass) {
 var classElements = new Array();
 if ( node == null )
  node = document;
 if ( tag == null )
  tag = ‘*’;
 var els = node.getElementsByTagName(tag);
 var elsLen = els.length;
 var pattern = new RegExp(“(^|\\s)”+searchClass+”(\\s|$)”);
 for (i = 0, j = 0; i < elsLen; i++) {
  if ( pattern.test(els[i].className) ) {
   classElements[j] = els[i];
   j++;
  }
 }
 return classElements;
}

Ps. for some nice JS onFocus effect like this:

on focus effect

You could add something like this:

on focus JS code

Don’t forget to reset coloring on the onBlur event ;-)

Comments (9)