Domino Code Fragment

Code Name*
Exchanging items values between
documents.
Date*
04/19/2000
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.
Description*
EKIPUSH.LSS Function Library Notes R4 LotusScript functions for exchanging items values between
documents.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:

%REM
----------------------------------------------------------------------------
FILE:    EKIPUSH.LSS - ItemPushs Function and Documentation
UPDATED: April 1997
AUTHOR:  Eric Koeler
       
mailto:ekoeler@panix.com
       
http://www.panix.com/~ekoeler
NOTE:    Copyright (C) 1997 by Eric Koeler, this file is protected by the
        GNU General Public License
----------------------------------------------------------------------------
First of all, in order to use the functions in this library, you must also
include the functions from EKLIB.LSS.  In the Declarations section of the
script in which you plan to use these functions, add the following two
lines:


      %INCLUDE "FILEPATH\EKLIB.LSS"
     %INCLUDE "FILEPATH\EKIPUSH.LSS"


Where FILEPATH is the OS-specific, fully qualified path to the location
where you saved these two files.
----------------------------------------------------------------------------
The ItemsPush function can make exchanging a number of fields from one
document to another quick and easy.  The function makes use of a custom
data type, ItemMap, which maps the name of the item on the source document
with the name of the item on the target document.  You should create an
array of type ItemMap and set up one structure per item...


     Dim MyItems(1 to 3) As ItemMap

     MyItems(1).SourceName = "FieldName1"
    MyItems(1).TargetName = "FieldName1"
    MyItems(1).DefaultVal = "BlahBlahBlah"
    MyItems(1).SetFlag    = REPLACE_VALUE


     MyItems(2).SourceName = ""
    MyItems(2).TargetName = "FieldName2"
    MyItems(2).DefaultVal = "Hhhhmmmmmm"
    MyItems(2).SetFlag    = REPLACE_VALUE


     MyItems(3).SourceName = "FieldName3"
    MyItems(3).TargetName = "FieldName3"
    MyItems(3).DefaultVal = "YesYesYes"
    MyItems(3).SetFlag    = APPEND_VALUE


This sample ItemMap array, passed to the ItemsPush function with source
and target NotesDocuments, specifies the following:


     (1) Copy the value of FieldName1 on the source document to FieldName1
        on the target document, if the values are different.  If the values
        are null, then the function will put "BlahBlahBlah" into FieldName1
        on the target document.  
    (2) Overwrite the value in FieldName2 on the target document with
        "Hhhhmmmmmm".  
    (3) Append the value in FieldName3 on the source document to the value
        of FieldName3 on the target document.


If the target document was changed in any way, the ItemsPush function
returns True, otherwise it returns False.  This function does not save the
target document, so you should set up the call to ItemsPush like this:


     If ItemsPush(srcDoc, trgDoc, MyItems, 3) Then
         Call trgDoc.Save(True, True)
    End If


This way you are only saving the target doc if it has changed.
----------------------------------------------------------------------------
%ENDREM


'Declarations
%INCLUDE "EKLIB.LSS"


'Data structure for ItemsPush function
Type ItemMap
    SourceName As String                 ' Name of source item in source doc
    TargetName As String                 ' Name of target item in target doc
    DefaultVal As String                 ' Default value if source is blank
    SetFlag    As Integer                ' Append or Replace target value
End Type


'Constants for SetFlag member of ItemMap
Const APPEND_VALUE  = 0
Const REPLACE_VALUE = 1


Function ItemsPush(srcDoc     As NotesDocument, _
                  trgDoc     As NotesDocument, _
                  theItems() As ItemMap,       _
                  numItems   As Integer)       As Integer
   
    On Error Goto ErrorHandler
    ItemsPush = False
   
    Dim X        As Integer
    Dim changed  As Integer
    Dim tempItem As NotesItem
    Dim theVal   As String
   
    If (srcDoc Is Nothing) Or (trgDoc Is Nothing) Then Goto TheEnd
    changed = False    
   
    '----- Iterate through the item array
    For X = 1 To numItems
         
         '----- If the target name is not null (skip to next if it is)
         If Strcomp(theItems(X).TargetName, "", 1) Then
             
              '----- If the name is not null & item exists & is not null
              If ItemTextExists(srcDoc, theItems(X).SourceName) Then
                   
                   '----- Get the source item
                   Set tempItem = srcDoc.GetFirstItem(theItems(X).SourceName)


                    '----- If the source is a date, format it
                   If tempItem.Type = DATETIMES Then
                        theVal = StringNotNull(Format$(tempItem.Values(0), _
                        dateFormat), Format$(Today, dateFormat))
                   Else


                         '----- Otherwise, just get the text val of source
                        theVal = tempItem.Text
                   End If
              Else
                   
                   '----- Get the default value
                   theVal = theItems(X).DefaultVal
              End If


               '----- If we are asked to append the val
              If theItems(X).SetFlag = APPEND_VALUE Then
                   If ItemTextExists(trgDoc, theItems(X).TargetName) Then
                       
                        '----- Get the target item
                        Set tempItem = trgDoc.GetFirstItem(_
                        theItems(X).TargetName)


                         '----- Append the value to the item values
                        If Not(tempItem Is Nothing) Then _
                        Call tempItem.AppendToTextList(theVal)
                        changed = True
                   Else
                       
                        '----- Replace the target item with the value
                        Set tempItem = trgDoc.ReplaceItemValue(_
                        theItems(X).TargetName, theVal)
                        changed = True
                   End If
              Else
                   '----- Check if replacement is same as current
                   If ItemTextExists(trgDoc, theItems(X).TargetName) Then
                        If Strcomp(ItemTextReturn(trgDoc, _
                        theItems(X).TargetName), theVal) Then
                             
                             '----- Replace the target item with the value
                             Set tempItem = trgDoc.ReplaceItemValue(_
                             theItems(X).TargetName, theVal)

                              changed = True
                        End If
                   Else
                       
                        '----- Replace the target item with the value
                        Set tempItem = trgDoc.ReplaceItemValue(_
                        theItems(X).TargetName, theVal)
                        changed = True
                   End If
              End If
              If Not(tempItem Is Nothing) Then tempItem.IsSummary = True
              Set tempItem = Nothing
              theVal = ""
         End If    
    Next X
   
    '----- Return whether or not this function changed the target doc
    ItemsPush = changed
   
TheEnd:
    Exit Function
   
ErrorHandler:
    Print "ItemsPush: " & Trim$(Str$(Err)) & ": " & Error$
    Resume Next
   
End Function