Solved

Appending Date into a multiple value date field via Lotus Script

Posted on 2003-10-24
17
1,809 Views
Last Modified: 2013-12-18
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)
0
Comment
Question by:smeyer4314
  • 10
  • 4
  • 2
  • +1
17 Comments
 
LVL 31

Expert Comment

by:qwaletee
ID: 9616233
You will need an array of dates, and then use NotesDocument.ReplaceItemValue
0
 
LVL 24

Accepted Solution

by:
HemanthaKumar earned 125 total points
ID: 9616265
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 9616308
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9616364
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9616418
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 9616490
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9616684
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 9616835
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 31

Expert Comment

by:qwaletee
ID: 9621286
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9621449
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 9621981
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9625275
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
 
LVL 15

Expert Comment

by:Bozzie4
ID: 9626846
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9626881
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9626890
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 9627018
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9627404
@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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For users on the Lotus Notes 8 Standard client, this article provides information on checking the Java Heap size and adjusting it to half of your system RAM in attempt to get the Lotus Notes 8.x Standard client to run faster.  I've had to exercise t…
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now