Link to home
Start Free TrialLog in
Avatar of macjacinto
macjacinto

asked on

Document Save Conflict

I have a main form with a Save and Close button and a Create Details button. The Details form inherits docid in the main form. The user, after filling up the main form, clicks the Create Details button. (see below the code i placed in the buttons). The Details form will open, and the user will fill it up, during saving of the details form, it will change some of the fields in the main form so the user has to click the save and click button on the main form after saving the details form. Another thing, i also added a code in the post save of  the details form. When the user clicks the Save and Close button in the main form the following message appears "Another copy of this document was saved while you were editing it. Save your changes also as a save conflict document?"

This is kindda urgent guys. Hoping for your prompt reply. Thanks in advance! :)

Codes:

Save and Close button in Main Form:
@Command([EditDocument];"1") ;
@Command([ViewRefreshFields]) ;
@Command([FileSave]);
@Command([FileCloseWindow])

Create Detail Button in Main Form:
@Command([FileSave]);
@Command([Compose];"details")

Save and Close button in Details Form:
@SetDocField(maindocid;"lastupdate";cpdate);
tempo :=@GetDocField(maindocid; "ctimeo");
@If (tempo= "";@SetDocField(maindocid;"ctimeo";timeo);"");
@If(@Trim(status) = "CLOSED"; @SetDocField(maindocid;"closedtime";cpdate);"");
@Command([FileSave]);
@Command( [FileCloseWindow] )

Post Save event in Details Form:
Sub Postsave(Source As Notesuidocument)
      Dim w As New NotesUiWorkspace
      Dim s As New NotesSession      
      Dim dbCurrent As NotesDatabase
      Dim viewDailyColl As NotesView
      Dim entryDailyColl As NotesViewEntry
      Dim vc As NotesViewEntryCollection
      Dim docDailyColl As NotesDocument
      Dim docTMP As NotesuiDocument
      Dim m As String
      
      Set dbCurrent = s.CurrentDatabase
      Set docTMP = w.currentdocument
      
      m = docTMP.FieldGetText( "maindocid" )
      
      Set viewDailyColl = dbCurrent.getView("Status")
      Set vc = viewDailyColl.GetAllEntriesByKey(m)
      If vc.count = 0 Then
            Messagebox("Please create at least 1 detail of the case")
            Exit Sub
      End If
      
      For i = 1 To vc.count            
            
            Set entryDailyColl = vc.GetNthEntry(i)            
            Set docDailyColl = entryDailyColl.Document            
            docDailyColl.dstat = docTMP.FieldGetText( "status" )
            Call docDailyColl.Save(True,False)      
            
      Next
End Sub
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

I assume you keep the main form open in edit-mode while details are filled in. I'd suggest to make the Details button visible only in read-mode, thus preventing multiple simultaneous updates on the main form. You can also put the document in read-mode when pressing the Details button. If you want to modify values on the main-form from the details-form, you can do that safely, but the info on the main-form on screen will not be changed until a reload.
Exactly!
Is there a way to make "dstat" computed for diaplay and let it show the value of "status" dynamically (using e.g. @DbLookup)?
(note that if a document already contains a field dstat, the computed for display result won't show; the available field will show instead!)
Avatar of HemanthaKumar
HemanthaKumar

Instead of executing the script in PostSave, execute it in QuerySave.. This should prevent the conflicts.

~Hemanth
Right now, you have the CREATE DETAILS button save the main document and create a detail doc.

Instead, have it save AND CLOSE the main document, so that it is not open at all.  Some others have suggested that yuo put teh main document in read mode.  That will sometimes be good enough.  But if the user siwtches the main docuemnt back to edit mdoe from read mode, the same conflict action will occur, because the "old state" of teh document is cahced even in red mode, and will not be refreshed when swicthin back to edit,  Because of that, the open window has the old doc information (before the details form modified it), and upon save, it will notice the conflict problem.

How to make this work?  Put the following code in CREATE DETAILS:
@PostedCommand([FileSave]);
@PostedCommand([Compose]; "details")
@PostedCommand([FileOpenDatabase]; @DbName; ""; "1");
@PostedCommand([OpenDocument]; "0"; @Text(@DocumentUniqueID));
@PostedCOmmand([FileCloseWIndow]);
@PostedCOmmand([FileCloseWIndow]);
Avatar of macjacinto

ASKER

sjef_bosman:

If i make the Details button visible in read-mode only, the user has to click the save and close first, then open the document again just to create a detail form? And also, how can put the main document in read mode when pressing the Details button?

crak:

Its possible to make dstat a computed for display field and get its value through dblookup..however, the main function of the post save code is to change all values of dstat in all details doc of one main document, you see, its possible that in one main doc, multiple details doc were created, and i used the docid of the main doc to keep tab of all the details of the said main doc. How will i then incorporate this function if i get the value through dblookup?

hemanth:

The message from the script appeared " Please create at least 1 detail of the case". I think this is because the document hasn't been saved yet bec as the definition of Query save in help - it "Occurs just before the current document is saved"

qwaletee:

The message "Server error: Entry not found in index" appeared


I'll provide additional points to whoever solves this problem today. Thanks!!
let me guess what ur doing.

U r creating a main form and once saved, some of the data is fetched into a new form called details form.

In this details form, once its is saved, few data should be updated in the main form which was created before.

Am I correct or missing some thing?
yes madheeswar, that's correct. as ive' said in my first post, some of the fields in the details form are inherited from the main form
Since what I said is correct, then I will expain my logic on how to complete this.

On ur main form, capture document unique id(say first 10 chars).
Inherit what ever the value u want into Details form, including unique id.

While u compose details form, close main form. Since we captured doc., unique  id, look for that document using L.Script and update the fields which ever u want.

If the logic is OK with u, I can provide solution by today itself.

waiting for ur reply.
the code as it is now can already capture the doc id of the main form and pass it to the details doc.

the logic is ok, i can try the solution u will be providing. tnx
Button on Main form:
@Command([FileSave]);@Command([FileCloseWindow]);@PostedCommand([Compose];"DetailsForm")

Now, For save&Close in Detials Form:.

Sub Click(Source As Button)
      'http://oldlook.experts-exchange.com/questions/20813669/Document-Save-Conflict.html
      Dim ws As New notesuiworkspace
      Dim uidoc As notesuidocument
      Dim curdoc As notesdocument
      
      Set uidoc=ws.currentdocument
      Set curdoc=uidoc.document
      
      Dim ss As New notessession
      Dim db As notesdatabase
      Dim view As notesview
      Dim dc As notesdocumentcollection
      Dim doc As notesdocument
      
      Set db=ss.currentdatabase
      Set view=db.getview("ViewNamehavingdocsofMainform")
      Dim tmpkey As String
      tmpkey=curdoc.UniquekeyFieldname(0)
      Set doc=view.getdocumentbykey(tmpkey,True)
      If Not doc Is Nothing Then
            doc.urfieldsto update=curdoc.fieldtoupload(0)
            'ur fields will follow
            Call doc.save(True,True)
      End If
      Call curdoc.save(True,True)
      Call uidoc.close
End Sub

Please let me know if it is working or not.
the above works only to update main document created by main form.

So, each document created by main form will have unique id's.So, documents created using these unique id's will be updated.

if u r not clear what it does, let me know.

Please test it fast and let me know.
-Thanks.
upon save and close of the main form..the message "Field 'casenum' : Invalid UNID; UNID must be a 32-digit hex string. Field casenum in Details form is a computed field. It gets its value using the code @GetDocField(maindocid ; "casenum" )
what is the code for maindocid?

U can use unique id or a part of unique id like:

@Left(uniqueid;10)
maindocid is computed when composed field. its value is docid. w/c is a field in main form. details form "formulas inherit values from selected document" option is enabled. docid field's code in main form is @DocumentUniqueID
in the main form for maindocid is correct

for details form field casenum should be computedwhen composed. And the defualt value should be maindocid

this will automatically fetches values from mainform.

And test my code and let me know.

Note: for mainform doc's., first column should be sorted and the formula for column should be @text(maindocid)

and in my code change to
set doc=view.getdocumentbykey(cstr(tmkey),true)

do it fast and let me know as I will stay for another 2 hours.
maindocid field in details form is empty.

i changed the casenum's code to docid, and it turned out empty as well. also, the value of the casenum field in the details form should be the value of the casenum form in the main form, that's why i used the code @GetDocField(maindocid ; "casenum" )

when i clicked save and close, the message "do u want to save this form" appeared

and since i have a post save code in my details form, another message " object variable not set" appeared. when i debugged it, it stopped at the if vc.count = 0 line

gosh...things got a little complicated
send me ur template to reddyatstoneforest.com.sg

ill send it now...thanks
u have wasted one hour time.
I will stay another 45 mintutes. Within that time, if I solve it, I will send, else u need to wait till tomorrow.
i sent it to reddy@stoneforest.com.sg?...is that correct?
Goodness gracious me! What's wrong with the original solution? I wrote a second possibility: "You can also put the document in read-mode when pressing the Details button. ". See also the first comment of qwaletee. There's nothing wrong with the general direction of your original solution, so why investigation alternatives?
i received it
i posted a comment after reading the suggestions u made, here it is again.

sjef_bosman:

If i make the Details button visible in read-mode only, the user has to click the save and close first, then open the document again just to create a detail form? And also, how can put the main document in read mode when pressing the Details button?

crak:

Its possible to make dstat a computed for display field and get its value through dblookup..however, the main function of the post save code is to change all values of dstat in all details doc of one main document, you see, its possible that in one main doc, multiple details doc were created, and i used the docid of the main doc to keep tab of all the details of the said main doc. How will i then incorporate this function if i get the value through dblookup?

hemanth:

The message from the script appeared " Please create at least 1 detail of the case". I think this is because the document hasn't been saved yet bec as the definition of Query save in help - it "Occurs just before the current document is saved"

qwaletee:

The message "Server error: Entry not found in index" appeared

WHAT i FOUND TILL now is u have some bugs in ur save&close button in details form. So, paste this code instead of urs.
Sub Click(Source As Button)
      
      Dim ws As New notesuiworkspace
      Dim uidoc As notesuidocument
      Dim curdoc As notesdocument
      
      Set uidoc=ws.currentdocument
      Set curdoc=uidoc.document
      
      Dim ss As New notessession
      Dim db As notesdatabase
      Dim view As notesview
      Dim dc As notesdocumentcollection
      Dim doc As notesdocument
      
      Set db=ss.currentdatabase
      Set view=db.getview("MainDocs")
      Dim tmpkey As String
      tmpkey=curdoc.maindocid(0)
'      Set doc=view.getdocumentbykey(tmpkey,True)
      Set doc=view.getdocumentbykey(Cstr(tmpkey),True)
      If Not doc Is Nothing Then
            doc.lastupdate=curdoc.cpdate(0)
            If doc.ctimeo(0)= "" Then
                  doc.ctimeo=curdoc.timeo(0)      
            End If
            If curdoc.status (0) = "CLOSED" Then
                  doc.closedtime=curdoc.cpdate(0)      
            End If
            
          'ur fields will follow
            Call doc.save(True,True)
      End If
      Call curdoc.save(True,True)
      Call uidoc.close
      
End Sub

This should be working now. check it  and let us know.
That's what I responded to: use qwaletee's and my suggestion, so
- display Details all the time
- prevent the main document from being modified when you edit the details, i.e.
   - close the main document first before filling in the Details form
   - or reopen the main document in read-only mode (as qwaletee suggests)

A slightly modified copy of qwaletee's code (without permission from the original editor ;-):

The general idea is to put the following code in CREATE DETAILS:
@PostedCommand([FileSave]);
@PostedCommand([Compose]; "details")
@PostedCommand([FileCloseWindow]);

The two lines removed used to reopen the current document in read-only mode. You can alway put them back:
@PostedCommand([FileOpenDatabase]; @DbName; ""; "1");
@PostedCommand([OpenDocument]; "0"; @Text(@DocumentUniqueID));

mac,
what is the result?
sjef_bosman:

i tried using the code w/o the last 2 lines but when i clicked the button, the details form did not appear and the current form (main form) was not closed

madhesswar:

i also tried the code u gave but m still having the same problem. can u just resend to me the database u already edited? my email address e macjacinto@yahoo.com



ill be waiting for your comments guys. thanks
I have sent, check it
ASKER CERTIFIED SOLUTION
Avatar of qwaletee
qwaletee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Qwaletee,
I opened a question, please try to answer it.

-Thanks.
qwaletee:

i used the code u gave, it saved but i cant see it in my all cases view, (view selection "uses forms Details & New Cases"(New Cases is the main form))
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what should be the value of casenum field in the details form?
casenum. this field is fetched from new details form automatically.

U r view is corrupted. Just now I have created a new view with both forms included. Its working fine,

I found the error. It is in the Resolution Time column. Once u remove it, it should be ok.
the maindocid and casenum's value in details form are still empty

can u pls send the dbase w/ not empty casenum?

ill wait. tnx
@Return should not be used in column formulas.

The above is from help.

U have used @Return and that code looks like it has lots of bugs. What u want actually in that column?

This is becoming another question. Its better u post another question with the code which u have it in the column.

its not fair for me to solve without other experts approval as it is different problem from this question.

don't take it too personal.
I have sent the db. Check it and let us know.