Appending Date into a multiple value date field via Lotus Script

I am trying to append the current date time into a date field on a form (multiple values alllowed) via LS and having no luck.. I first tried and appenditem and that didn't work (it creates additional items).  I've since tried a few other things and that has not helped.

Current code, which is not working is below, although it may be totally useless (or close - i don't know.)

Thanks

' field name i'm trying to update is LastSentDate

     If doc.hasitem("LastSentDate") Then
            
            
            Dim tmpdates() As Variant
            Dim i As Integer
            Dim vnextdate As Integer
            Dim vnumdates As Integer
            Dim todaydatetime2 as Variant            

            vnumdates=Ubound(doc.LastSentDate)
            vnextdate=vnumdates+1
            For i= 0 To vnextdate
                  Redim Preserve tmpdates(i)
                  If i = vnextdate Then
                        todaydatetime2=Now
                        tmpdates(vnextdate)=todaydatetime2
                  Else
                        tmpdates(i)=doc.LastSentDate(i)
                        
                  End If
                  
            Next
            doc.lastsentdate=tmpDates()
            
      Else
            Call doc.replaceitemvalue("LastSentDate",todaydatetime2)
smeyer4314Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

qwaleteeCommented:
You will need an array of dates, and then use NotesDocument.ReplaceItemValue
0
HemanthaKumarCommented:
Try this one

Sub Click(Source As Button)
      Dim ws As New NotesUIWorkspace
      Dim doc As notesdocument
      Set doc = ws.CurrentDocument.Document
      If doc.hasitem("LastSentDate") Then
            
            
            Dim tmpdates() As Variant
            Dim i As Integer
            Dim vnextdate As Integer
            Dim vnumdates As Integer
            Dim todaydatetime2 As Variant          
            
            vnumdates=Ubound(doc.LastSentDate)
            vnextdate=vnumdates+1
            For i= 0 To vnextdate
                  Redim Preserve tmpdates(i)
                  If i = vnextdate Then
                        todaydatetime2=Now
                        tmpdates(vnextdate)=todaydatetime2
                  Else
                        If Not doc.LastSentDate(i) = "" Then      tmpdates(i)=doc.LastSentDate(i)                        
                  End If
                  
            Next
            doc.lastsentdate=tmpDates()
            
      Else
            Call doc.replaceitemvalue("LastSentDate",todaydatetime2)
      End If
End Sub

~Hemanth
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HemanthaKumarCommented:
Here is the enhnaced version, which neglects any nulls in the string and gives a perfect list of dates


Sub Click(Source As Button)
      Dim ws As New NotesUIWorkspace
      Dim doc As notesdocument
      Set doc = ws.CurrentDocument.Document
      If doc.hasitem("LastSentDate") Then
            
            
            Dim tmpdates() As Variant
            Dim i As Integer
            Dim vnextdate As Integer
            Dim vnumdates As Integer
            Dim todaydatetime2 As Variant          
            
            vnumdates=Ubound(doc.LastSentDate)
            vnextdate=vnumdates+1
            For i= 0 To vnextdate
                  Redim Preserve tmpdates(i)
                  If i = vnextdate Then
                        todaydatetime2=Now
                        tmpdates(vnextdate)=todaydatetime2
                  Else
                        If Not doc.LastSentDate(i) = "" Then      tmpdates(i)=doc.LastSentDate(i)                        
                  End If                  
            Next
            count = 0
            For i = 0 To Ubound(tmpDates)
                  If tmpDates(i) <> "" Then
                        Redim Preserve newDates(count) As Variant
                        newDates(count) = tmpDates(i)
                        count = count + 1
                  End If
            Next            
            
            doc.lastsentdate = newDates()
            
      Else
            Call doc.replaceitemvalue("LastSentDate",todaydatetime2)
      End If
End Sub
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

qwaleteeCommented:
This should help you understand how to append a single value.  Put it in an agent, run it through the debugger, and after each ArrayAppend, take a look at the new variable assigned.  At the end, it does a replaceItemValue, which has 6 values -- Jan 1, Feb 2, Mar 3, Apr 4, the date/time the agent is run, and a default dateTime value, which shows in LotusScript as blank but is stored as a time value of 12:00 AM.

Sub Initialize
      Dim s As New notesSession
      Dim doc As notesDocument
      Set doc = s.currentdataBase.createDocument
      Dim a
      a = Evaluate("FIELD dates := [1/1] : [2/2]; [3/3] : [4/4]",doc)
      Dim item As notesItem
      Set item = doc.items(0)
      Dim b As Variant
      b = item.values
      Dim c
      c = Arrayappend(b,a) 'b had the field values, a had the extra dates from the evaluate; d should now be 1/1, 2/2, 3/3, 4/4
      Dim d
      d = Now
      Dim e
      e = Arrayappend(c,d) '1/1, 2/2, 3/3, 4/4, NOW
      Dim dateObj As New notesDateTime ("")
      Dim f
      f = Arrayappend(e,dateObj) '1/1, 2/2, 3/3, 4/4, NOW, default-notes-date-time-value -- 6 in all
      doc.replaceItemValue "dates" , f 'replaces the original two values -- 1/1, 2/2 -- with 6
End Sub
0
qwaleteeCommented:
And, here's a short version -- a function that appends a date for you.

Sub AppendDate ( doc as notesDocument, itemName as String, newDate as variant )
   'you can pass second parameter as a variant of type datetime, or a notesDateTime object
   Dim oldValue as Variant, combo as Variant
   oldValue = doc.getItemvalue(itemName)
   combo = ArrayAppend(oldValue,newDate)
   doc.replaceItemValue itemName , combo
End Sub

This can be easily adapted so you just have to pass a NotesItem and a new date...

Sub AppendDate ( item as notesItem, newDate as variant )
   'you can pass second parameter as a variant of type datetime, or a notesDateTime object
   Dim combo as Variant
   combo = ArrayAppend(item.values,newDate)
   item.values = combo
End Sub
0
Sjef BosmanGroupware ConsultantCommented:
How about this one? Can't get much shorter...

     If doc.hasitem("LastSentDate") Then
          Dim tmpdates As Variant
          Dim n As Integer

          tmpdates= doc.LastSentDate
          n= Ubound(tmpdates)+1
          Redim Preserve tmpdates(n)
          tmpdates(n)= todaydatetime2
          doc.lastsentdate=tmpDates()
     Else
          Call doc.replaceitemvalue("LastSentDate",todaydatetime2)
     End If
0
qwaleteeCommented:
sjef_bosman,
Well, "much shorter" is relative.  Yours is 11 lines, mine is 3, though I don't check for an undefined item.  I can do the check and still bring it down to four lines, I think.
0
Sjef BosmanGroupware ConsultantCommented:
Sure, okay, mine is 9 now (see below), but looks like the original code. And, for convenience you seem to forget to count the lines for the call from the main program and the declaration of the sub. Anyway, in the definition of ArrayAppend in Notes Help the feature you use isn't described, but it might work, since Notes is full of undoc features. You use a scalar as second parameter, whereas the Help says that ArrayAppend requires two arrays. I dare you ...

     If doc.hasitem("LastSentDate") Then
          Dim tmpdates As Variant
          tmpdates= doc.LastSentDate
          Redim Preserve tmpdates(Ubound(tmpdates)+1)
          tmpdates(Ubound(tmpdates))= todaydatetime2
          doc.LastSentDate=tmpDates()
     Else
          Call doc.replaceitemvalue("LastSentDate",todaydatetime2)
     End If
0
qwaleteeCommented:
sjef_bosman,

Dare?  Pants down!

1) read ArrayAppend doc more carefully.  first param is variant conating array, second is just plain variant

2) original "sample" code was tested before posted; the sub uses teh sample functions the same way

3) I'll count my sub if you'll count yours; otherwise...  care for a one-liner?  [untested]

   item.values = ArrayAppend(item.values,newDate)

Or,
   
   doc.myDateList = ArrayAppend(doc.myDateList , Now )


Or, with error check:

   If doc.hasItem("myDateList")
      doc.myDateList = ArrayAppend(doc.myDateList , Now )
   Else
      doc.myDateList = Now
   End If

0
qwaleteeCommented:
Interestingly, while ArrayAppend does accept a VARIABLE of type variant containing a scalar (date) as the second paraneter, t does not acept a function returning a scalar variant (date).  I.e., I can say x=now and pass x, but I can't pass now.  In at least one version, it even crashes Notes.  So I have to amend the code a bit, and add two lines:

      If doc.hasItem("LastSentDate") Then
            Dim theCurrentTime
            theCurrentTime = Now
            doc.LastSentDate = ArrayAppend(doc.LastSentDate , Now )
      Else
            doc.LastSentDate = Now
      End If

The Subroutine version does not have to change, as it did that already.  The one liners become three lines.

I've also fond that if you enclose teh secnd parameter in parentheses you will also possibly crash.  Apparently, it MUST have a variable of tpe variant, and any other type of expression is not qite the same (putting the variable in parentheses turns it into an expression).

So, maybe my belt is a little loose, but my pants are still up.

Here's a slightly shorter variation:

      Dim theCurrentTime
      theCurrentTime = Now
      If doc.hasItem("LastSentDate") Then      doc.LastSentDate = ArrayAppend(doc.LastSentDate , Now )
      If Not doc.hasItem("LastSentDate") Then      doc.LastSentDate = Now

Only for lines. But using the same trick, yours could be:

          Dim tmpdates As Variant
          tmpdates= doc.LastSentDate
          Redim Preserve tmpdates(Ubound(tmpdates)+1)
          tmpdates(Ubound(tmpdates))= todaydatetime2
     If doc.hasitem("LastSentDate") Then doc.LastSentDate=tmpDates()
          If Not doc.hasitem("LastSentDate") Then Call doc.replaceitemvalue("LastSentDate",todaydatetime2)
0
Sjef BosmanGroupware ConsultantCommented:
Okay, you've made your point, at least linecount-wise ;-).

Some remarks:
1. please check the R6 Help (for I just checked R5), to see that they applied some changes to the function ArrayAppend
2. ArrayAppend is to me a bit like using a cannon to kill a fly, but I agree with you that it looks neater than a Redim Preserve
3. R4 (still being used) doesn't have ArrayAppend

So keep 'em up! (your pants, I mean)
0
qwaleteeCommented:
Actually, I like arrayAppend better than Redim Preserve, because it is more efficient.  Remember, your redim preservce is based on a scripted calculation of what you need to redim, and then you script a change to the new array space.  ArrayAppend does that atomically, though having o set up a variant to pass loses some of that efficiency.  (I still can't believe it crashes Notes.)

I'm not religious about it, I just think it is more efficient, and elegant at that (what you call "neater")

Interesting, the change in docs for arrayAppend in 6.  Both parameters are now listed as variant containing array. But then you read further:

Error handling
ArrayAppend throws a Type mismatch error if:
sourceArray1 is not an array
an array with more than one dimension is used

*** sourceArray1 is not an array *** -- but not if sourceArray2 is not an array.  Sounds like a doc fudge.
0
Bozzie4IT ArchitectCommented:
Since you're obviously doing a pissing contest: do it in @formula's, in 1 line :

FIELD LastSentDate := @trim(LastSentDate:@today);
@true


:-)

cheers,

Tom

0
qwaleteeCommented:
How about:
@SetField("LastSentDate"; @If(@Trim(@Text(LastSentDate))=""; @Now; LastSentDate : @Now))

Or, in script:
someVariant = Evaluate({@SetField("LastSentDate"; @If(@Trim(@Text(LastSentDate))=""; @Now; LastSentDate : @Now))},doc)


0
qwaleteeCommented:
Oh, Bozzie, since we trying to shoot, er, shorter, your is technically two statements, do to the need for a formula result value.  of course, you are an R6 person, so I don't know why you bothered.
0
Sjef BosmanGroupware ConsultantCommented:
Just adding a drople in the contest: @Trim doesn't accept date-lists (according to R6 help). I like the formula-approach very much! Needless to say that we built Explode and Implode in LotusScript ourselves...

For the rest: I surrender.
0
qwaleteeCommented:
@trim, never converted date lists, hence the @Text.  The sole purpose of the @Trim(@Text...)) is to ensure that the field is absolutely blank before replace (first assignment), and if not, to do an append.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.