Bill F
Bill F

Reputation: 2087

dealing with a null date field

I have a date field on an XPage, this control may contain a date or be blank. In a repeat control I have this code:

var doc:NotesDocument = detailData.getDocument();
var sDate = doc.getItemValue("ACAutoStart");
doc.recycle()
return "Start Date = " + sDate

If ACAutoStart contains a date then it is displayed as [10/10/2013 12:34:15 AM MDT] if it is blank it displays as []. As I understand it the [] indicates that the result is an array but if I try using sDate[0] there is an error. I can't use getItemValueDateTime as it does not like the null return. How do I get this into a simple string value?

Upvotes: 0

Views: 723

Answers (2)

AndrewG10i
AndrewG10i

Reputation: 681

...just as another way (returns converted NotesDateTime to Date):

function getJavaDateData(doc:NotesDocument, field:string)
{
var item:NotesItem = doc.getFirstItem(field);

if (item != null){
    var dt:NotesDateTime = item.getDateTimeValue();
    if (dt != null){
        return dt.toJavaDate();
    }       
}

return null;
}

Off-course need to be adapted for your logic...

Upvotes: 0

Knut Herrmann
Knut Herrmann

Reputation: 30970

Replace your last line with return "Start Date = " + sDate.firstElement().

doc.getItemValue() returns an object of class java.util.Vector. As it is not an Array you get the first element with firstElement() instead of [0] .

UPDATE:

As you mentioned in your comment it has to work also for empty values and you added try:

    var sDate = "";
    try {sDate = doc.getItemValue("ACAutoStart").firstElement()} catch (e) {};
    return "Start Date = " + sDate

Upvotes: 3

Related Questions