Agent using formula to simply find and replace from list on multiple docs

Hi guys,

I have a categorised view
I want to allow users to select some docs and find and replace values in a field.
the field is a list and may contain one or more values.


select one or  more documents
hit action button
presented with list, lookup from view in same database
select one or cancel
presented with a 'change to' pop up string box
make change
do not allo more than 10 documents at one time (if possible)
do not present box for each document

I have so far this
list:=@Prompt([OkCancelListMult] ; "Select "; "choose" ; "" ; LOOKUP FROM VIEW; "" );
x := AFIELD;
y := @Replace(x; list;choice);
z := @Trim(y);
@SetField("AFIELD"; z);

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

mbonaciConnect With a Mentor Commented:
Here, I tested the solution and it was braking LotusScript's Replace function for no apparent reason.
So I changed it to avoid that function and simplified it in the process.

The example view is attached, but since the EE doesn't allow attaching nsf files, I've changed the extension to txt. Extract the file to Lotus\Notes\Data folder and rename extension back to nsf.
Double click the nsf, Notes opens, right click on tab and choose Open in Designer.
Then go to view - view action, there's the code. Start debugger, preview in Notes, select a couple of docs and click the action. Observe the code in action so that you can better understand what happens...

If I misunderstood something post the question...

Option Declare
Sub Click(Source As Button)
	On Error Goto ErrHandler
	Dim s As New NotesSession
	Dim w As New NotesUIWorkspace
	Dim db As NotesDatabase
	Dim c As NotesDocumentCollection
	Dim cur As NotesDocument
	Dim it As NotesItem
	Dim ret As Variant, newValue As Variant
	Const fldName = "HTTPHeaders"		'Change this to the name of the field that holds values you want to change
	Const viewName = "AllDocsNew"		'Change this to the name of the the view that displays values you want to change
	Const columnNumber = 1					'Change the number of column that displays the values
	Set db = s.CurrentDatabase
	Set c = db.UnprocessedDocuments
	If c Is Nothing Then Exit Sub
	If c.Count = 0 Then
		Msgbox "You have to select at least one document!", 16, "Warning"
		Exit Sub
	End If
	If c.Count > 10 Then
		Msgbox "The maximum number of selected documents is 10!", 16, "Warning"
		Exit Sub
	End If
	ret = dbColumn( db, viewName, Cint( columnNumber ), True, False )	'Get unique values from view's first column
	If Isempty( ret ) Then Exit Sub
	ret = w.Prompt( 7, "Select values to change", "Select one or more values to change.", ret(0), ret ) 'ask user to choose values he wants to change
	If Isempty( ret ) Then
		Messagebox "User canceled", , "No value selected"
		Exit Sub
	End If
	newValue = w.Prompt( 3, "New value", "Enter replacement value", "" )	'get new value from user
	If Isempty( newValue ) Then Exit Sub
	Set cur = c.GetFirstDocument
	Do Until cur Is Nothing
		Set it = cur.GetFirstItem( fldName )	'get the field that holds the values
		If Not it Is Nothing Then
			Forall el In ret	'Call replace sub for every string user selected to be changed
				Call ReplaceValueOfMultiItem( it, el, newValue )
			End Forall
			Call cur.Save( False, False )
		End If
		Set cur = c.GetNextDocument( cur )
	Exit Sub
	Msgbox "Error in action - Click" & Chr$(10) & "Line: " & Cstr( Erl ) & Chr$(10) & "Err No: " & Err & ": " & Error$
	Resume out	
End Sub
Function dbColumn( db As NotesDatabase, viewName As String, columnNo As Integer, flgUnique As Boolean, flgUseReplicaID As Boolean ) As Variant
	'Mbonaci, 30.08.2005.
	'Same as @DbColumn
	'Warning:		the return value is limited to 64K
	On Error Goto ErrHandler
	Dim s As New NotesSession
	Dim strDbPath As String, tmpFormula As String
	strDbPath = AdjustedFilePath( s, db )
	If flgUseReplicaID Then	
		tmpFormula = |@DbColumn( "" : "NoCache"; "| & db.ReplicaID & |"; "| & viewName & |"; | & columnNo & | )|
		tmpFormula = |@DbColumn( "" : "NoCache"; "| & db.Server & |" : "| & strDbPath & |"; "| & viewName & |"; | & columnNo & | )|		
	End If
	If flgUnique Then
		tmpFormula = |@Unique( | & tmpFormula & " )"
	End If
	dbColumn = Evaluate( tmpFormula )
	Exit Function
	Msgbox "Error in function - dbColumn" & Chr$(10) & "Line: " & Cstr( Erl ) & Chr$(10) & "Err No: " & Err & ": " & Error$
	Resume out	
End Function
Function AdjustedFilePath( s As NotesSession, db As NotesDatabase ) As String
    'Mbonaci, 19.01.2006
    'Function is used to prepare the path for LS dbColumn implementation
    'We need path relative to notes\data dir
	On Error Goto ErrHandler
	Dim datadir As String, fcPath As String, tmpPath As String
	dataDir$ = Lcase( s.GetEnvironmentString( "Directory", True ) )
	fcPath$ = Lcase( Left( db.FilePath, Len( datadir ) ) )
	If( fcPath = datadir ) Then
        'If the database is open and on the Notes workstation FilePath returns the complete path
        'for example, C:\Lotus\Notes\Data\Subdir1\Subdir2\dbName.nsf
		fcPath = Mid$( db.FilePath, Len( datadir ) + 2 )
		AdjustedFilePath = fcPath
	Elseif Instr( fcPath, "!!" ) Then
        'FilePath returns the server name (srvName!!path) when both of these cases are true:
        'FilePath is used in an agent running on a server
        'The NotesDatabase object was not obtained through the CurrentDatabase property of NotesSession
		tmpPath = db.FilePath
		AdjustedFilePath = Right( tmpPath, Len( tmpPath ) - Instr( tmpPath, "!!" ) - 1 )
        'If the database is on a Domino server, or closed on the Notes workstation
        'FilePath returns the path relative to the data directory (for example, sub\db.nsf)
		AdjustedFilePath = db.FilePath
	End If
    'Last we double all backslashes
	AdjustedFilePath = Replace( AdjustedFilePath, "\", "\\" )
	Exit Function
	Msgbox "Error in function - AdjustedFilePath" & Chr$(10) & "Line: " & Cstr( Erl ) & Chr$(10) & "Err No: " & Err & ": " & Error$
	Resume out
End Function
Sub ReplaceValueOfMultiItem( Item As NotesItem, ValueToRemove As Variant, ReplacementValue As Variant )
    'Mbonaci, 18.12.2006
    'Function replaces ValueToRemove with ReplacementValue in Item
	On Error Goto ErrHandler
	Dim ArrayV( ) As Variant
	Dim x As Integer
	Forall v In Item.Values
		Redim Preserve ArrayV( x )
		If v <> ValueToRemove Then
			ArrayV( x ) = v
			ArrayV( x ) = replacementValue
		End If
		x = x + 1
	End Forall
	Item.Values = ArrayV
	Exit Sub
	Msgbox "Error in Sub - ReplaceValueOfMultiItem" & Chr$(10) & "Line: " & Cstr( Erl ) & Chr$(10) & "Err No: " & Err & ": " & Error$
	Resume out
End Sub

Open in new window
It should work.
Did I get it right, you want to get keyword list from view lookup and then let user to replace some of the values?

In this case, multi selection is not the best option, of course, option "OkCancelList" should be better. Unless you need to replace multiple choices with one.
Then we must clear resulting list from duplicate values using  @Unique.

Agent target should be "All selected documents".

You can simplify code, specifying just:
myList:=@Prompt([OkCancelList] ; "Select "; "choose" ; "" ; LOOKUP FROM VIEW; "" );
myChoice:= @Trim(ALLOW USER TO TYPE);
FIELD AFIELD:= @Trim(@Replace(AFIELD; myList; myChoice));

Open in new window

Try this.
Create new action on your categorized view, choose Lotus Script, go to Options section of the action's code, right click and choose Import, select the file codeEE.txt, choose yes if prompted to replace existing code...

Haven't tested it, post away if something is wrong...

Hope this helps,
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

tashsmithAuthor Commented:
Hi guys,

Thanks for that, gotta run, will test and post back tomorrow:-)
tashsmithAuthor Commented:
Hi mbonaci:

I have tried your solution first as it appears to cater for more of my requirements, thanks.

On running the agent, nothing seems to be happening.

I have replaced the field and view names as requested in the markup.

There is no prompts and when the debugger is running it isnt evoking or stepping throuhg anything?
tashsmithAuthor Commented:
This works perfectly, and the instructions were concise, accurate and easy to follow.

Thanks very much
All Courses

From novice to tech pro — start learning today.