LotusScript to replace @mailsend and @setfield

Hello Experts,

I have to replace formula language I have in place for sending an email from a database with Lotus Script so that rich text can be included in the email.  The emails are generated from a button on a form, and send some text that is input in one field, and a doclink to the form where there is more information and an attachment.

I have some lotus script that I modified from a post that I found.  So far, it does send the rich text from the Comments field, but I get an error that it can't find the Daily Mill form, and I'm not sure how to set all the fields that need to be updated, or how to do the prompt.

Any pointers gratefully accepted, I'm under a tight deadline so sample code appreciated even more!


Formula Language:
@Command( [EditDocument]; "1"; "1" );
ans := @Prompt([YesNo]; "Send memo?"; "Press Yes to confirm Send");
@If(ans = 1;
@Do(@SetField("Sent_H";"Yes");@SetField("Sent";"Yes") ;@SetField("Report_Date";@Today); @SetField("ReportTime"; @Now);
GlobalComment  +@NewLine+@NewLine+ Comments + @NewLine + @NewLine + Body + @NewLine + @NewLine +  "Please click this link to view the entire report.   ---->  ";[IncludeDoclink]) ;
@PostedCommand([FileSave]) ;
@Do(@SetField("Draft";"Yes") ;@PostedCommand([FileSave]);@PostedCommand([CloseWindow])))
Lotus Script so far:
Sub Click(Source As Button)
	Dim ws As New NotesUIWorkspace
	Dim note As NotesDocument
	Dim rt As NotesRichTextItem
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim doc As NotesDocument
	Set db = session.CurrentDatabase
	Set doc = New NotesDocument( db )   ' Mail Doc
	Set uidoc = ws.CurrentDocument
	Set note = uidoc.Document
	If uidoc.EditMode Then
		Call uidoc.Save
	End If
	Set rt = note.GetFirstItem( "Comments")  ' The documents richtext field
	doc.Form = "Daily Mill"
	doc.SendTo = "Joyce Kee/Fms/MetLife/US"
	doc.Subject = "Rich Text Test"
	Set body = New NotesRIchTextItem( doc, "Body" )
	Call body.AppendRTItem( rt )
	Call doc.Send( False )
End Sub

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

1) DO you have a "Daily Mill" form defined in the database.

If not, you will need to define one in Designer.

2) Check in Designer to see what forms are in the DB,  It may be best to use the "Memo" form if available.

I hope this helps !
jkee54Author Commented:
Yes there is a Daily Mill form; the doclink will bring the user back to it.  The form has more information in addition to the rich text that is being sent in the email.
You don't have to "Daily Mill" form, all you do is sending regular mail message.

Therefore you need to change doc.Form = "Daily Mill"  to
doc.Form = "Memo"

That should do it
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

You could have stuck with ofrmula language. the following would pick up the COMMENTS field from the source and drop it into your notification message:

GlobalComment  +@NewLine+@NewLine+ @NewLine + @NewLine + Body + @NewLine + @NewLine +  "Please click the link to view the entire report"; "Comments"; [IncludeDoclink]) ;
jkee54Author Commented:
The formula language picks up comments but not if it's rich text.
Not so. Please carefully read the Help description of the @MailSend function. The parameter after subject is supposed to be plain text. The next parameter (before the IncludeDocLink) is supposed to be THE NAME OF rich text field(s), in quotes.

Note that due to quirks in rich text processing, this works best when run from an agent oputside the document, or form a document in read mode. Note a doucment that has just been created and never closed.
jkee54Author Commented:
I see!

We'll be pushing these out with various deadlines during the day, so an agent is not really good.  The help document does say,
"You can specify a rich text field as one of the bodyfields in an agent formula only."

There are also 5 of us using this database, generating mail sends.  If I do it this way, can I set up the agent to run as often as I want, even every 15 minutes, looking for documents that are ready to go?  I have a 'sent flag' which could be used to identify those ready to go, but can the agent also set another sent flag so we know which ones actually went?
Yes, you can do that. Or, you can just set the action button to close the document and run the agent right away. You'll just have to code it in such a way that it recignizes what to run against, but that should not be hard.
Kennyboy5IT Support AnalystCommented:
Here's a snippet I use to send mail along with an attachment they click on to see the document in the database on the website:

Sub Initialize
	Dim maildb As notesdatabase
	Dim mailbox As notesdatabase
	Dim maildoc,doc As notesdocument
	Dim rtitem As notesrichtextitem
	Dim session As New notessession
	Dim db As notesdatabase
	Dim webdb As notesdatabase
	Dim supplierdb As notesdatabase
	Dim recips As String
	Dim s As New notessession
	Dim view As notesview
	Set db = session.currentdatabase
	server = db.server
	Set webdb = session.GetDatabase(server, "database on my website\mydb.nsf")
	Set maildb =session.GetDatabase( server, "mail/administ.nsf" )
	Set MailBox = session.GetDatabase(server,"mail.box")
	Set supplierdb = s.currentdatabase
	Set view = supplierdb.getview("Damage")
	Call view.refresh
	Set doc = view.getfirstdocument
	'sets up the email with the appropriate fields
	recips = "put recips in here as group or individuals"
	Set maildoc =  New Notesdocument(maildb)
	Set rtitem = New NotesRichTextItem(maildoc,"Body")
	Call rtitem.AppendText ("Click on the link below for the details.")
	Call rtitem.AddNewLine(2)
	maildoc.Form = "Memo"
	maildoc.From = "the database the mail is coming from (just type in whatever here"
	maildoc.SendTo = "same as recips above"
	maildoc.Subject = "New Damage Submission"
	maildoc.Principal = "Damage Submission"
	maildoc.recipients = recips
	Call view.refresh
	'attaches the link to the document and sends the email
	Call rtitem.appenddoclink(doc,"Click Here")
	Call maildoc.save(True,False)
	Call maildoc.copytodatabase(mailbox) 
End Sub

Open in new window

Kennyboy5IT Support AnalystCommented:
Just to add to the code, I have this as an agent that fires once they click on the Submit button on the webpage.
jkee54Author Commented:
To Qualtee:  I have it working perfectly in an agent! Thank you! except for one last problem.

When I ran @mailsend from the form, it showed up in the inbox as if it was coming from me - that's what it should do. From the agent, it appears to be coming from the server name.

It doesn't necessarily have to say my name, it could say "Reports Team" for example, if there's a way to code a string, but we'd rather not have it say the server.  Any way to correct that?
jkee54Author Commented:
I will add that I did find the setting on the security tab of the agent, "run on behalf of", which Help says should then be the name that shows up on mail, but it didn't work - the mail still comes in with the server name.
jkee54Author Commented:
I've upped the points as I need to install this, just need to fix this one last problem.  I have tried this many times, the mail still comes in saying it's from the server name, not me, even though I've used the 'run on behalf of' on the agent security tab.  Any thoughts?
Don't run the agent from the server. For @MailSend, that will always show as coming form the server. Instead, set it to run from agent list or action menu.
jkee54Author Commented:
I got it to work manually from the action menu.  It came through with my name.

I'm not sure how to use agent list, so I tried creating another agent that picks the first agent from the list, and runs it scheduled.  This one comes through with the server name again.

I really need this to run on a scheduler, not manually.   - I read the documentation about sending mail from the server and how it will use the server name, although I thought maybe it would be masked by using an agent within an agent.

I'm guessing I'll have to use Lotus Script, which I'm not that familiar with, unless I'm missing something?  Will that let me schedule and sign with the user's/sender's name?
OK, try this. Let's say you want this to run when the user clicks a button on the document that says "Save and Exit."

1) Add a hidden field to the form named MailSendNotify, type names, computed, formula is @UserName
2) Create a view called MailSendNotify, selection formula is: !(MailSendNotify="")
3) Create an agent, called MailSendNotify, type Formula language, run from action menu, targetting "All documents in view," updates documents. The formula for the agent is:

SELECT MailSendNotify=@UserName;
FIELD MailSendNotify := @DeleteField;
@MailSend(...... your existing mailsend code here .....)

4) Your Save and Exit button should contain the following code (you may need to adapt this if you have other code in your current button used to save and exit the document):

@PostedCommand([OpenView]; "MailSendNotify"; ""; "1");
@PostedCommand([ToolsRunMacro]; "MailSendNotify");

Now what happens is that when you save a document, an indication field is set with the users name, the document is exited, and a sequence of events occurs:
a) Special view is opened that only has doucments with this field set
b) agent is run
c) the view is exited, so the user is back where they would have been had the doucmnet simply exited normally

The agent runs against all documents in the view. The SELECT forces it to only run against the user's own documents (in case other users happened to save at the same time). The agent clears the value in the field, and does the MailSend. Since this all happens through the button and agent on the lcient's PC, it is sending in the user's own name instead of in the server's name.

I used @PostedCommand instead of @Command to ensure that everything runs in the right order. Certain @Commands don't always run in the right order, and will wait until everything else is finished before they run. using [PostedCommand] forces all the [activities] to behave the same way, and only run at the end of processing, in the same order. Most versions of Notes out there right now have alternatives to the weird [actions] that run in the proper order, but the above code will work for all versions of Notes since version 3.

If you want to take the agent out of the actions menu, you need to do two things.
1) Change the run action from "action menu" to "agent list"
2) In the [ToolsRunMacro], change teh agent name from [ToolsRunMacro];"MailSendNotify" to [ToolsRunMacro];"(MailSendNotify)"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkee54Author Commented:
I'm getting the error, Formula error: @Command and other UI functions are not allowed with this search type; please select 'None' as your runtime target." and it doesn't run.

I'm getting close though!
jkee54Author Commented:
Upping the points to bring this one home....

I've made some progress but am at a stopping point.  Everything works up until the agent tries to run.  It opens the view, selects the right documents, and starts to run - The documents disappear from the view (the FIELD MailSendNotify := @DeleteField; is working)but when it gets to the @mailsend statement it fails:  I get an error that there is no SendTo in the document. At first the field wasn't named SendTo, it was named RptSendTo - is SendTo a reserved name?

So I changed the name from RptSendTo to SendTo to see if that helped, so now there is in fact a SendTo field, and it is populated with the intended recipients.  The only change I made to your suggestions is that I made the MailSendNotify field a shared resource.  

Here is the @mailsend code:

GlobalComment  +@NewLine+@NewLine+ Comments + @NewLine + @NewLine + Body + @NewLine + @NewLine +  "Please click this link to view the entire report.   ---->  ";

It also seems to take awhile creating the view for the agent to act on.  Any way to speed that up?
I don't quite understand what problem you are having now, other than the view speed problem. You should not see it taking much time in this view, so long as you design the view the way I have described it... it should have only one column, sorted, and the view should normally be empty of documents.  The first time the view is used, it may take some time... or if the database is very active between uses of the view, it may take some time... otherwise, it shoudl be very quick, if your server in general is behaving well.
jkee54Author Commented:
I finally got it to work - I had to make some custom changes but it works perfectly.  I still may change to a script answer at some point to make it faster, but for now to get it up and running it is great.  Thank you qualtee!
Performance issue solved?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.