Help with numbering agent

HI Guys

Well  I have inherited another mess

What I would like to do is add a unique number to documents created with the Purchase Requistion form. I will create a new number field PRNumber

This is easy enough to do when new, however there are several already created dox. Part two of this is that there are several dox created with the Purchase Order form that are related to the PR's. They are created by a button on the PR when all approvals have passed. When the PO's are created the pr_link1 is populated with the UNID of the PR.  The unfortunate side of this is that the original creater of this db did not use response dox.

There is a field on the PO's that captures the UNID of the PR,  pr_link1
There can be several PO's related to the singular PR.

I would like to have an agent that will
a) capture the UNID of the PR
b) locate that UNID in a view that contains all the UNID's that are in the pr_link1 field that match the PR UNID
c) add the new number of the PR to the pr_link1 field

I am thinking this is a two part solution
Fisrt create the PR numbers then run the other that will change the fields as outlined above

Any help or thoughts appreciated



Who is Participating?
Yes, you are missing a few things.

First off,  from this line:
    If PRNum = "" Then
... I take it that this is your permanent code for assigning PR #'s -- both "retroactive" assignment of old PR docs, and new ones as well.Yet, you have:
    PRNum = 0000
That shoudl only be done if you can't get an existing PRNum to increment!

In addition, this sequence makes no sense:

          PRNum =prdoc.PRNumber
          PRNum = 0000

... for two reasons.  prdoc.PRNumber will return an array, so you are missing prdoc.PRNumber(0).  Second, why the heck would you assign it 0 right after havinfg retrieved it?  FInally, what's the 0000 business?  Are you trying to force it to be four digits?  That won't do it, because 0000 evaluates to plain 0.  Maybe you meant it tobe a string?

Here's what Ithink you want to do.  Sort the view on PRNum, descending sort.  That will make the latest assigned PR number appear at the top, and the docs with no PR apppear at the bottom.  Now you can do this:

Dim s as new notesSession
Dim db as notesDatabase
Set db = s.currentDatabase
Dim view as notesView
Set view = db.getView("GPR")
Dim doc as notesDocument
Set doc = view.getFirstDOcument
Dim priorNumber as integer
If doc.PRNumber(0) = "" Then
    'No PR ever assigned, use prior number of 0, which it already is since it was not assigned a vlue
    priorNumber = doc.PRNumber(0)
End If

view.autoRefrehs = false 'otherwise, when we assign # and save, cur doc goes to top of view, and prev doc becomes end of view

Set doc = view.getLastDocument
Do Until doc Is Nothing
    If doc.PRNumber(0) = "" Then
        priorNumber = priorNumber + 1 'now it is "next" number
        doc.replaceItemValue "PRNumber" , Right("000" + priorNumber,4) false, false
        Set doc = view.getPreviousDocument(doc)
        'We hit a doc that was assigned a number, force processing to stop!
        Set doc = Nothing 'sort of fools the loop, as if we ran out of docs
    End If
Try this

Create a field in PO as computed with formula

@GetDocField( pr_link1; "PRNumber")

Run agent on selected documents with following command

imjameswAuthor Commented:
Here is what I have so far for creating an agent to number the existing PR;s

I created a field name PRNumber on the PR form

Am I missing anything from this agent

Dim ThisDB As NotesDatabase
Dim ThisServer As String
Dim ThisAgent As NotesAgent
Dim NewNum As Integer
Dim prdoc As NotesDocument
Dim view As NotesView

Dim Session As New NotesSession  
      Set ThisDB = session.CurrentDatabase  
      Set view = ThisDB.GetView("GPR")       
      Set prdoc = view.GetFirstDocument
      While Not (prdoc Is Nothing)       
            PRNum =prdoc.PRNumber
            PRNum = 0000
            NewNum = PRNum + 1
            If PRNum = "" Then
                  prdoc.PRNumber = NewNum
            End If
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Yes you have to set PRNumber for PR docs as you have in above agent .. And create another agent (this would be a temp agent) which will pull data in PO docs...
Next up would be to push the changes through to the PO's.  Build a view of PO's sorted by the PR doc ID # field.  Go through each PR, use its docID as a key to search that view, using getAllDocumentsByKey.  This retruns a document collection of all matching documents, and you can use NotesDocumentCOllection.stampAll to assign the PR # to the PO.

One probelm I see with this is that PR:PO is a many to many relationship in most companies.  I can have one PR that generates many POs, and one PO that came from several PRs.

SOmeone puts in a PR for all the equipment to build a server cluster.  The drives may come from one vendor while the chassis comes from another vendor.  That would be two POs for one PR.  But, if someone else happened to request a server chassis within teh same period, the purchase agnet might put both chassis on a single PO to the server vendor.
imjameswAuthor Commented:
Hi I get an error on viewAutoRefresh
Not a Member
imjameswAuthor Commented:
I am looking to modify the field PRNumber as

PR-00000001 etc....

I believe it gas to do with this line   doc.replaceItemValue "PRNumber" , Right("000" + priorNumber,4)
PRNumber is a text field

I have tried variations of this
doc.replaceItemValue "PRNumber" , Cstr(Right("000" +Cstr( priorNumber),4))
try this

doc.replaceItemValue "PRNumber" , Right("000" & priorNumber,4)
Isn't that exactly what I had put in?
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.