Search code examples
xpagesssjs

Checking what a value is in SSJS


I have this block of code in SSJS that I'm doing some field validation stuff: thisDoc is a NoteXspDocument fld = the name of a field

var thisValue = thisDoc.getValue(fld);
print("Check Text = " + thisValue);
print("Is is a Date " + (thisValue === Date))

when I run it the log has this:

Check Text = 09/10/15 12:00 PM
Is is a Date false

In this code I do not know what the datatype is of the fld which is a field name. I check the backend document and get the NotesItem.Type() and this field is of type text 1280 in the backend, but the NotesXspDocument has a date. I need to determine what the data type is thisValue sure acts like a NotesDateTime object, but I'm doing something wrong somewhere. I think the issue might be the difference between a NotesDateTime and a java.util.Date but they drive me up the wall.

Further Edit -- The problem is that I have an Array of field names var Fields:Array that I then loop through and get fld = Fields[n] so when I get the value of the field it could be anything Text, Date, Number so when I do var thisValue = thisDoc.getValue(fld) or thisDoc.getItemValue(fld) I need to figure out what kind of value I have. I guess I could put the getItem..... inside a try until I find one that works but that seems like a less than optimum solution.


Solution

  • Because the field that I am retrieving can be just about anything I use

    var thisValue = thisdoc.getValue(fld);
    

    i had a lot of trouble then determining what kind of data I had. It could be a null Date/Number/String So the first thing I did was find out what the backend data type was:

    var thisItem:NotesItem = thisDoc.getDocument().getFirstItem(fld);
    var type:Integer = thisItem.getType()
    

    This helps somewhat if the field has been previously set, but if it is a new document or the field has not received a value yet it will be type 1280 or text and probably null. So my fisrt test is for null or "". then it becomes a bit tougher because I need to test for some values. In all my comboboxs I add the text "--- Select ?????" as the first item in the list so I tried to get a substring of "---" but because of variance in the datatype I needed to put that in a try:

    try{
    if (thisValue.substring(0,3) == "---"){
    print("have null Prefix");
    rtn = false;
    errMsg.push("The field " + fld + " is a Required Field please enter a value");
    break;
    }catch(e){ etc
    

    I then wrapped the various other datatype tests in trys and now I have it working. Might be a better way but this works.