Solved

Compose new document with values from selected doc using LotusScript

Posted on 2014-03-25
36
250 Views
Last Modified: 2016-01-04
I have a CRM database with a contact form. On the form is a button with LS to create a new Activity and this works fine except that the users always have to open the contact to run this. I would like to attach the LS to an action button in the Contacts view so that the user can create a new activity for the selected contact.
The script that works in the form is

Sub Click(Source As Button)
      Dim ns As NotesSession
      Dim db As NotesDatabase
      Dim tmpdoc As NotesDocument
      Dim doc As NotesDocument      
      Set ns= New NotesSession
      Set db= ns.Currentdatabase
      Set tmpdoc= New NotesDocument (db)
      Set ws = New NotesUIWorkspace
      Set uidoc = ws.CurrentDocument
      Set doc = uidoc.Document
      
      ' Fill tmpdoc fields with values from the document
      tmpdoc.CoUNID = doc.CoUNID
      tmpdoc.CoName = doc.CoName
      
      tmpdoc.CoChainName=doc.CoChainName
      tmpdoc.CoAccountType=doc.CoAccountType
      tmpdoc.ConSource=doc.ConSource
      tmpdoc.ConUNID = doc.ConUNID
      tmpdoc.ConStatus = doc.ConStatus
      tmpdoc.SourceForm = "Contact"
      tmpdoc.ConSalesperson= doc.ConSalesperson
      tmpdoc.ConInterests= doc.ConInterests
      tmpdoc.CoDivision = doc.CoDivision
      tmpdoc.ConFullName = doc.ConFirstName(0) & " " & doc.ConLastName(0)
      tmpdoc.ConBranch = doc.ConBranch
      tmpdoc.ConPhone = doc.ConPhone
      tmpdoc.ConMobile = doc.ConMobile
      tmpdoc.ConEmail = doc.ConEmail
      
      'Resizes the window to fit the table on the form
      If ws.Dialogbox("Activity", True, True, False, False, False, False, "New Activity for " & doc.ConFirstName(0) & " " & doc.ConLastName(0) , tmpdoc, True,True,True) Then
      End If
      Call uidoc.RefreshHideFormulas
End Sub

When I attach this to an action I get "Object Variable Not Set"
What am I missing?
Thanks
0
Comment
Question by:kcoxon
  • 18
  • 17
36 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 39953925
My guess: the uidoc is Nothing, because there is no current document when you're in the view. You have to get the currently selected document in the view differently, i.e. using the view.UnprocessedDocuments property (or is it db.UnprocessedDocuments, I always forget and I don't have the Help files immediately available). Check the help for that property, there are some good examples with it.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 39953929
You could also try to use the LotusScript debugger...
0
 
LVL 2

Expert Comment

by:jmaritz
ID: 39955296
Yes, uidoc is not available in your code, but you do not need it, only need to pick it up and then work with doc
      Dim ns As NotesSession
      Dim db As NotesDatabase
      Dim tmpdoc As NotesDocument
      Dim doc As NotesDocument      
      ' Add document collection
      Dim col As NotesDocumentCollection
     
      Set ns= New NotesSession
      Set db= ns.Currentdatabase
      Set tmpdoc= New NotesDocument (db)
      Set ws = New NotesUIWorkspace
      ' Set document collection
      Set col = db.UnprocessedDocuments
      For a = 1 To col.Count
            'Get the document to get values from
            Set doc = col.GetNthDocument(a)
           
            '  rest of the code  .........
      Next
0
 

Author Comment

by:kcoxon
ID: 39955393
Thanks for your response.
I still get "Object Variable Not Set"

Sub Click(Source As Button)
      Dim ns As NotesSession
      Dim db As NotesDatabase
      Dim tmpdoc As NotesDocument
      Dim doc As NotesDocument      
      ' Add document collection
      Dim col As NotesDocumentCollection
      
      Set ns= New NotesSession
      Set db= ns.Currentdatabase
      Set tmpdoc= New NotesDocument (db)
      Set ws = New NotesUIWorkspace
       ' Set document collection
      Set col = db.UnprocessedDocuments
      For a = 1 To col.Count
            'Get the document to get values from
            Set doc = col.GetNthDocument(a)
            
            Set uidoc = ws.CurrentDocument
            Set doc = uidoc.Document
            
      ' Fill tmpdoc fields with values from the document
            tmpdoc.CoUNID = doc.CoUNID
            tmpdoc.CoName = doc.CoName
            tmpdoc.CoChainName=doc.CoChainName
            tmpdoc.CoAccountType=doc.CoAccountType
            tmpdoc.ConSource=doc.ConSource
            tmpdoc.ConUNID = doc.ConUNID
            tmpdoc.ConStatus = doc.ConStatus
            tmpdoc.SourceForm = "Contact"
            tmpdoc.ConSalesperson= doc.ConSalesperson
            tmpdoc.ConInterests= doc.ConInterests
            tmpdoc.CoDivision = doc.CoDivision
            tmpdoc.ConFullName = doc.ConFirstName(0) & " " & doc.ConLastName(0)
            tmpdoc.ConBranch = doc.ConBranch
            tmpdoc.ConPhone = doc.ConPhone
            tmpdoc.ConMobile = doc.ConMobile
            tmpdoc.ConEmail = doc.ConEmail
            
      'Resizes the window to fit the table on the form
            If ws.Dialogbox("Activity", True, True, False, False, False, False, "New Activity for " & doc.ConFirstName(0) & " " & doc.ConLastName(0) , tmpdoc, True,True,True) Then
            End If
            Call uidoc.RefreshHideFormulas
      Next
End Sub
0
 

Author Comment

by:kcoxon
ID: 41372954
I've requested that this question be deleted for the following reason:

Never found solution, continued to create document from the form.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41372521
You should have asked a wee bit earlier for our attention... Sorry that I abandoned this question. I suppose you're no longer interested in its resolution?
0
 

Author Comment

by:kcoxon
ID: 41372853
Sjef, Just doing a tidy up.
You know how it is, things move on and drop down the list.
Yes it would still be useful for users to do this from the view rather than opening the form first. Saved mouse clicks are always good.
Focused on the other issue I raised last night at present but would interested in pursuing this one too.


Cheers,

Kevin
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 500 total points
ID: 41372942
Ok, let's continue then. :-)

Suggested code:
Sub Click(Source As Button)
      Dim ns As NotesSession
      Dim db As NotesDatabase
      Dim tmpdoc As NotesDocument
      Dim doc As NotesDocument      
      ' Add document collection
      Dim col As NotesDocumentCollection
      
      Set ns= New NotesSession
      Set db= ns.Currentdatabase
      Set ws = New NotesUIWorkspace
       ' Set document collection
      Set col = db.UnprocessedDocuments
      If col.Count<>1 Then
            MsgBox "Please select (only) one document",16,"Activity"
            Exit Sub
      End If
      Set tmpdoc= New NotesDocument (db)

            'Get the document to get values from
            Set doc = col.GetFirstDocument
                     
      ' Fill tmpdoc fields with values from the document
            tmpdoc.CoUNID = doc.CoUNID
            tmpdoc.CoName = doc.CoName
            tmpdoc.CoChainName=doc.CoChainName
            tmpdoc.CoAccountType=doc.CoAccountType
            tmpdoc.ConSource=doc.ConSource
            tmpdoc.ConUNID = doc.ConUNID
            tmpdoc.ConStatus = doc.ConStatus
            tmpdoc.SourceForm = "Contact"
            tmpdoc.ConSalesperson= doc.ConSalesperson
            tmpdoc.ConInterests= doc.ConInterests
            tmpdoc.CoDivision = doc.CoDivision
            tmpdoc.ConFullName = doc.ConFirstName(0) & " " & doc.ConLastName(0)
            tmpdoc.ConBranch = doc.ConBranch
            tmpdoc.ConPhone = doc.ConPhone
            tmpdoc.ConMobile = doc.ConMobile
            tmpdoc.ConEmail = doc.ConEmail
            
      'Resizes the window to fit the table on the form
            If ws.Dialogbox("Activity", True, True, False, False, False, False, "New Activity for " & doc.ConFirstName(0) & " " & doc.ConLastName(0) , tmpdoc, True,True,True) Then
            End If
            Call uidoc.RefreshHideFormulas
     
End Sub 

Open in new window


Make it a habit to declare variables. There's an option in the Designer Properties to enforce Option Explicit (synonym for Option Declare). I assume you have a Save action in the Activity form?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41372955
Work resumed.
0
 

Author Comment

by:kcoxon
ID: 41373086
Thanks.
Got script error with Call uidoc.RefreshHideFormulas so changed this line to
Call tmpdoc.RefreshHideFormulas and it now seems to work.
You have added a check to see that only one document is selected, it would be good to check also that the selected document has form name "Contact"
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41373090
Well... Knock yourself out! :)

Hint: doc.Form(0)="Contact"
0
 

Author Comment

by:kcoxon
ID: 41373196
Point made but when changed to this
If col.Count<>1 & doc.Form(0)="Contact" Then
            Msgbox "Please select (only) one contact",16,"Activity"
            Exit Sub
      End If

I'm back to "Object Variable not set"
0
 
LVL 46

Assisted Solution

by:Sjef Bosman
Sjef Bosman earned 500 total points
ID: 41373440
Two things:
- the logical-and in LotusScript is the word And instead of the ampersand character
- the bigger problem: doc isn't set yet when you use doc.Form(0)

Solution:
- copy/paste the entire if-statement after the line with Set doc= ...
- remove the condition on Form from the originally first if-statement
- leave the condition on Form in the 2nd if-statement
- modify the MsgBox text, e.g. This action can only be applied to an Activity
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41373445
Sorry, ... to a Contact document
0
 

Author Comment

by:kcoxon
ID: 41373945
Thanks, you're a star and thank you for excusing my LS ignorance!

Finished code.

Sub Click(Source As Button)
      Dim ns As NotesSession
      Dim db As NotesDatabase
      Dim tmpdoc As NotesDocument
      Dim doc As NotesDocument      
      ' Add document collection
      Dim col As NotesDocumentCollection
      
      Set ns= New NotesSession
      Set db= ns.Currentdatabase
      Set ws = New NotesUIWorkspace
       ' Set document collection
      Set col = db.UnprocessedDocuments
      If col.Count<>1  Then
            Msgbox "Please select (only) one Contact",16,"New Activity"
            Exit Sub
      End If
      
      Set tmpdoc= New NotesDocument (db)
      
      'Get the document to get values from
      Set doc = col.GetFirstDocument
      'Check that the correct form is selected
      If doc.form(0)<>"Contact" Then
            Msgbox "Please select a Contact",16,"New Activity"
            Exit Sub
      End If
      
      ' Fill tmpdoc fields with values from the selected document
      tmpdoc.CoUNID = doc.CoUNID
      tmpdoc.CoName = doc.CoName
      tmpdoc.CoChainName=doc.CoChainName
      tmpdoc.CoAccountType=doc.CoAccountType
      tmpdoc.ConSource=doc.ConSource
      tmpdoc.ConUNID = doc.ConUNID
      tmpdoc.ConStatus = doc.ConStatus
      tmpdoc.SourceForm = "Contact"
      tmpdoc.ConSalesperson= doc.ConSalesperson
      tmpdoc.ConInterests= doc.ConInterests
      tmpdoc.CoDivision = doc.CoDivision
      'tmpdoc.ConFullName = doc.ConFirstName(0) & " " & doc.ConLastName(0)
      tmpdoc.ConFirstName = doc.ConFirstName
      tmpdoc.ConLastName = doc.ConLastName       
      tmpdoc.ConBranch = doc.ConBranch
      tmpdoc.ConPhone = doc.ConPhone
      tmpdoc.ConMobile = doc.ConMobile
      tmpdoc.ConEmail = doc.ConEmail
      
      'Resizes the window to fit the table on the form
      If ws.Dialogbox("Activity", True, True, False, False, False, False, "New Activity for " & doc.ConFirstName(0) & " " & doc.ConLastName(0) , tmpdoc, True,True,True) Then
      End If
      'Button on the form saves the doc
End Sub
0
 

Author Closing Comment

by:kcoxon
ID: 41373949
Thank you for your patience
0
 

Author Comment

by:kcoxon
ID: 41375281
Sorry it is this one.

One last question, well on this at any rate. Please tell me if I should raise another question.
On the above LS, how do I save the selected Contact in the view so that fields a re-computed.
I have 2 fields that display values from the last created Activity
I tried the obvious of Call doc.Save( True, True ) but this doesn't work.
:)
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41375703
If ws.Dialogbox("Activity", True, True, False, False, False, False, "New Activity for " & doc.ConFirstName(0) & " " & doc.ConLastName(0) , tmpdoc, True,True,True) Then
            Call doc.ComputeWithForm(False, False)
            Call doc.Save(True, False)
      End If
      'Button on the form saves the doc
End Sub

Instead of the ComputeWithForm, it is usually better and faster to set the fields directly, if you know what their values will be, using
     doc.field= tmpdoc.field
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kcoxon
ID: 41375789
Seems that you don't sleep more than me!

May be easier to just refresh the form and save but I have tried the above and it doesn't work.

There are 3 computed fields on the selected contact form in the view, NOT the new Activity Doc

The 3 fields are

1. ViewLastComment field with formula

uname:=@Subset(@DbLookup("Notes":"NoCache";"":"";"LupContactsActsByConUNID";ConUNID;"ActUserName");1);
uinitial:=@Left(uname; 1) + @Middle(uname; " "; 1);
comm:=@Subset(@DbLookup("Notes":"NoCache";"":"";"LupContactsActsByConUNID";ConUNID;"Comment");1);

lc:=uinitial + " " + comm;

@If(@IsError(lc);"";lc)

2. ViewNextActivity field with formula

REM {This looks up the next activity on the Contact Activities view and pulls back the Action Required with any Reminder Date};
actreq:= @Subset(@DbLookup("Notes":"NoCache";"":"";"LupContactsActsByConUNID";ConUNID;"ActionRequired");1);
rdate:= @Left(@Text(@Subset(@DbLookup("Notes":"NoCache";"":"";"LupContactsActsByConUNID";ConUNID;"ActReminderDate");1));10);

val:=@Trim(@Explode(actreq +";"+rdate+";";";"));
@If(@IsError(val);"";val)

3. ViewLastActivity field with formula

REM {This looks up the last activity on the Company Activities view and pulls back the Last Action taken with date};
lact:= @Subset(@DbLookup("Notes":"NoCache";"":"";"LupContactsActsByConUNID";ConUNID;"ActionTaken");1);
adate:= @Left(@Text(@Subset(@DbLookup("Notes":"NoCache";"":"";"LupContactsActsByConUNID";ConUNID;"ActDateTime");1));10);
val:=@Trim(@Explode(lact +";"+adate+";";";"));
@If(@IsError(val);"";val)
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41375866
I bet you'll answer my question in 7 hours or so... :-P

The question(s): would your @DbLookup function above retrieve the Activity document that you just created with the tmpdoc and the @DialogBox? If I'm correct the exact same document is retrieved in all cases (same view, same key, yet different field name looked up).
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41375878
Correction: would ... happen to retrieve...
0
 

Author Comment

by:kcoxon
ID: 41376135
Certainly would. This is a CRM db where users add activities and or comments using the Activity form.
These are displayed in an embedded view in the contact form.
In order that the user can see his 'last activity', next activity in a standard view without opening the contact I display these fields so that he can review.
So yes it is always the doc just created.
Cheers,
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41376227
Well, if that's the case, the values of the fields are in tmpdoc when the DialogBox call returns. No need to use ComputeWithForm.

If ws.Dialogbox("Activity", True, True, False, False, False, False, "New Activity for " & doc.ConFirstName(0) & " " & doc.ConLastName(0) , tmpdoc, True,True,True) Then
            Dim v As Variant
            v= Split(tmpdoc.ActUserName(0), " ")
            doc.ViewLastComment= Left(v(0),1) + Left(v(Ubound(v)), 1) + " " + tmpdoc.Comment(0)
            doc.ViewNextActivity= Split(tmpdoc.ActionRequired(0) + ";" + Cstr(tmpdoc.ActReminderDate(0)), ";")
            doc.ViewLastActivity= Split(tmpdoc.ActionTaken(0) + ";" + Cstr(tmpdoc.ActDateTime(0)), ";")
            Call doc.Save(True, False)
      End If
      'Button on the form saves the doc
End Sub

I couldn't quite understand the formulae you used without information about the types of the fields, so it's an educated guess. You might need to add FullTrim() around both Split() functions, not quite sure.
0
 

Author Comment

by:kcoxon
ID: 41376273
Thanks but this seems not to attempt to save the contact even when just using
Call doc.Save(True, False) it shows as modified last night when I last saved it.
There is something more fundamental we are missing.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41376312
That's strange. I assume you have the rights to save the document.

Try using the LotusScript debugger. You cannot debug a DialogBox, but you can stop execution right after it. Set a breakpoint, and study the variables. What exactly is doc? right before the Save, what field values does it have?

It's a pity EE doesn't have TeamViewer... (hint!)
0
 

Author Comment

by:kcoxon
ID: 41376382
Debugger, setting break points all new to me.
I am off to enjoy my Christmas lunch now so will have a play with this over the weekend.
We have a similar system to Team Viewer so if all else fails.

Have a good one!
Many thanks.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41376461
No Debugger?? A whole new world is there for you to discover! Get acquainted with it, you'll like and need it when developing LS code.

Joyeux Noël !
0
 

Author Comment

by:kcoxon
ID: 41379407
Spent some time trying to find out how to set a BreakPoint in debugger and conclude that I have to be missing something. Read 'Debugging with breakpoints' where it refers to selecting a statement and choosing Debug - Set/Clear Breakpoint but don't see this anywhere!
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41379412
Double-click a line... Note that you cannot set breakpoints on non-executable lines, like Sub Soandso, Else, etc.
0
 

Author Comment

by:kcoxon
ID: 41379416
Assume I need Tools/Debug LotusScript ticked first.
I have double clicked every line and nothing happens.
It's like something isn't turned on or available to me
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41379422
Indeed, Tools/Debug first. Then you start the agent, or click the action button. When the code starts, it stops at the first line in the Debugger. Then you can set breakpoints.
0
 

Author Comment

by:kcoxon
ID: 41379429
ALL becomes clear. I have tried setting a breakpoint on Call doc.Save(True, False) but it doesn't stop here, it continues to End If.
Where should I be placing the breakpoint and then what should I be looking for?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41379435
You can also step through the code, statement by statement.

Where to set breakpoints? It's hard to give a simple answer. You set a breakpoint on the Call Save, and execution didn't stop there apparently, which proves that execution took a different path through your code. Try to set a breakpoint at a line of which you think it will be executed, e.g. an If statement, and then you execute one step.

Each time you can read the variables in the frame at the bottom.
0
 

Author Comment

by:kcoxon
ID: 41380929
Sjef, I have to put this on hold until the new year now as I'm running out of time with year end stuff to do.
Have an excellent Christmas! I'll be back.
0
 

Author Comment

by:kcoxon
ID: 41395378
Happy New Year!
Sadly with that action all I get with the debugger is "Lotus Script cannot debug the current object in a dialog" I then complete the activity form and save but no debugger. Debugger is running in my mail. Somewhat confused
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 41395602
Two things:
- the debugger doesn't start while in a form and document when you activate it; you should activate the debugger before opening the form and document.
- you should set a breakpoint on the lines after the call to ws.DialogBox, both the True and the False part; if you're only interested in the part where the user clicked OK, you can set only one breakpoint.

Bonne Année 2016! ;)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This is an old article, please see an updated version of this article, located here: http://www.experts-exchange.com/articles/23619/Notes-8-5x-Windows-7-Notes-info-and-tips.html
For beginners of Lotus Notes user this is important to know about the types of files and their location supported by IBM Notes. Mostly users are unaware about how many file types are created and what their usages are. This Article is fully dedicated…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now