1

I have a date field in a notes document and need to read the value using ssjs. if the field contains a value I can read it using

doc.getItemValueDateTimeArray[0]

but if the date field is empty the code will return an error.

so my question is how do I read a date field correctly using SSJS so that it does not return an error if it is empty and return a date value if there is a date value in the field

I am hoping for a solution without the need of a try/catch.

Thanks Thomas

Thomas Adrian
  • 3,543
  • 6
  • 32
  • 62

4 Answers4

3

Try something like this

var item:NotesItem =doc.getFirstItem("ItemName");
if (item== null) {
    dateVar=now;
} else {
    dateVar=doc.getItemValueDateTimeArray[0];
}
adminfd
  • 1,006
  • 7
  • 14
3

If you know the Item only contains a single DateTime, never multiple values and never DateRanges, adminfd's answer should work.

Otherwise it's more complex, as you will see from looking at Document.getItemValueDateTimeArray() in OpenNTF Domino API. The code (albeit Java) from M5 is appended below.

Bear in mind also that recycling doc or item will not recycle the resulting DateTime object. If you're getting DateTimes in a loop, you'll need to use validDominoBase.recycle(dateVar), where validDominoBase is any Domino object that has not yet been recycled.

@Override
    public Vector<org.openntf.domino.Base<?>> getItemValueDateTimeArray(final String name) {        // cf. DateRange.java
        checkMimeOpen();
        boolean mayBeMime = true;
        Vector<org.openntf.domino.Base<?>> vGIV = null; // see below
        try {
            Vector<?> v = getDelegate().getItemValueDateTimeArray(name);
            mayBeMime = false;
            if (v == null || v.size() == 0)
                return (Vector<org.openntf.domino.Base<?>>) v;
            FactorySchema<?, ?, Session> schema = DateTime.SCHEMA;
            if (v.elementAt(0) instanceof lotus.domino.DateRange)   // at moment: never
                schema = DateRange.SCHEMA;
            else {  // Workaround for Vector of DateRange-s
                while (true) {
                    int sz = v.size(), i;
                    for (i = 0; i < sz; i++)
                        if (v.elementAt(i) != null)
                            break;
                    if (i < sz)
                        break;
                    vGIV = getDelegate().getItemValue(name);
                    if (vGIV.size() != sz * 2)
                        break;
                    for (i = 0; i < sz * 2; i++)
                        if (!(vGIV.elementAt(i) instanceof lotus.domino.DateTime))
                            break;
                    if (i < sz * 2)
                        break;
                    Vector<lotus.domino.DateRange> aux = new Vector<lotus.domino.DateRange>(sz);
                    lotus.domino.Session rawsession = toLotus(Factory.getSession());
                    for (i = 0; i < sz; i++) {
                        lotus.domino.DateTime dts = (lotus.domino.DateTime) vGIV.elementAt(2 * i);
                        lotus.domino.DateTime dte = (lotus.domino.DateTime) vGIV.elementAt(2 * i + 1);
                        lotus.domino.DateRange dr = rawsession.createDateRange(dts, dte);
                        aux.add(dr);
                    }
                    v = aux;
                    schema = DateRange.SCHEMA;
                    break;
                }
            }
            return (Vector<org.openntf.domino.Base<?>>) fromLotusAsVector(v, schema, getAncestorSession());
        } catch (NotesException e) {
            while (mayBeMime) {
                MIMEEntity entity = this.getMIMEEntity(name);
                if (entity == null)
                    break;
                Object mim = null;
                try {
                    mim = Documents.getItemValueMIME(this, name, entity);
                } finally {
                    closeMIMEEntities(false, name);
                }
                if (mim == null)
                    break;
                Vector<?> v;
                if (mim instanceof Vector)
                    v = (Vector<Object>) mim;
                else if (mim instanceof Collection)
                    v = new Vector<Object>((Collection<Object>) mim);
                else if (mim.getClass().isArray())
                    v = (Vector<Object>) Arrays.asList((Object[]) mim);
                else
                    break;
                int sz = v.size(), i;
                for (i = 0; i < sz; i++) {
                    Object o = v.elementAt(i);
                    if (o == null)
                        break;
                    if ((!(o instanceof DateTime)) && (!(o instanceof DateRange)))
                        break;
                }
                if (i < sz)
                    break;
                return (Vector<org.openntf.domino.Base<?>>) v;
            }
            DominoUtils.handleException(e);
            return null;
        } finally {
            if (vGIV != null)
                Base.s_recycle(vGIV);
        }
    }
Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
3

A little late, but this may help someone later on as an alternative.

  doc.getFirstItem("dateAdded").getDateTimeValue().getLocalTime()
carlo
  • 359
  • 3
  • 11
1

I updated the IBM example with something a little simpler that returns a Java Date data type. I rarely have encountered date fields with multiple values.

protected Date getDateValue(String fieldname, Document doc, boolean getenddate) throws NotesException{
    Vector times = doc.getItemValueDateTimeArray(fieldname);
    Object time = null;
    Date date = null;

    if(times != null){
        time = times.elementAt(0);

        if (time.getClass().getName().endsWith("DateTime")) {
            date = ((DateTime) time).toJavaDate();
        }
        else if(time.getClass().getName().endsWith("DateRange")) {
            if(getenddate == true){
                date = ((DateRange) time).getEndDateTime().toJavaDate();
            }else{
                date = ((DateRange) time).getStartDateTime().toJavaDate();
            }
        }
    }

    return date; 
}
xpagesbeast
  • 776
  • 1
  • 10
  • 21