LS Agent - Having problems selecting multiple docs in a view and setting field values

Hi,

I am struggling to get this agent to work in LS. I have an action on a view which is meant to set all documents which are selected to Paid, along with some other fields, and then email the person(s) to say their expense(s) has/have been paid.

The button on the form is formula language and is below:

@Command([EditDocument];“1”);

@Command([ViewRefreshFields]);

@If(Change = “Yes”; @Do(@Prompt([Ok]; “Please Confirm Change”; “Please press the Notify Change Button to confirm your change”); @Return(“”)); “”);

FIELD Cancelled:=“No”;

FIELD Decline:=“No”;

FIELD SentForApp:=“No”;

FIELD ManApproved:=“No”;

FIELD Paid:=“Yes”;

FIELD Status:=“Paid”;

FIELD LogName:=@Name([CN];@UserName);

space:= " ";

comma:=“,”;

month:=@Select(@Month(@Today);“January”;“February”;“March”;“April”;“May”;

“June”;“July”;“August”;“September”;“October”;“November”;“December”);

day:=@Text(@Day(@Today));

year:=@Text(@Year(@Today));

date:=day + space+ @Left(month;3) + space + comma + space + year;

FIELD ModifyDate:=date;

FIELD Log:= Log+@NewLine+Status+ " confirmed by “+LogName+” on " +ModifyDate;

@Command([ViewRefreshFields]);

@Command([FileSave]);

@MailSend(Name;Acccounts;CreatedBy;“Expense Form for “+Name+” Has Been Paid “;””;“This expense form has been paid by Accounts.”+@NewLine +@NewLine + “Please follow link” +@NewLine +@NewLine + “Thank you.”;[IncludeDoclink]);@PostedCommand([FileSave]) & @PostedCommand([FileCloseWindow])

This works perfectly, but now the accounts person has asked for a button on a view, so they can select more than one expense at a time, instead of having to go into each individual one. (Which makes sense). I have written an agent in LS below, but it appears to be working on one expense, but not 2 if 2 are selected.

Does anyone happen to know why? I’ve also been messing around trying lots of things, so you might find some Dim’s etc… are no longer required.

I’ve also coded out the email part as I know this is working.

I have coded out this:

’ f_log.AppendToTextList Char(10) & " Paid Confirmed by " & v_commonName & " on " & Cstr(str_Date)

as I’m not sure how to fix this. On this field, it is multivalue but does not have newline selected as I did this in formula language, I and trying to find what newline is in LS, but am also having problems. Does anyone know this?

Sub Initialize

Dim workspace As NotesUIWorkspace

Dim session As New Notessession

Dim db As NotesDatabase

Dim col As NotesDocumentCollection

Dim doc As NotesDocument

Dim newdoc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim uidoc As NotesUIDocument

Dim str_Date As String



Set workspace=New NotesUIWorkspace

Set db = session.currentdatabase

Set newdoc=New NotesDocument(db)

Set uidoc=workspace.currentdocument

Set rtitem=New NotesRichTextItem(newdoc,"Body")

'get the selected docs

Set col = db. UnprocessedDocuments 



If col Is Nothing Then

	Msgbox "none docs selected"

	Exit Sub

End If

'now you have a doccollection that you can handle

Set doc = col.Getfirstdocument

While Not (doc Is Nothing)

'do anything with the doc

	Dim f_log As NotesItem

	Set f_log = doc.getFirstItem( "Log" )

	str_Date = doc.c_Date(0)

	

	If doc.Change(0 ) = "Yes" Then

		Msgbox("Please Confirm change, Please press the Notify Change button to confirm your change.")

		continue = False

	End If

	

	v_commonName = session.CommonUserName

	

	doc.LogName=v_commonName

	doc.Cancelled="No"

	doc.Decline="No"

	doc.SendForApp="No"

	doc.ManApproved="No"

	doc.Paid="Yes"

	doc.Status="Paid"

	doc.ModifyDate=str_Date

	NameN=doc.Name(0)

’ f_log.AppendToTextList Char(10) & " Paid Confirmed by " & v_commonName & " on " & Cstr(str_Date)

	doc.Save True, True

	

	flag=doc.ComputeWithForm(False,False)

'######## Start - Coding email out as this part is working ########

’ newdoc.Form=“Memo”

’ newdoc.Subject="Expense for " & NameN & “, Has been Paid”

’ newdoc.SendTo=doc.Name

’ newdoc.CopyTo=doc.Accounts

’

’ Call rtitem.AddNewLine(1)

’ Call rtitem.AppendText(“This expense form has been paid by Accounts”)

’ Call rtitem.AddNewLine(2)

’ Call rtitem.AppendText("Please follow link to view bid ->> ")

’ Call rtitem.AppendDocLink(doc,db.Title)

’

’ Call newdoc.Send(False)

'######## End - Coding email out as this part is working ########

	Set doc = col.Getnextdocument(doc)

	

	'doc.ComputeWithForm( False, False )

	'flag=doc.ComputeWithForm(False,False)

	

'	uidoc.Refresh

'	uidoc.Reload

	

'	Call doc.Save(False, True)

'	Call uidoc.Close

Wend

End Sub

Many thanks to all that read this and help.

Hayley.

Subject: LS Agent - Having problems selecting multiple docs in a view and setting field values

Thank you Rampisad,

I have made your changes, but it still is not working correctly. If I select 2 documents in the view, and then hit the button in the view which runs this agent, it only runs on the document which my cursor is actually on, and not the docs I have selected?

Hayley.

Subject: RE: LS Agent - Having problems selecting multiple docs in a view and setting field values

You are using db.unprocessed documents which implies that your agent should be:

Set to ‘Run manually from Agent list’

Set to run on Selected Documents

The first hides it from the user in the actions menu

The second picks up on the selected documents.

Make sure your agent is shared and not private.

You could also use the documents property of the NotesUIView object

(set uiv =ws.currentview

Set col=uiv.documents)

The agent selection can then be set to ‘None’

Subject: Thank you everyone, this is now working as I intended.

Subject: LS Agent - Having problems selecting multiple docs in a view and setting field values

There’s nothing wrong with your logic to create the collection and loop through it, so I think maybe the save/ComputewithForm may be throwing you out. Try this - after you get an entry in the collection, save it in a new Document object, then do your work on the doc, write it, and now point doc to the “saved” object, so that your colelction pointer is still intact.

Something like this …

Dim savdoc as NotesDocument

Set doc = col.Getfirstdocument

While Not (doc Is Nothing)

Set savdoc=doc

            'do anything with the doc

Set doc = col.Getnextdocument(savdoc)

Wend

HTH

Subject: LS Agent - Having problems selecting multiple docs in a view and setting field values

Hello Melissa,

Thank you for your help. that has fixed it.

I now have a problem with the emails though. If I select 2 documents, I am receving 2 emails, which is great, but… one email has a doc link to both expenses, and the other email has a doc link to one expense? I’d like one email to be generated and sent for each expense.

I submitted both expenses myself so would expect to receive email.

Any additional help, really appreciated.

Here is my revised code now:

Hayley.

Sub Initialize

Dim workspace As New NotesUIWorkspace

Dim session As New Notessession

Dim db As NotesDatabase

Dim col As NotesDocumentCollection

Dim doc As NotesDocument

Dim newdoc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim str_Date As String

Dim uiv As NotesUIView



Set db = session.CurrentDatabase

Set newdoc=New NotesDocument(db)

Set rtitem=New NotesRichTextItem(newdoc,"Body")

Set uiv = workspace.CurrentView

'Get the selected documents

Set col = uiv.Documents



If col Is Nothing Then

	Msgbox "none docs selected"

	Exit Sub

End If

'Set the documents I now have to Paid and email the person who the expense is for to inform them

Set doc = col.GetFirstDocument

While Not (doc Is Nothing)

	Dim f_log As NotesItem

	Set f_log = doc.getFirstItem( "Log" )

	str_Date = doc.c_Date(0)

	

	If doc.Change(0 ) = "Yes" Then

		Msgbox("Please Confirm change, Please press the Notify Change button to confirm your change.")

		continue = False

	End If

	

	v_commonName = session.CommonUserName

	

	doc.LogName=v_commonName

	doc.Cancelled="No"

	doc.Decline="No"

	doc.SendForApp="No"

	doc.ManApproved="No"

	doc.Paid="Yes"

	doc.Status="Paid"

	doc.ModifyDate=str_Date

	NameN=doc.Name(0)

	

	f_log.AppendToTextList Chr(13) & "Paid Confirmed by "  & v_commonName & " on " & Cstr(str_Date)

	Call doc.Save (True, True)

	

	newdoc.Form="Memo"		

	newdoc.Subject="Expense for " & NameN & ", Has been Paid"

	newdoc.SendTo=doc.Name

	newdoc.CopyTo=doc.Accounts

	

	Call rtitem.AddNewLine(1)

	Call rtitem.AppendText("This expense form has been paid by Accounts")

	Call rtitem.AddNewLine(2)

	Call rtitem.AppendText("Please follow link to view bid ->>  ")

	Call rtitem.AppendDocLink(doc,db.Title)

	Call newdoc.Send(False)

	

	Set doc = col.GetNextDocument(doc)

Wend

End Sub

Subject: RE: LS Agent - Having problems selecting multiple docs in a view and setting field values

You need to set a new rtitem each time you send an email. Right now you just keep appending stuff to the same item without ever clearing it out. Put the Set rtitem=New NotesRichTextItem(newdoc,“Body”) within your loop and you should be fine.