Tuesday, March 9, 2010

Function to Enable or Disable a scheduled agent (#2) - Added param

Function scheduledAgentSetEnabled(enable As Boolean, targetDB As NotesDatabase,scheduledAgentName As String) As Boolean
'***************************************************************
'@Purpose    :    To enable or disable a scheduled agent in target database
'@Param       :     enable - boolean, true if agent is to be enabled and false otherwise
'@Param       :     targetDB - NotesDatabase, database in which the agents have to be processed
'@Param       :     scheduledAgentName - String, name of the agent on which the operation has to made
'@Name        :    scheduledAgentSetEnabled
'@Author        :    Karthikeyan A
'@Return        :    Boolean. True if the operation completes successfully and false other wise
'***************************************************************
   
    'mark the flow of control getting inside the current function
    scheduledAgentSetEnabled=False
   
    'constants used in this function
    Const TITLE_ITEM="$Title"
    Const ASSIST_FLAG_ITEM="$AssistFlags"
   
    'declare variables and objects necessary for further manipulation
    Dim nc As notesnotecollection
    Dim desDoc As NotesDocument
    Dim noteID As String
    Dim assistFlags As String
   
    'handle errors in case of abrupt termination
    On Error Goto errHandler
    If Not targetDB.IsOpen Then
        Exit Function
    End If
   
    'get the notes collection of all the agents in the current database
    Set nc=targetDB.CreateNoteCollection(False)
    nc.SelectAgents=True
    Call nc.BuildCollection()
    'get the first noteid from the notes design document collection
    noteID=nc.GetFirstNoteId
    'loop through the design document collection using the noteIDs and enable or diable the agent whose title matches
    'input parameter- agent name
    Set desDoc=targetDB.GetDocumentByID(noteID)
    While Not desDoc Is Nothing       
        If Ucase(desDoc.GetItemValue(TITLE_ITEM)(0))=Ucase(scheduledAgentName) Then
            assistFlags=desDoc.GetItemValue(ASSIST_FLAG_ITEM)(0)
           
            If enable Then
                If Not Instr(Ucase(assistFlags),"E") Then
                    assistFlags=assistFlags & "E"
                End If
            Else
                While Instr(Ucase(assistFlags),"E")
                    assistFlags=Replace(Ucase(assistFlags),"E","")               
                Wend
            End If   
            Call desDoc.ReplaceItemValue(ASSIST_FLAG_ITEM,assistFlags)       
            Call desDoc.Save(True,False)
           
        End If
        noteID=nc.GetNextNoteId(noteID)
        Set desDoc=targetDB.GetDocumentByID(noteID)
    Wend
   
        'mark the flow of control moving out of the current function
    scheduledAgentSetEnabled=True
   
    Exit Function
    'inform the user regarding the errors that resulted in abrupt termination
errHandler:
    If Err=4270 Then Exit Function   
    Msgbox "Error ***" & Error & "*** occured in line ***" & Cstr(Erl) & "*** with error number ***" & Cstr(Err) & "*** in agent scheduledAgentSetEnabled(enable As Boolean, agentName As String)"
    Print "Error ***" & Error & "*** occured in line ***" & Cstr(Erl) & "*** with error number ***" & Cstr(Err) & "*** in agent scheduledAgentSetEnabled(enable As Boolean, agentName As String)"
    Exit Function
End Function

No comments:

Post a Comment