I am adding a small script into query close in the standard Notes 9 mail template; that copies the sent mail into a CRM system that we are using. Note the code below.
The scrips always runs twice… how can I prevent it from running twice…
XXXX
Dim session As New NotesSession
Dim db As NotesDatabase
Set db = session.CurrentDatabase
Dim workspace As New NotesUIWorkspace
Call workspace.DialogBox( “aCRMdialog”, True, True, True, False, False, False, “Select project”, )
If source.Document.fldAlert(0) = “Will not be saved in CRM” Then
Print “Not copied to CRM”
Else
Set maildoc = db.GetDocumentByUNID(source.document.UniversalID)
Dim INCRMdoc As NotesDocument
Dim CRMdoc As NotesDocument
Dim CRMdb As New NotesDatabase( “”, “” )
Call CRMdb.Open( source.Document.serverinn(0), “databases\crm.nsf” )
If Not source.document.fldLookupCompContacts(0) = “” Then
’ Copy email document to CRM and make response
Set CRMdoc = CRMdb.GetDocumentByUNID(source.document.gildiCompContact(0))
Set INCRMdoc = maildoc.CopyToDatabase( CRMdb )
Call INCRMdoc.MakeResponse( CRMdoc )
Call INCRMdoc.Save (True, True)
End If
If Not source.document.fldLookupContacts(0) = “” Then
’ Copy email document to CRM and make response
Set CRMdoc = CRMdb.GetDocumentByUNID(source.document.gildiContacts(0))
Set INCRMdoc = maildoc.CopyToDatabase( CRMdb )
Call INCRMdoc.MakeResponse( CRMdoc )
Call INCRMdoc.Save (True, True)
End If
End If
Subject: Double check the dialog-form
I’ve used that too and it works… Have you checked that you are not calling also a dialogbox in the form “aCR= Mdialog”?
Additionally I would also put the code to ‘PostSend’ and why are you getting the maildoc by universalID? Just
set maildoc =3D source.document
should do it:
Sub PostSend(Source as Notesuidocument)=A8
Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim maildoc as NotesDocument
Dim db As NotesDatabase
Set db =3D session.CurrentDatabase
Call workspace.DialogBox( “aCRMdialog”, True, True, True, False, False, False, “Select project”, )
Set maildoc =3D source.document
If maildoc.fldAlert(0) =3D “Will not be saved in CRM” Then
Print “Not copied to CRM”
Else
Dim INCRMdoc As NotesDocu= ment
Dim CRMdoc As NotesDocume= nt
Dim CRMdb As New NotesDat= abase( “”, “” )
Call CRMdb.Open( maildoc.= serverinn(0), “databases\crm.nsf” )
If Not maildoc.fldLookupC= ompContacts(0) =3D “” Then =
’ Copy email document to CRM and make response
Set CRMdoc =3D CRMdb.GetDocumentByUNID(maildoc.gildiCompContact(0))<= /font>
If not CRMdoc is nothing then =
Set INCRMdoc =3D maildoc.CopyToDatabase( CRMdb )
Call INCRMdoc.MakeResponse( CRMdoc )
Call INCRMdoc.Save (True, True)
Print “Copied to CRM”
Else
Print “Not copied to CRM”
End if
End If
End If
End Sub
Subject: moved to postsend… still running twice…
I moved the script to postsend and simplified the code for debugin purposes… removed any save calls… I am still getting the workspace.DialogBox twice… can it have something to do with the workspace.DialogBox parameters?
’ // Copy to CRM
Dim session As New NotesSession
Dim db As NotesDatabase
Set db = session.CurrentDatabase
Dim workspace As New NotesUIWorkspace
Call workspace.DialogBox( “aCRMdialog”, True, True, True, False, False, False, “Select project”, )
Print “Not copied to CRM”
’ // Copy to CRM ends
Subject: Re: Query Close in mail template running twice
In current code
- It looks like the code in QueryClose is getting executed only once. However the code contains two ‘IF’ loops from where the document can get saved.
- It looks like there is possibility of a document satisfying both ‘IF’ conditions and hence the document is getting saved twice in the CRM database.
- Secondly, as the code is kept in QueryClose event, it is getting executed for every QureyClose (irrespective of document is ‘Saved as draft’ or ‘Sent’). If document is to be saved only after ‘SENT’ then there is need of flags to be set/reset appropriately from PostOpen, QuerySave event to avoid duplication of the document.