Question

I have a button in a view called "Add Line Item". You select a document, then hit this button. The intent is that we create a new document with values from the selected document being stuffed into the new document.

In the view-button, I set a sessionScope variable with a long string. I delimit each entry with a "~". Within each entry, the first thing is the actual name of the field. The next thing is the value of that field. These two are delimited by a pipe "|". If the actual value of the field is an array, they naturally have semicolons dividing them. I mention all of this so the following code can be understood.

In the form I launch, in the beforePageLoad event, I have the following;

var AddingNewLine = sessionScope.get("AddingNewLine");
if (AddingNewLine != null) {
  fieldsArray = AddingNewLine.split("~"); // make an array that has the field names & its value(s)
  for (i = 0 ; i < fieldsArray.length ; i++) {
    splitValue = fieldsArray[i].split("|"); // now we make an array out of the entry. value 0 will be
    fieldName = splitValue[0];              // the name of the field. value 1 will be the values which
    fieldValues = splitValue[1].split(";")  // in turn will be split into an array
    var f = "PayRates:HourlyRates:CPrompts:IssuingCountry:ServiceCurrency:ReceivedDate:InvoiceDate:Client:InvoiceNumber:WBS";
    if (f.search(fieldName) >= 0) {
      currentDocument.setValue(fieldName,fieldValues);
      if (fieldName == "CPrompts") { var CPrompts = fieldValues }
    }
  }
// new doc but adding new line item or replicating? then get our prompts from the original doc
for (i = 0; i < CPrompts.length ; i++) {sessionScope.put("CPrompt"+(i+1),CPrompts[i])}
}

Note: I'm screening out the numeric values with "f" variable right now as I'm having a different problem with them

Anyway, all of my textboxes are getting updated, but the comboboxes are not (Which happen to be IssuingCountry, ServiceCurrency, Client, and WBS.

I've verified that the field names and their values are correct with print statements when I do the currentDocument.setValue. I've even tried doing a currentDocument.replaceItemVale. Neither seems to work for comboBox type fields. This also holds true for the date value fields.

Combobox field example:

<xp:comboBox 
  id="IssuingCountry" 
  style="width:160.0px" 
  value="#{FInvoiceDoc.IssuingCountry}">
    <xp:selectItem 
      itemLabel="United States of America"
      itemValue="United States of America" 
      id="selectItem1">
    </xp:selectItem>
    <xp:selectItems id="selectItems1">
      <xp:this.value>
        <![CDATA[#{javascript:@DbLookup("", "Keywords", "Countries", 2)}]]>
    </xp:this.value>
    </xp:selectItems>
</xp:comboBox>

Could it be that I need to somehow update the itemLabel? If so, how?

Date field example;

<xp:inputText
  value="#{FInvoiceDoc.ReceivedDate}" 
  id="ReceivedDate"
  style="width:85px">
    <xp:this.converter>
      <xp:convertDateTime
        pattern="MM/dd/yyyy">
      </xp:convertDateTime>
    </xp:this.converter>
  <xp:dateTimeHelper
    id="dateTimeHelper1">
  </xp:dateTimeHelper>
</xp:inputText>
Was it helpful?

Solution

The session scope can handle complex objects. Creating a string reduces maintainability quite a bit. I would create the stuff like this:

  var prepopulate = [];
  prepopulate.push({ "name" : "IssuingCountry", value : "United Stated of America"});
  prepopulate.push({ "name" : "ReceivedDate", value : [date1, date2]);

  sessionScope.AddingNewLine = prepopulate;

Then is the afterPageLoad (which runs before renderResponse) you do something like this (omitted the test for valid field names):

  if (!sessionScope.AddingNewLine) {
     return;
  }
  var prepopulate = sessionScope.AddingNewLine;

 for( var i = 0; i < prepopulate.length; i++ ) {
    var oneItem = prepopulate[i];
    FInvoiceDoc.setValue(oneItem.name, oneItem.value);
 }

  // reset
 sessionScope.AddingNewLine = null;

So the 2 important differences: use afterPageLoad and use a complex object, not a wild string. Instead of an array, you might want to use a object since you have a special use for CPrompts.

Btw you declare a variable inside a condition. Only the fact that JS doesn't use block level variables prevents the code from failing if you don't have CPrompts in your string. So the complex object might be:

 var prepopulate = {};
 prepopulate.cprompts = ["some","prompts"];
 prepopulate.fields = [];
 prepopulate.fields.push(....);

Hope that helps

OTHER TIPS

Or you can just give UNID of selected document in parameter with something like that :

context.redirectToPage( "issue.xsp?action=newDocument&parentId=" + selectedUNID );

and in beforePageLoad, retrieve parentDoc to get all fields needed

if (dataDoc.isNewNote()) {
    var sParentId = dataDoc.getParentId();
    if (sParentId != null) {
        var parentDoc:NotesDocument = database.getDocumentByUNID(sParentId);

        dataDoc.setValue("KeyParent", parentDoc.getItemValueString("KeyDoc"));
        dataDoc.setValue("Domain", parentDoc.getItemValueString("Domain"));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top