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?
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.

larsberntropCommented:
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

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
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: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
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.