Link to home
Start Free TrialLog in
Avatar of AliciaVee
AliciaVee

asked on

LotusScript needs adjusting (sending mail)

Experts,

A form will track issues and "sometimes" it will be helpful for specific users to receive an alert when the document is submitted.  I have a field SendNotify that is a yes or no value and if this is Yes -- then another field is displayed that provides the ability for users to select names from the address book, otherwise, this field (NotifyGroup) is hidden.

Now, I had this script working earlier today (leveraged from another application I built) but when I changed it to affect only new documents, it stopped working.  Probably have the Ifs and End Ifs in the wrong place -- or some other minor issue, but I've been trying to figure it out and I think I'm getting farther from the solution.

This is what I'd like the script to do:

If SendNotify = Yes, then send an alert -- but there are two versions based on what the status is.  Bottom line, for each document, there will always be two alerts sent, one for when the document is first created (RptStatus = Open and IsNewDoc) and then one for when the log is closed (RptStatus = Closed).  If the author makes changes to the document, after it is saved once -- I don't want alerts going, only when the status changes to closed (hence IsNewNote)

If SendNotify = No -- then this alert should not run at all -- it should be ignored.  I have ithe code in the PostSave event -- this is a client only appliccaiton.  Please help!  Thanks in advance.

AliciaV

============================

Sub Postsave(Source As Notesuidocument)
      
      Dim workspace As New NotesUIWorkspace
      Dim currentdoc As NotesDocument
      Dim dbase As NotesDatabase
      Dim Session As New NotesSession
      Dim docStatus As String
      Dim newDoc As NotesDocument
      Dim rtitem As NotesRichTextItem
      Set currentdoc = source.Document
      Source.AutoReload = False
      Set dbase = session.CurrentDatabase
      
      If currentdoc.SendNotify(0) = "Yes" Then
            
            docStatus =currentdoc.RptStatus(0)
            
            If docStatus ="Open" And currentdoc.IsNewNote  Then
                  Set newDoc = New NotesDocument( dbase )
                  Set rtitem = New NotesRichTextItem( newDoc, "Body" )
                  newdoc.Form= "Memo"
                  
                  If docStatus ="Open" Then
                        newdoc.SendTo= currentdoc.NotifyGroup
                        newdoc.Subject= "New DEM Online Log Report - Priority: " & currentdoc.LogPriority(0)
                        
                        Call rtitem.AppendText("This is an automated alert from the DEM Online Resource database")
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("A New DEM Online Log Report has been created by " & currentdoc.dspDocAuthorDefault(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Log Priority: " & currentdoc.LogPriority(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Log Type: " & currentdoc.RptClassification(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Support Contact Name: " & currentdoc.dspContactName(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Information Reported: " & currentdoc.RptInfo(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Click here to go to the document --->  ")
                        Call rtitem.AppendDocLink( currentdoc, "Log Report" )
                  Else
                        
                        If docStatus ="Closed" Then
                              
                              newdoc.SendTo= currentdoc.NotifyGroup
                              newdoc.Subject= "DEM Online Log Report Closed: " & currentdoc.RptInfo(0)
                              
                              Call rtitem.AppendText("This is an automated alert from the DEM Online Resource database")
                              Call rtitem.AddNewLine(2)
                              Call rtitem.AppendText("The following DEM Online Log Report has been closed: ")
                              Call rtitem.AddNewLine(2)
                              Call rtitem.AppendText("Log Priority: " & currentdoc.LogPriority(0))
                              Call rtitem.AddNewLine(1)
                              Call rtitem.AppendText("Log Type: " & currentdoc.RptClassification(0))
                              Call rtitem.AddNewLine(1)
                              Call rtitem.AppendText("Support Contact Name: " & currentdoc.RptContactName(0))
                              Call rtitem.AddNewLine(1)
                              Call rtitem.AppendText("Information Reported: " & currentdoc.RptInfo(0))
                              Call rtitem.AddNewLine(2)
                              Call rtitem.AppendText("Solution Description: " & currentdoc.RptSolution(0))
                              Call rtitem.AddNewLine(1)
                              Call rtitem.AppendText("To view additional information for this Log Report, click here ----> ")
                              Call rtitem.AppendDocLink( currentdoc, "Closed Log Report" )
                        End If
                        Call newDoc.Send( False )                              
                  End If
            End If             
      End If
      
End Sub
Avatar of AliciaVee
AliciaVee

ASKER

Ugh!  I'm such a dummy!  Okay, I can guess why using currentdoc.IsNewNote will not work on a PostSave event -- the document is no longer a new document -- right?

So, I've added a new hidden field AlertFlag, and have set it to "New" and will update it once the mail is sent the first time, but its still now working.
Avatar of SysExpert
If docStatus ="Open" And currentdoc.IsNewNote

should be
If  (docStatus ="Open" And currentdoc.IsNewNote ) or ( docStatus ="Closed" ) then

I hope this helps !
ALso please note that once the docStatus ="Closed" , any edits will send out another alert.  You may want to change the new AlertFlag to false after the alert is sent.

Please post your new code.

I hope this helps !
probably change

 If docStatus ="Open" And currentdoc.IsNewNote  Then

to

If  (docStatus ="Open" And AlertFlag="New" ) or ( docStatus ="Closed" )

also, why can't you put this in the querySave ?

I hope this helps !
Hi SysExpert,

Okay, I've been playing the past couple of hours (no -- far from a LS expert), and I have compied up with the following, taking in the fact that I cannot capture IsNewNote in a postsave event (right??).  Anyway -- it is working fine, except for when the status is changed to Closed -- no email alert is sent.

So, there are three values for RptStatus: Open (this is the default) In Progress (should be changed by the user if the log is not closed, but the user wants to update notes, etc (in this way, no email alert will be sent, unless it is closed) and finnaly Closed status.

So, what is wrong? (I'm not using the AertFlag anymore...)  Do you think this should be in QuerySave instead?  I have validation code there -- will it conflict?

========================
Sub Postsave(Source As Notesuidocument)
      
      Dim workspace As New NotesUIWorkspace
      Dim currentdoc As NotesDocument
      Dim dbase As NotesDatabase
      Dim Session As New NotesSession
      Dim docStatus As String
      Dim docNotify As String
      Dim newDoc As NotesDocument
      Dim rtitem As NotesRichTextItem
      Set currentdoc = source.Document
      Source.AutoReload = False
      Set dbase = session.CurrentDatabase
      
      docStatus =currentdoc.RptStatus(0)
      docNotify = currentdoc.SendNotify(0)      
      
      If docNotify = "Yes" Then
            
            If docStatus="Open" Or docStatus="Closed" Then
                  Set newDoc = New NotesDocument( dbase )
                  Set rtitem = New NotesRichTextItem( newDoc, "Body" )
                  newdoc.Form= "Memo"
                  If docStatus="Open" Then
                        newdoc.SendTo= currentdoc.NotifyGroup      
                        newdoc.Subject= "New DEM Online Log Report - Priority: " & currentdoc.LogPriority(0)
                        
                        Call rtitem.AppendText("A New DEM Online Log Report has been created by " & currentdoc.dspDocAuthor(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Log Priority: " & currentdoc.LogPriority(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Log Type: " & currentdoc.RptClassification(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Support Contact Name: " & currentdoc.dspContactName(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Information Reported: " & currentdoc.RptInfo(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Click here to go to the document --->  ")
                        Call rtitem.AppendDocLink( currentdoc, "Log Report" )
                        
                  Else
                        newdoc.SendTo= currentdoc.NotifyGroup                        
                        newdoc.Subject= "DEM Online Log Report Closed: " & currentdoc.RptInfo(0)
                        
                        Call rtitem.AppendText("The DEM Online Log Report " & "Submitted on " & currentdoc.DocCreated &  " has been closed.")
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Log Priority: " & currentdoc.LogPriority(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Log Type: " & currentdoc.RptClassification(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Support Contact Name: " & currentdoc.dspContactName(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Information Reported: " & currentdoc.RptInfo(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Solution Description: " & currentdoc.RptSolution(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("To view additional information for this Log Report, click here ----> ")
                        Call rtitem.AppendDocLink( currentdoc, "Closed Log Report" )
                        
                  End If
                  
                  Call newDoc.Send( False )
                  
            End If
            
      Else
            Continue = False
      End If
End Sub
Okay -- my brain is now fried.  The only problem I am having now is that I'm getting two emails (when using myself as a test) instead of one, both for Open status and Closed status.  Can anyone help?  And, I'm more than open to fine tuning this code -- since I'm not sure its as efficient as it should (could) be.  Thanks!!

--------------------------------------------------------
Sub Postsave(Source As Notesuidocument)
      
      Dim workspace As New NotesUIWorkspace
      Dim currentdoc As NotesDocument
      Dim dbase As NotesDatabase
      Dim Session As New NotesSession
      Dim docStatus As String
      Dim docNotify As String
      Dim newDoc As NotesDocument
      Dim rtitem As NotesRichTextItem
      Set currentdoc = source.Document
      Source.AutoReload = False
      Set dbase = session.CurrentDatabase
      
      docStatus =currentdoc.RptStatus(0)
      docNotify = currentdoc.SendNotify(0)      
      
      If docNotify = "Yes" Then
            
            If docStatus="Open" Or docStatus="Closed" Then
                  Set newDoc = New NotesDocument( dbase )
                  Set rtitem = New NotesRichTextItem( newDoc, "Body" )
                  newdoc.Form= "Memo"
                  newdoc.SendTo= currentdoc.NotifyGroup            
                  
                  If docStatus="Open" Then
                        newdoc.Subject= "New DEM Online Log Report - Priority: " & currentdoc.LogPriority(0)
                        
                        Call rtitem.AppendText("A New DEM Online Log Report has been created by " & currentdoc.dspDocAuthor(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Log Priority: " & currentdoc.LogPriority(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Log Type: " & currentdoc.RptClassification(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Support Contact Name: " & currentdoc.dspContactName(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Information Reported: " & currentdoc.RptInfo(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Click here to go to the document --->  ")
                        Call rtitem.AppendDocLink( currentdoc, "Log Report" )
                        
                  Else
                        newdoc.Subject= "DEM Online Log Report Closed: " & currentdoc.RptInfo(0)
                        
                        Call rtitem.AppendText("The following DEM Online Log Report has been closed.")
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Log Priority: " & currentdoc.LogPriority(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Log Type: " & currentdoc.RptClassification(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Support Contact Name: " & currentdoc.dspContactName(0))
                        Call rtitem.AddNewLine(1)
                        Call rtitem.AppendText("Information Reported: " & currentdoc.RptInfo(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("Solution Description: " & currentdoc.RptSolution(0))
                        Call rtitem.AddNewLine(2)
                        Call rtitem.AppendText("To view additional information for this Log Report, click here ----> ")
                        Call rtitem.AppendDocLink( currentdoc, "Closed Log Report" )
                        
                  End If
                  
                  Call newDoc.Send( False )
                  
            End If
            
      Else
            Continue = False
      End If
End Sub
You are correct, IsNewNote in a postsave << isNewNote means it hasn't been saved, and in Post Save, it will never be a new note.

YOu can add a field in the save event to check for notification status, like OKNOTIFY, OKREMIND, and check for the field in the postsave event.  However, if you delete or modify the field, then you also have to add some flag in your SAVE event to bypass all the other validations.

The notes mail file does this using an ActionInProgress field and a script library with constants.  For example,

When I want to bypass all the normal save validations, I set an action-in-progress value.. but I do this at the form level, and class objects.  This way as long as the handle to the current document is open, I will have the constants associated with the document through all the form events.

So when you save the form, you need to know what action is in progress so you don't try to validate stuff that will fail.  Especially in a Post Save event that may need to know if it needs to send a notification and if so, what kind.  (BTW, I do this is the query close event, or post close).

I can give you better assistance when I get home tonight.

The reason you don't want any notification going out in the "query save" is because:  people hit control + s, and end up spamming the world.  People escape without saving, or canceling.  The only way you know that it's a true document that needs a notification is in the query close, or post close.  Also, it's easier to update the document on the close, rather than on the qs.
Marilyng -- thanks for your comments and detailed information.  I think I'm following you -- so should I move the code to the query close? Or is it okay in the PostSave event?

Also -- at this point, I've pasted the most recent code I've come up with -- and I'm getting two emails -- don't konw why?
It looks OK.
Are you sure you do not have any code anywhere else that would cause duplicates ?

Please note that if you do any save, it loks like you will get an alert. Even for just an Edit ( unless docNotify = "Yes" is turned off somewhere )

I hope this helps !
SysExpert,

I did review where there might be a duplicate value being taken -- but could not find anything (I'm not that good with debugger).  So, I'm not sure where I should be looking?  I figured, if I'm taking the values from the field NotifyGroup, and there is only one name and not dupes (I just did another test and picked 5 people -- all 5 people got two emails for the "Open" Alert!).  Woe is me....

And, yes, if someone goes in and updates the document, but doesn't close it -- they will send duplicate alerts, but, for the most part, I am recommending that if it doesn't get closed (in the 2nd update of the document), then when they add comments, they need to change the status from Open to In Progress.  In this way, they do not send alerts.  Is there a way to track or automate doing this for them?  In other words, they first create a docment, with default OPEN.  They go back in to do comments, but don't close the document -- is there a way, on save, that the Status field can automatcially get changed to In Progress (just in case they forget?).

Also, there will be many documents that will not use the alert features -- and is for tracking purposes only -- so docNotify will equal No -- and is No on default, unless the user changes it.  Am open to suggestions here...

Thanks!
DId you check that the code is not in Postsave and anywhere else ?

You need some status fields as Marilyn suggested, perhaps something in the postopen that can differentiate between a new and a re-edited doc.

I hope this helps !
There wouldn't be any duplicate mail sends, the way the code is written and placed in the post save event, anytime you save will generate an email.  For instance, if you have a button that says, @if(@Command([FileSave]).. depending on how the button is composed, this will run the save event twice, thus sending two emails.

I would move the code to one of the close events.   Then add an actioninprogress field that you can fill with a number or event.  So that when your querysave happens, it doesn't process everything, if the last thing that was saved was the update to send notifications.  Ive added a field for each notification, along with a date - so when someone tells me "I didn't get it.." I can point to the document..

Easiest is to create two fields.. sendnotify, closenotify.   And check for the status on the close or post close event.  If the notification is sent, then update the field.  Then modify the send notice to check those fields and the status.
M,

okay -- I moved the code to the queryclose event -- and didn't realize that I needed to create new fields in order for this to work correctly (I opened and closed several documents and alerts went flying!  Ha ha)

Before I add the new fields, I need to understand if this will work for what I need.  The scenarios are:

1.A user adds a log document. It may or may not require alerts to be sent to other people, so no names will be in GroupNotify and SendNotify will equal "NO" (the default.)

2. All documents will have the status OPEN as a defaul.  The user can choose to update this field by clicking a button and change its value to IN PROGRESS or CLOSE.

3. If SendNotify = NO -- then it doesn't matter whether OPEN or IN PROGRESS is in status -- but the goal is to get this issue CLOSED.

4. If SendNotify = Yes, then on composing the document, the status field will be OPEN on default (although the user can change this to IN PROGRESS before saving and then no alerts will be sent).  So -- here I have a problem, unless I remove the button that updates the status field to IN PROGRESS -- so choices are OPEN or CLOSE.  I guess if I go this route, then I can add a new flag field that sets the value to OKNotify so if a user goes back to the document to add updates, but hasn't closed it, the flag field will stop future alerts from sending.  But -- I need a way for an alert to be sent when the document is CLOSED.

What to do?
M -- by the way, how do I change the OKNotify fields value?  Through the LS code that sends the initial email -- where would that snippit be and what would it look like?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of marilyng
marilyng

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Marilyng -- I got it working great now.  Yay!  I am using a AlertFlag field to capture values of "Open Nofity Sent" and "Close Notify Sent" after each one kicks off, so they don't conflict and users can continue to update the documents without sending additional alerts -- unless of course the status goes from open to close, then open again, which at that point I want another alert to go out stating the log was opened (or reopend -- not currently closed).

Anyway -- no dupe emails being sent and its working great.  Thank you!!