Change main document into response document

Hello experts,
I am using the code below in an agent which I call from a button in a view after selecting a document to check and see if there are existing documents with the same lot number. If it exists it asks if I wish to switch to the existing document.

My question is this. When I switch to the existing document I want to check if it is already a response document, if it is , then nothing should happen, if it is not then I want to change it into a response to the selected document. How do I do this?


Check for duplicate lot code:

Dim workspace As New NotesUIWorkspace
      Dim uidoc As NotesUIDocument
      Dim doc As NotesDocument
      Dim curdoc As NotesDocument
      Dim db As NotesDatabase
      Dim view As NotesView          
      Dim PartNo As String
      Dim answer As Integer
      Dim codeType As String
      Set uidoc = workspace.CurrentDocument
      If uidoc.IsNewDoc Then
            Set curdoc = uidoc.Document
            Set db = curdoc.ParentDatabase
            Set view = db.GetView("BatchStatusLot")'All Fields Values sorted
            PartNo = uidoc.FieldGetText("batch") 'The FieldName you want to check
            Set doc = view.GetDocumentByKey(PartNo)
            If Not (doc Is Nothing) Then
                  Forall i In doc.Items
                        If (i.Text = PartNo) Then codeType = i.Name
                  End Forall
                  answer = Messagebox( "Duplicate " & codeType & " found. Click Ok to switch to existing document.", 1+32, "Duplicate Lot Number")
                  If (answer = 1) Then
                        curDoc.SaveOptions = "0"
                        Call uidoc.Close          
                        Delete uidoc
                        Call workspace.EditDocument(True, doc)
                  End If
            End If  
      End If      
schmad01Asked:
Who is Participating?
 
mshogrenConnect With a Mentor Commented:
The code only executes inside this if statement:

 If uidoc.IsNewDoc Then

That is, it only executes if the current doc is a new one.

I think I see now what is going on.  Some where in your code above we need to get a reference to the parent document


Dim workspace As New NotesUIWorkspace
     Dim uidoc As NotesUIDocument
     Dim doc As NotesDocument
     Dim curdoc As NotesDocument
     Dim parentdoc as NotesDocument
     Dim db As NotesDatabase
     Dim view As NotesView          
     Dim PartNo As String
     Dim answer As Integer
     Dim codeType As String
     Set uidoc = workspace.CurrentDocument
     If uidoc.IsNewDoc Then
          Set curdoc = uidoc.Document
          Set db = curdoc.ParentDatabase
          Set parentdoc = db.GetDocumentByUNID(curdoc.ParentDocumentUNID)
          Set view = db.GetView("BatchStatusLot")'All Fields Values sorted
          PartNo = uidoc.FieldGetText("batch") 'The FieldName you want to check
          Set doc = view.GetDocumentByKey(PartNo)
          If Not (doc Is Nothing) Then
               Forall i In doc.Items
                    If (i.Text = PartNo) Then codeType = i.Name
               End Forall
               answer = Messagebox( "Duplicate " & codeType & " found. Click Ok to switch to existing document.", 1+32, "Duplicate Lot Number")
               If (answer = 1) Then
                    if not(doc.IsResponse) then
                        doc.makeresponse(parentdoc)
                        doc.save true, true
                    end if
                    curDoc.SaveOptions = "0"
                    Call uidoc.Close          
                    Delete uidoc
                    Call workspace.EditDocument(True, doc)
               End If
          End If  
     End If
0
 
HemanthaKumarCommented:
Use MakeResponse Method of the NotesDocument

~Hemanth
0
 
mshogrenCommented:
Try adding the few lines as below:

Dim workspace As New NotesUIWorkspace
     Dim uidoc As NotesUIDocument
     Dim doc As NotesDocument
     Dim curdoc As NotesDocument
     Dim db As NotesDatabase
     Dim view As NotesView          
     Dim PartNo As String
     Dim answer As Integer
     Dim codeType As String
     Set uidoc = workspace.CurrentDocument
     If uidoc.IsNewDoc Then
          Set curdoc = uidoc.Document
          Set db = curdoc.ParentDatabase
          Set view = db.GetView("BatchStatusLot")'All Fields Values sorted
          PartNo = uidoc.FieldGetText("batch") 'The FieldName you want to check
          Set doc = view.GetDocumentByKey(PartNo)
          If Not (doc Is Nothing) Then
               Forall i In doc.Items
                    If (i.Text = PartNo) Then codeType = i.Name
               End Forall
               answer = Messagebox( "Duplicate " & codeType & " found. Click Ok to switch to existing document.", 1+32, "Duplicate Lot Number")
               If (answer = 1) Then
                    if not(doc.IsResponse) then
                        doc.makeresponse(curDoc)
                        doc.save true, true
                    end if
                    curDoc.SaveOptions = "0"
                    Call uidoc.Close          
                    Delete uidoc
                    Call workspace.EditDocument(True, doc)
               End If
          End If  
     End If
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.

 
schmad01Author Commented:
Getting Illegal pass by value (curDoc)
0
 
HemanthaKumarCommented:
use call doc.makeresponse(curdoc)
0
 
mshogrenCommented:
Sorry, I didn't check carefully but that is just a syntax error.  Either HemanthaKumar's
Call doc.makereponse(curdoc)
or
doc.makeresponse curdoc
will work
0
 
schmad01Author Commented:
Variant does not contain an object.
0
 
marilyngCommented:
If uidoc.IsNewDoc Then  << I think you have to save the uidoc before you can make another a response to it.
0
 
mshogrenCommented:
That raises an interesting question.  The code is preventing this new document from being saved ever, so there should be no need to make it a response to another document.

Perhaps we need to clarify the logic of what you wish to do...
0
 
marilyngCommented:
Yup, it can't be a response to a non-existant document, you'll have orphans.   You have to save the open document before you continue, or if they don't save, then exit the sub.
0
 
marilyngCommented:
How are we doing on this?  Do you know how to resolve the problem, or do you require more assistance?
0
 
schmad01Author Commented:
Sorry, got pulled away for something else. Will get back into this today and clarify.  Thanks.
0
 
schmad01Author Commented:
Ok, will try to clarify.  I have a saved document open (Yieldsheet1 form). I click a button to created a response document (scanform form) and it inherits certain fields from Yieldsheet1 document. The scenario that may arise is that there may already be a scanform created from the past. The code searches by ipl code. If it finds one of these documents , I switch to it. The problem is that it is a main document and I want to convert it into a response document to the already opened Yieldsheet1 document.  Is that any clearer?  Sometimes what is in my mind doesn't make it to the keyboard very well. Let me know if any more questions.
0
 
marilyngCommented:
I think where we left off was that your open document might be a new document, then you can't make the next document a response to the open one until you save the open document.
0
 
schmad01Author Commented:
My open document is a saved document, not a new one.
0
 
marilyngConnect With a Mentor Commented:
if it is not then I want to change it into a response to the selected document<<

Ah, then you would need to add another lookup once you have the handle on the new document.

So the flow is:

User opens a new document.
    Fills in some information
    Somewhere before the save, the agent kicks in and checks to see if this new document is a duplicate.
       if  YES - then ask to open the original
                  if YES - then while you have a handle on the original, check to see if THAT document is a response.
                       IF NO- then do a lookup to find the parent document, and before the user opens it, make it a response.
                           save the document
                           open the document and exit the code.
                       IF YES - Open the other document and discard the NEW document.
       If NO - (Use doesn't want to open the duplicate) - abyss.. ? AArrrgh. No trap or code here.

So you're going to need a function to find the Parent document as mshogren says.

Do you need further assistance to create the code?  Or will the logic suffice?
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.