Solved

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

Posted on 2003-11-06
22
345 Views
Last Modified: 2013-12-18
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

0
Comment
Question by:imjamesw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 4
  • +1
22 Comments
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 9695182
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9695857
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9695867
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:imjamesw
ID: 9696008
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 9696058
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
 

Author Comment

by:imjamesw
ID: 9696419
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 9696477
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
 

Author Comment

by:imjamesw
ID: 9696555
Hi

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

0
 
LVL 9

Expert Comment

by:Arunkumar
ID: 9696601
check if the view got set....do you have a view named AH ??
0
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 9696604

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
 

Author Comment

by:imjamesw
ID: 9696610
Yes
0
 

Author Comment

by:imjamesw
ID: 9696645
Illegal use of property
error

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

Expert Comment

by:HemanthaKumar
ID: 9696843
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
 

Author Comment

by:imjamesw
ID: 9696996
Son of a gun type mismatch error on

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

0
 

Author Comment

by:imjamesw
ID: 9696997
Son of a gun type mismatch error on

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

0
 
LVL 9

Expert Comment

by:Arunkumar
ID: 9697029
Try this...

doc.TotalAH = cint(note.TotalAH(0)) - Cint(note.TotalDaysOff(0))
0
 
LVL 24

Accepted Solution

by:
HemanthaKumar earned 250 total points
ID: 9697059
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
 
LVL 31

Expert Comment

by:qwaletee
ID: 9698241
So, after all this back and forth, you almost arrive at my solution, eh?

heh heh
0
 

Author Comment

by:imjamesw
ID: 9700998
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
 
LVL 9

Expert Comment

by:Arunkumar
ID: 9701492
Money spent by your company ?  WoW....

Why dont you give me some free pts just to celebrate this ?
0
 

Author Comment

by:imjamesw
ID: 9702475
I tried to split the points did not see the split ponts button
0
 
LVL 9

Expert Comment

by:Arunkumar
ID: 9703210
well...you can give me pts on a separate question.  yes qwaletee ?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  In today’s Arena we can’t imagine our lives without Internet as we are highly used to of it. If we consider our life style just for only 2 min we found that face to face communication is swapped by e-communication.  Every Where from Works place to…
IBM Notes offer Encryption feature using which the user can secure its NSF emails or entire database easily. In this section we will discuss about the process to Encrypt Incoming and Outgoing Mails in depth.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question