Xpages How to set default value to blank field after document has already been saved? - xpages

My PCModel document will go through several workflow processes, and the user will only add the necessary information at the appropriate stage. The user will select a dialogbox and be presented with a few fields.
For some of these fields I want to pre-populate a default value. However, my fields are bound to my Java object. and default code is ignored, I believe because there ALREADY is a value in the field, blank.
How can I add a default value, like the current user, to the bound field while still allowing the user to override this?
<xc:cc_CommonFormField id="cc_CommonFormField7" fieldName="buildUser" label="By Team Member">
<xp:this.facets>
<xp:comboBox id="comboBoxA" xp:key="field" value="#{PCModel.buildUser}"
defaultValue="#{userBean.commonName}">
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:"Select A Value|"}]]></xp:this.value>
</xp:selectItems>
<xp:selectItems
value="#{PCConfig.networkTeam}"/>
</xp:comboBox>
</xp:this.facets>
</xc:cc_CommonFormField>
My java object is normal.

Initialize the value in your Java bean
or
set the value somewhere in your SSJS code with
PCModel.buildUser = userBean.commonName

Related

dataContext binding to data

I am trying to understand the dataContext better and tried creating a dataContext referencing a document.
<xp:this.dataContexts>
<xp:dataContext var="doc1">
<xp:this.value>
<![CDATA[#{javascript:
var db:NotesDatabase = sessionAsSigner.getDatabase("","privDb.nsf");
var adoc:NotesDocument = db.createDocument();
return adoc }]]>
</xp:this.value>
</xp:dataContext>
</xp:this.dataContexts>
I then tried using EL and javascript to bind fields on my xpage to the dataContext
<xp:inputText id="inputText2" value="#{doc1.lastname}"></xp:inputText>
<xp:inputText id="inputText1" value="${javascript:doc1.firstname}"></xp:inputText>
But when I save, it does't save anything.
<xp:button value="save" id="button1">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:
print(doc1.getClass().getName() )
doc1.save();
}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
The print command showing me the class name is showing up as lotus.domino.local.Document
The document is saved to the database, but it has no values other than $UpdatedBy. I can't seem to bind fields to the edit boxes.
The reason I am going down this path is twofold, 1. I want to use sessionAsSigner so I can keep the database security on the remote db (privDb.nsf) at No Access for anonymous and default, and 2. I want to learn a little more about dataContext, data sources and binding. I have read the "easy" way of using a Public Document, using the $PublicAccess field, etc, which is the "Old School" Notes way, and yes, I could do it that way, but want to understand how to do it using dataContexts, if possible.
A dataContext is basically a scoped variable, scoped lower than viewScope but higher than requestScope, scoped to a component. That component can be an XPage, a Custom Control or a Panel (yes, dataContexts can be added to a Panel too).
Like other scoped variables on the page, a simple save action does not save dataContexts. If you want a variable that's create-able and save-able, that's the Data Object. That has specific createObject and saveObject properties, where you define what should happen when they're called by the XPages runtime.
Similarly, like the other scoped variables, it needs to be serialized, so you cannot store a Domino object in them. So you can't store a NotesDocument in them. You need to wrap a normal Java object around a NotesDocument. With a greater understanding of XPages it becomes apparent that's what the dominoDocument datasource is doing (creating properties for all fields on the document, storing its note id, UNID, adding other properties like whether it's in edit mode or new etc).
A final point, as Jesse says, dataContexts are re-evaluated multiple times during a partial refresh. I haven't re-tested recently, but under 8.5.3 dataContexts bound to an XPage or Custom Control were re-evaluated more than dataContexts bound to a Panel, so I'd recommend the latter.
The immediate problem that you're running into is that #{}-bound dataContexts are re-evaluated constantly, several times during a page load and, I believe, every time they're referenced. Generally, the rule of thumb with dataContexts (and don't get me wrong - I like them) is that they should either be extremely low-cost, like a quick mathematical calculation, or be ${}-bound. The latter wouldn't work here, though, since the document wouldn't survive past the first load.
The tack you may want to try is to use a dataContext like this:
<xp:dataContext var="docData" value="${javascript: new java.util.HashMap() }" />
Basically, using a simple object as a holding pen. Then, in the save action, create the new document and set all of the values from "docData" there, like:
var db = sessionAsSigner.getDatabase("", "privDb.nsf");
var doc = db.createDocument();
doc.replaceItemValue("firstname", docData.get("firstname"));
doc.replaceItemValue("lastname", docData.get("lastname"));
doc.save();
There are a few caveats to this approach:
Each save will create a new document, rather than editing the existing one. You could change this by storing the UNID back into the map and fetching it from the DB, though
It should work as-is in this case, but you'll have to be mindful of data types. For example, if you have a multi-value control, then the XPages runtime will probably create an ArrayList, which you would have to convert to a Vector for storage unless you're using the OpenNTF Domino API
And as a final note, that binding you have for firstname is almost definitely not what you want. That may just be an artifact of the testing you were doing, but I'd be remiss if I didn't mention it.

xpages, get value of a field in a form from other form

I have two forms Job and Comment,type of these forms are document and response.
There is a field in the Job form that save name of developer and there is a field in the Comment form
that I want to get the name of developer from Job when I want to create a comment for selected job.
One way to get field values from a parent document (in your case from the Job document) is to use a data context to create a parentDoc object. You can then refer to this parentDoc object to get field values from the parent document.
Start by creating a parentDoc data context:
<xp:this.dataContexts>
<xp:dataContext var="parentDoc">
<xp:this.value><![CDATA[#{javascript:
return database.getDocumentByUNID(currentDocument.getParentId());
}]]></xp:this.value>
</xp:dataContext>
</xp:this.dataContexts>
If you just want to display a value from the parent document (and not save it in the response document), you can then use a computed field to display the value from the parent document (using expression language to refer to the field from the parentDoc object):
<xp:text escape="true" id="displayParentField" value="#{parentDoc.field}" />
You can also use the value from the parent document as the default value for an input field:
<xp:inputText id="responseValue" value="#{currentDocument.responseField}" defaultValue="#{parentDoc.field}" />
Short answer, you filter the 2nd data source with the value of the field. It could be the response field on the response doc or the value of the field that exists on both docs.
Long answer, This is a common xPages question, one I have asked myself. Take a look at this question. xPage with multiple datasources has the second datasource always opened in edit mode
A simple solution, I added the below code in default value of Assign field in response form it works now
var parentDoc = database.getDocumentByID(document1.getParentId())
return parentDoc.getItemValueString("Developer")
Thank you Per Henrik Lausten your answer helped me to solve this issue.

xpages set value to field before action=newDocument

I have a simple button with the eventHandler:
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
<xp:this.action>
<xp:openPage target="newDocument" name="/doc.xsp"></xp:openPage>
</xp:this.action>
</xp:eventHandler>
In lotusScript I would add a value to a field, before composing the form, using:
Call doc_new.ReplaceItemValue("txt_codformularmain","01")
....
....
Set uidoc = w.EditDocument ( True, doc_new )
I tried in the postNewDocument event of the doc.xsp
<xp:this.data>
<xp:dominoDocument var="Contr" formName="(fmFormularCIP)">
<xp:this.postNewDocument><![CDATA[#{javascript:Contr.replaceItemValue("txt_codformularmain", "01")}]]></xp:this.postNewDocument></xp:dominoDocument>
</xp:this.data>
I don't want every time the doc is created that value to be, let say, 01, BUT only when the doc. is composed by a specific button. Some other button will have the chance to add the 02 value for that field, and so on.
How can I achieve this in xpages development? Thanks for your time.
This is a little bit tricky in Web application development, because what you did in classical Notes development cannot be applied here. I had a lot of issues in this.
The classical scenario is that you have a Page X with some value (say txt_codformularmain will be 01). How do you decide this value "01"?
In some cases, this value is something you have in the page X. So you want to pass a specific value to the target page. One option could be using a query parameter (doc.xsp?myValue=01) and consume it on doc.xsp with param.myValue. Or, you might put a sessionScope variable before going to the target page and consume it by sessionScope.myValue.
It depends on what you need. Query parameter is somewhat safer, because the user might use the same button twice and sessionScope variable causes inconsistent behaviour. On the other hand, query parameter can be changed by the user, so you might have a breach in your application.
Some cases, you want to populate some values at the second page. Simple values (e.g. creation date, user names, etc.) can be populated with forms (i.e. compute value on load). Sometimes it would be more complicated (e.g. the department of the user where you have to lookup some views). For such cases, you need to use postNewDocument event at the target page.
After you have passed the value to the doc.xsp page, you will consume in the way you need. If it's not going to be edited on the page, you may again use postNewDocument event and set value by dataSourceName.replaceItemValue('itemName', param.myValue). If the value can be edited, you would use it as a default value on your field component.
One problem I have experienced: Once you set a value in postNewDocument, you may have problems to change it later, until the document data source saved. This is happening on specific cases. If you experience such a problem, you might use some editable field with readonly attribute. Just keep in mind :)

xpages passing the UNID to other field

I have an input field:
<xp:inputText value="#{Cdoc.txt_UNID}" id="txt_UNID1"></xp:inputText>
The field txt_UNID is computed having the value: #Text(#DocumentUniqueID).
What I noticed is that even I just compose the xpage containing the document content the inputText already contains some UNID even if the xpage containing the doc content wasn't saved.
There is a button which is showing a dialog containing a field. I want this field to have the value of txt_UNID
Cdoc.save(); // I must save first the Cdoc datasource ?
getComponent('exampleDialog').show()
And the code for the field:
<xp:inputText value="#{Pdoc.txt_CompanieUNID}"
id="txt_CompanieUNID1" defaultValue="#{Cdoc.txt_UNID}">
</xp:inputText>
Thanks for your time.
If your question is whether or not you must first save the document in order to get the UNID, my answer is yes. In my tests, the UNID has changed from a new document to a freshly saved document. So, yes if the current document is new, save before getting the UNID. At least when it was a NotesXspDocument.
I also generally use JavaScript for this,
cdoc.getDocument().getUniversalId();
Otherwise, I am unsure what you are asking.
If PDoc is available at the same level of the hierarchy as CDoc, e.g. both are datasources assigned to the XPage / Custom Control or the same Panel, you could update PDoc on save, using PDoc.replaceItemValue("txt_CompanieUNID",Cdoc.getDocument().getUniversalID()). setValue should also work. Then you would not need the default value.
It just helps keep all business logic in one place. You could also check whether a value has already been set.

Dynamically add custom control on Xpage

how can I add custom control on the basis of sessionScope variable. I try include page container control but no luck:
<xp:this.afterPageLoad><![CDATA[#{javascript:sessionScope.put("viewName","ccViewAll.xsp");}]]></xp:this.afterPageLoad>
<xp:text escape="true" id="computedField1">
<xp:this.value><![CDATA[#{javascript:sessionScope.get("viewName")}]]></xp:this.value>
</xp:text>
<xc:appLayout>
<xp:this.facets>
<xp:panel xp:key="facetMiddle">
<xp:include id="include1">
<xp:this.pageName><![CDATA[${javascript:sessionScope.get("viewName")}]]>
</xp:this.pageName>
</xp:include>
</xp:panel>
</xp:this.facets>
</xc:appLayout>
The above code give me error Error 404 HTTP Web Server: Item Not Found Exception. But when I hardcode the viewname that is ccViewAll.xsp instead of sessionScope.get("viewName"), its work fine.
-MAK
You can use the dynamic content control or the switchFacet control if you have the ExtLib for XPages. The Teamroom template (demo application that comes with the ExtLib) uses these in the "allDocuments" Xpage or the "allDocsAllTab" custom control, these are good examples to look at.
If you don't have the ExtLib you could use the loaded / rendered property of a custom control to decide which one gets loaded.
e.g.
<xp:panel key="MiddleColumn">
<xc:customControl1 loaded="${javascript: if(viewScope.control == "customControl1")}"></xc:customControl1>
<xc:customControl2 loaded="${javascript: if(viewScope.control == "customControl2")}"></xc:customControl2>
</xp:panel>
loaded = false means that nothing will be done for this control.
rendered = false means that the control will be created but hidden, you can change this later to show it.
use rendered if its going to change for example when a button is clicked and loaded when its decided at start up and won't change while the user is logged in.
If you are using this to show a different view in the domino database based on some other selection that I would suggest looking at the Extension Libraries 'Dynamic View Panel' control.
Using this control means you won't need to create different custom controls for each view that you want to use, just a single page with this control and point it to the correct view to display via a scope variable.
If you need to customize how each view displays you can create a viewControl bean to set additional properties based on the view that it is showing.
Something worth to mention is that if you don't use the ExtLib - If you're using Partial Refresh then you HAVE to use the rendered property, since the loaded property only can be calculated on pageLoad.
From my understanding this means that every custom control will be computed by the server and that's probably not something you want. (Added overhead, for one thing.)
/J

Resources