update a field on a document with the value from a different doc

Hi

I need to simply update a field on a document with the value from a different doc. These docs are created with different forms and do not have Parent/Response relationship

I have searched around the net but have yet to find a solution

The only criteria is that form one Status field must = approved
The field names are the same TotalDaysOff

I tried @DocSetField and no go
I tried the following:

And I get an Not member Current Database error
on the set db = session.CurrentDatabase

Any help apprecited

Dim session as New NotesDatabase
Dim db as NotesDatabase
dim doc as NotesDocument
Dim view as NotesView
set db = session.CurrentDatabase
Set view = db.GetView( "AH")
set doc = view.GetDocumentByKey(doc.RequesterName(0))
if not doc is nothing then
   doc.TotalDaysOff = doc.TotalDaysOff(0)
   doc.save true, false
else
   msgbox "No leave document found for this employee"
end if

imjameswAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

HemanthaKumarCommented:
here is the corrected version

Dim session as New NOTESSESSION
Dim db as NotesDatabase
dim doc as NotesDocument
Dim view as NotesView
set db = session.CurrentDatabase
Set view = db.GetView( "AH")
set doc = view.GetDocumentByKey(doc.RequesterName(0))
if not doc is nothing then
   doc.TotalDaysOff = doc.TotalDaysOff(0)
   doc.save true, false
else
   msgbox "No leave document found for this employee"
end if
 
 ~Hemanth
0
qwaleteeCommented:
Whoa!  That is one truly fruly messed up script.

As Hemantha pointed out, you create session as a NotesDatabase (I'm surpirsed it let you save -- NotesDatabase's New takes two parameters).

But you also have a fundamental logic error.

You declare doc: dim doc as NotesDocument

The next time you use doc, its is an assignment: set doc = view.GetDocumentByKey(*******)

But look at what's inside the assignment: doc.RequesterName(0)

You are trying to retrieve by key, and the key vale you are using is a field on a document, but the doc variable was never assigned yet.  It ***WOULD*** be assigned AFTER this statemnet, if the assign were successful, but in this case, it will faile with an OBJECT VARIABLE NOT SET error.


Next problem: if not doc is nothing then
Becasue of LotusScript order of preference rules, this MIGHT be interpreted as: if ((not doc) is nothing) then
What you want is: if (not (doc is nothing)) then
Or: if not (doc is nothing) then
Otherwise, you will get some sort of type compatibility error, since you cannot NOT a NotesDocument object. I know this problem occured regularly in earlier versions of LotusScript, it might have been changed later.


So, here's what I think you really wanted to do -- you need two documents.  These are the "leave" document and the (I assume) "employee" document.  It isn't clear which document you want tolook up, and which document you want to modify. I assumed here that you want to look up a value from the leave doc and bring it back to the employee doc, as opposed to finding the leave doc in order to push the employee doc value back out onto the leave doc.

Dim employeeDoc as NotesDocument
Set employeeDoc = .... -- something or other, perhaps DocumentContext, perhaps uiDoc.document, we can discss this

Dim session as New NotesDatabase
Dim db as NotesDatabase
dim leaveDoc as NotesDocument
Dim view as NotesView
set db = session.CurrentDatabase
Set view = db.GetView( "AH")
set leaveDoc = view.GetDocumentByKey(employeeDoc.RequesterName(0))
if leaveDoc  is nothing then
   msgbox "No leave document found for this employee"
else
   employeeDoc.TotalDaysOff = leaveDoc.TotalDaysOff(0) 'this might be backward -- switch teh two docs, and change the next line similarly
   employeeDoc.save true, false
end if


You could also do this in formula, too.
result := @DbLookup(""; ""; "AH"; RequesterName; "TotalDaysOff");
@If(@IsError(result); @Return(@Prompt([ok]; ""; "No leave document found for this employee")); CONTINUE);
FIELD TotalDaysOff := result;

That's if you want to retrieve a value from the leave doc and put it on the emplyee doc.  The other way around is possible, but more work.  Add a column to the AH view, with a formla of @Text(@DocumentUniqueID) -- then you can do this as follows:

UNID := @DbLookup(""; ""; "AH"; RequesterName; new_column_number_which_you_should_set_here);
@If(@IsError(result); @Return(@Prompt([ok]; ""; "No leave document found for this employee")); CONTINUE);
@SetDocField(UNID; "TotalDaysOff"; TotalDaysOff);
0
qwaleteeCommented:
So, in summary:

1) SOme fundamental flaws with the script, ecause it doesn't really recognize that it is dealing with two documents, and thus uses one variable incorrectly for both

2) Possible solution to script issue provided

3) Formula alternative provided

4) Need to pay attention to which direction data should flow, as it was not clear from original question
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

imjameswAuthor Commented:
Hi

The original script with a minor change camr hemanthakumar in

http://www.experts-exchange.com/Applications/Email/Lotus_Notes/Q_20786162.html.



What i really want this to do is

When an employee puts thru a leave request and it is approved then the request form will update the tracking form so the employee can track how many days leave he has available within the year.


Thanx for your explanation
0
HemanthaKumarCommented:
My mistake, sorry for that typo.

In your case you are working on current document so you have to pass the key value like this

Dim ws as New NotesUIWorkspace
Dim uidoc as NotesUIDocument
Dim session as New NotesSession
Dim db as NotesDatabase
dim doc as NotesDocument
Dim view as NotesView
set uidoc = ws.CurrentDocument
set db = session.CurrentDatabase
Set view = db.GetView( "AH")
set doc = view.GetDocumentByKey(uidoc.Document.RequesterName(0)) ' KEY Value passed to view
if not doc is nothing then
   doc.TotalDaysOff = doc.TotalDaysOff(0)
   doc.save true, false
else
   msgbox "No leave document found for this employee"
end if
0
imjameswAuthor Commented:
Thats Ok I appreciate your help

As a side question I would like to maybe expand the above to updating other fields.

THe values would be mathematically calculated something like

doc.TotalAH(0) = TotalAH - TotalDaysOff etc...


I am thinking this is the better way to go rather than try to program for every eventuallity. I would make the fields on the Tracking form computed and then they would be updated only when the conditions are met.

James
0
HemanthaKumarCommented:
To accomodate it use this script

Dim ws as New NotesUIWorkspace
Dim session as New NotesSession
Dim db as NotesDatabase
dim doc as NotesDocument, note as notesdocument
Dim view as NotesView
set note = ws.CurrentDocument.Document
set db = session.CurrentDatabase
Set view = db.GetView( "AH")
set doc = view.GetDocumentByKey(note.RequesterName(0)) ' KEY Value passed to view
if not doc is nothing then
   doc.TotalDaysOff = doc.TotalDaysOff(0)
   doc.TotalAH(0) = note.TotalAH(0) - note.TotalDaysOff(0)  ' Provide the field here are number type
   doc.ComputeWithForm true, false  ' This line will help you to calculate any other numeric calculcations setup in leave document but slows down the performance
   doc.save true, false
else
   msgbox "No leave document found for this employee"
end if
0
imjameswAuthor Commented:
Hi

I get object variable not set on the set doc = view.GetDocumentByKey(note.RequesterName(0)) ' KEY Value passed to view

0
ArunkumarCommented:
check if the view got set....do you have a view named AH ??
0
HemanthaKumarCommented:

Let me clarify how you setup this script.. Is this run from an open document ? If so where is this script run ... from a button

Does the view AH exists in the db.. to make it more failproof let us include some checks in the script

replace this line
set doc = view.GetDocumentByKey(note.RequesterName(0)) ' KEY Value passed to view

with this script block

if view is nothing then
  Msgbox "Cannot find view"
Exit sub
End if
if note.HasItem("RequesterName") then
  set doc = view.GetDocumentByKey(note.RequesterName(0)) ' KEY Value passed to view
ENd if
0
imjameswAuthor Commented:
Yes
0
imjameswAuthor Commented:
Illegal use of property
error

on  
 doc.TotalAH(0) = note.TotalAH(0) - note.TotalDaysOff(0)  ' Provide the field here are number type
0
HemanthaKumarCommented:
Oops I copied it from your comment, change it like this

doc.TotalAH = note.TotalAH(0) - note.TotalDaysOff(0)  ' Provide the field here are number type
0
imjameswAuthor Commented:
Son of a gun type mismatch error on

doc.TotalAH = note.TotalAH(0) - note.TotalDaysOff(0)  ' Provide the field here are number type

0
imjameswAuthor Commented:
Son of a gun type mismatch error on

doc.TotalAH = note.TotalAH(0) - note.TotalDaysOff(0)  ' Provide the field here are number type

0
ArunkumarCommented:
Try this...

doc.TotalAH = cint(note.TotalAH(0)) - Cint(note.TotalDaysOff(0))
0
HemanthaKumarCommented:
That is why I put a comment on the side saying that is true only for number type field or else you have to convert it to int and then process it.

Give me the data types for these fields

TotalAH in Doc
TotalAH in note
TotralDaysOff in note

and I will give you the correct formula
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
qwaleteeCommented:
So, after all this back and forth, you almost arrive at my solution, eh?

heh heh
0
imjameswAuthor Commented:
I have fixed it mayself
What was happening is that there is no Total AH field on the Leave Request so I re wrote it to look like this

doc.TotalAH = doc.TotalAH(0) - note.TotalDaysOff(0)

Thanx for all you assitance lads


Also a side note I have been authorized to buy a year subscription premium to the site. LOTS OF POINTS TO SPREAD AROUND.

Cheers James
0
ArunkumarCommented:
Money spent by your company ?  WoW....

Why dont you give me some free pts just to celebrate this ?
0
imjameswAuthor Commented:
I tried to split the points did not see the split ponts button
0
ArunkumarCommented:
well...you can give me pts on a separate question.  yes qwaletee ?
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.