Help with a formatting agent.

Hoping this might be a quick fix. I have a DB that has about 5000 records. For these I need to format the Agency field to be 3 characters with leading zeroes if needed. The agent I have is getting hung up. I am hoping that it can be rewritten to run just on the tagged records I have. I think one issue here is that it is running on all the records in the Agency view. How can this be tweaked to run just on the records I have tagged. It would not be in the Agency view. I have the main records in another view.

Sub Initialize
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim view As NotesView
	Dim doc As NotesDocument
	
	Set db = session.currentdatabase
	Set view = db.getview("Agency")
	Set doc = view.GetFirstDocument
	
	While Not Doc Is Nothing
		Call doc.ReplaceItemValue( "Agency", Format( doc.Agency(0), "000") )
		Call Doc.Save(True,False)
		Set doc = view.GetNextDocument(doc)
	Wend
	
End Sub

Open in new window

jforget1Asked:
Who is Participating?
 
larsberntropConnect With a Mentor Commented:
Update all if not compliant:

Sub Initialize
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim view As NotesView
	Dim doc As NotesDocument, docNext as NotesDocument
	Dim agStr$, agNorm$
	
	Set db = session.CurrentDatabase
	Set view = db.GetView("Agency")
	view.AutoUpdate = False
	Set doc = view.GetFirstDocument
	
	Do Until Doc Is Nothing
		Set docNext = view.GetNextDocument(doc)
		agStr = Cstr(doc.GetItemValue("Agency")(0))
		agNorm = Right$("000" & agStr, 3)
		If agStr <> agNorm Then
			Call doc.ReplaceItemValue( "Agency", Format$(doc.Agency(0), "000"))
			Call Doc.Save(True, False)
		End If
		Set doc = docNext
	Loop
	
End Sub

Open in new window


If you want to process only selected documents: make sure to set the target in the agent properties to 'All selected documents'
Sub Initialize
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim dc As NotesDocumentCollection
	Dim doc As NotesDocument, docNext as NotesDocument
	Dim agStr$, agNorm$
	
	Set db = session.CurrentDatabase
	Set dc = db.UnprocessedDocuments
	Set doc = dc.GetFirstDocument
	
	Do Until Doc Is Nothing
		Set docNext = dc.GetNextDocument(doc)
		agStr = Cstr(doc.GetItemValue("Agency")(0))
		agNorm = Right$("000" & agStr, 3)
		If agStr <> agNorm Then
			Call doc.ReplaceItemValue( "Agency", Format$(doc.Agency(0), "000"))
			Call Doc.Save(True, False)
		End If
		Set doc = docNext
	Loop
End Sub

Open in new window

0
 
Sjef BosmanGroupware ConsultantCommented:
Two ways:
1. you run the agent based on the unprocessed documents, instead of the view:
Sub Initialize
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim dc As NotesDocumentCollection
	Dim doc As NotesDocument
	
	Set db = session.currentdatabase
	Set dc= db.unprocessedDocuments
	Set doc = dc.GetFirstDocument
	
	While Not Doc Is Nothing
		Call doc.ReplaceItemValue( "Agency", Format( doc.Agency(0), "000") )
		Call Doc.Save(True,False)
		Set doc = dc.GetNextDocument(doc)
	Wend
	
End Sub

Open in new window


or you use a one-line Formula agent that runs on selected documents:
Field Agency:= @Right("000"+@Text(Agency);"3")

Open in new window

0
 
larsberntropCommented:
Yeah, both will solve the problem.  I tend to try not to save when not needed though.  Avoids being a resource hog.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Sjef BosmanGroupware ConsultantCommented:
I don't know, but one of my friends found out that Notes doesn't save when nothing has changed. Not quite sure if it applies here.
0
 
Sjef BosmanGroupware ConsultantCommented:
Plus, for a one-shot agent, who cares...
0
 
jforget1Author Commented:
Appreciate the help with this one.
0
 
Sjef BosmanGroupware ConsultantCommented:
Appreciate the points for the one-liner... ;-)
0
 
jforget1Author Commented:
Well I tired the one liner first and for some reason it clear out the Agency value for all the records tagged. I was able to correct as I had another field I could reference, but something was a little off with that one.
0
 
Sjef BosmanGroupware ConsultantCommented:
Correct! I'm a fool, what a silly mistake... Here the corrected line:
Field Agency:= @Right("000"+@Text(Agency); 3)

Open in new window

0
 
jforget1Author Commented:
Damn those silly quotes. I see that looks like the only difference. As always I really do appreciate all the help on these issues. We are moving to Outlook/Sharepoint very soon, so these are hopefully my final Notes questions I will need to ask.
0
 
jforget1Author Commented:
This one is not our call, this is at a much higher level in the company, I am just along for the ride.
0
 
jforget1Author Commented:
sjef,

Any change you could take a peak at the other little issue I have at this point. Having trouble getting the hides working properly on a form.
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/Lotus_SmartSuite/Lotus_Notes/Q_27671056.html
0
 
larsberntropCommented:
Heh, oneliners that kill off data. Still calling me fool for using LotusScript on a test db?
0
 
jforget1Author Commented:
Figured out the syntax on the other issue I had.
0
 
Sjef BosmanGroupware ConsultantCommented:
@Lars: where did I call you a fool??

IMHO, the "art" of using Formula language is grossly underrated... Instead of 24 lines, only 1...
0
 
larsberntropCommented:
Nowhere.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.