If Then Code for a New Document

I have a button that saves a document, and creates response documents for users that are added to it. I have a @Unique function to generate a random number for my Form Number.

The problem I am having is that when I go in and edit the document and Save & Send the response documents, it treats it as a different document, so the Form Number is also changing. The form number needs to remain the same for the parent document.

Is there an easy to way to check to see if there is already a form number, and if so, to leave it the same? If not, go ahead and give it a random number.

Here is the code on my Save & Send button:

// get handle to names list
var item = new Array(document1.getItemValue("assignedassociates"));

var attach:String;

// loop through the list to create a review doc for each person listed
for(var i = 0; i <= (@Elements(item)-1); i++){

var newdoc:NotesDocument = database.createDocument()
newdoc.replaceItemValue("Form", "Review");
newdoc.replaceItemValue("datecreated", document1.getItemValueString("datecreated"));
newdoc.replaceItemValue("formnumber", document1.getItemValueString("formnumber"));
newdoc.replaceItemValue("title", document1.getItemValueString("title"));
newdoc.replaceItemValue("department", document1.getItemValueString("department"));
newdoc.replaceItemValue("description", document1.getItemValueString("description"));
newdoc.replaceItemValue("assignedassociates", item[i]);

newdoc.replaceItemValue("Status", "Sent");

// attachment URL
// calculate file path
// check for multiple attachments, create multiple URLs
attach = "http://domtest01.hendricks.org/chad/bloodhound.nsf/0/" + document1.getDocument().getUniversalID() + 
"/$file/" + @AttachmentNames() + "?openElement";

newdoc.replaceItemValue("AttachmentURL", attach);

// set the security fields on the new doc we created
setSecurityFields(newdoc, item[i]);

newdoc.save();

}

// update document history
var cuser:NotesName=session.createName(@UserName());  // gets user name in name field
updateHistoryField(document1.getDocument(),cuser.getCommon(),"EditHistory","form_create");      // call function


// show assigned docs
requestScope.put("showContainer", true);

Here is my code from the page:

<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xe="http://www.ibm.com/xsp/coreex"
    styleClass="forms"
    xmlns:xc="http://www.ibm.com/xsp/custom">
    <xp:this.data>
        <xp:dominoDocument
            var="document1"
            formName="Create">
        </xp:dominoDocument>
    </xp:this.data>
    <xp:table>
        <xp:tr>
            <xp:td style="font-weight:bold">Created By:</xp:td>
            <xp:td>
                <xp:text
                    escape="true"
                    id="computedField1">
                    <xp:this.value><![CDATA[#{javascript:var userName:NotesName = session.createName(@UserName());
return(userName.getCommon());}]]></xp:this.value>
                </xp:text>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold">Date:&#160;</xp:td>
            <xp:td>
                <xp:inputText
                    id="inputText2"
                    value="#{document1.datecreated}" defaultValue="#{javascript:{javascript:@Today()}}">
                    <xp:this.validators>
                        <xp:validateRequired message="You must select a Date."></xp:validateRequired>
                    </xp:this.validators>

                    <xp:dateTimeHelper id="dateTimeHelper1"></xp:dateTimeHelper>
                    <xp:this.converter>
                        <xp:convertDateTime
                            type="date"
                            dateStyle="short">
                        </xp:convertDateTime>
                    </xp:this.converter>
                </xp:inputText>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold">Form Number:</xp:td>
            <xp:td>

            <xp:inputText
                id="inputText3"
                value="#{document1.formnumber}"
                required="true" disabled="true">
                <xp:this.validators>
                    <xp:validateRequired message="You must enter a Form Number."></xp:validateRequired>
                    <xp:validateLength message="Form Number must be 8 characters in length."></xp:validateLength>
                </xp:this.validators>
            <xp:this.defaultValue><![CDATA[#{javascript:session.evaluate("@Unique")}]]></xp:this.defaultValue></xp:inputText>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold">Title:&#160;</xp:td><xp:td>

    <xp:inputText
        id="inputText1"
        value="#{document1.title}"
        maxlength="60"
        required="true">
        <xp:this.validators>
            <xp:validateLength maximum="60"></xp:validateLength>
            <xp:validateRequired message="You must enter a Title."></xp:validateRequired>
        </xp:this.validators>
    </xp:inputText>&#160;</xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold">Department:</xp:td>
            <xp:td>
                <xp:comboBox
                    id="department"
                    value="#{document1.department}"
                    rendered="#{javascript:document1.isEditable()}">
                    <xp:this.validators>
                        <xp:validateExpression message="You must select a Department">
                            <xp:this.expression><![CDATA[#{javascript:value != "Select One"}]]></xp:this.expression>
                        </xp:validateExpression>
                    </xp:this.validators>
                    <xp:selectItem
                        itemLabel="Select One"
                        itemValue="Select One"
                        id="selectItem1">
                    </xp:selectItem>
                    <xp:selectItems id="selectItems1">
                        <xp:this.value><![CDATA[#{javascript:var dbName = ["", "aApplications\\HCHPhoneBk.nsf"];
@DbColumn( dbName , "DeptNameLookUp", 1);}]]></xp:this.value>
                    </xp:selectItems>
                </xp:comboBox>
                <xp:text
                    escape="true"
                    id="computedField2"
                    value="#{document1.department}"
                    rendered="#{javascript:!document1.isEditable()}">
                </xp:text>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold">Description:</xp:td>
            <xp:td>
                <xp:inputTextarea
                    id="inputTextarea1"
                    value="#{document1.description}"
                    style="width:300.0px;height:96px"
                    required="true">
                    <xp:this.validators>
                        <xp:validateRequired message="You must enter a Description."></xp:validateRequired>
                    </xp:this.validators>
                </xp:inputTextarea>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold">Attachments:</xp:td>
            <xp:td>
                <xp:fileUpload
                    id="attachments"
                    value="#{document1.attachments}">
                </xp:fileUpload>
            &#160;</xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td style="font-weight:bold"></xp:td>
            <xp:td>
                <xp:fileDownload
                    rows="30"
                    id="fileDownload1"
                    displayLastModified="false"
                    hideWhen="true"
                    displaySize="true"
                    displayCreated="false"
                    value="#{document1.attachments}"
                    displayType="false" allowDelete="true">
                </xp:fileDownload>
            </xp:td>
        </xp:tr>
        <xp:tr>
            <xp:td
                rowspan="2"
                style="font-weight:bold">
                Associates to
                <xp:br id="br1"></xp:br>
                review document:
            </xp:td>
            <xp:td rowspan="2">
                <xe:djextNameTextBox
                    id="djextNameTextBox1"
                    multipleSeparator=","
                    multipleTrim="true"
                    value="#{document1.assignedassociates}"
                    rendered="#{javascript:document1.isEditable()}">
                </xe:djextNameTextBox>
                &#160;&#160;
                <xe:namePicker
                    id="namePicker1"
                    for="djextNameTextBox1"
                    pickerIcon="/Symbol+Add.gif">
                    <xe:this.dataProvider>
                        <xe:namePickerAggregator>
                            <xe:this.dataProviders>
                                <xe:dominoNABNamePicker
                                    addressBookDb="HCHPhoneBk"
                                    nameList="people">
                                </xe:dominoNABNamePicker>
                            </xe:this.dataProviders>
                        </xe:namePickerAggregator>
                    </xe:this.dataProvider>
                </xe:namePicker>
            </xp:td>
        </xp:tr>
        <xp:tr></xp:tr>
    </xp:table>
    <xp:messages id="messages1"></xp:messages>
    <xp:br></xp:br>
    <xp:button
        id="button4"
        rendered="#{javascript:document1.isEditable()}">
        <xp:this.value><![CDATA[Save & Assign Response Documents]]></xp:this.value>
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="complete">
            <xp:this.action>
                <xp:actionGroup>
                    <xp:saveDocument var="document1"></xp:saveDocument>
                    <xp:executeScript>
                        <xp:this.script><![CDATA[#{javascript:// get handle to names list
var item = new Array(document1.getItemValue("assignedassociates"));

var attach:String;

// loop through the list to create a review doc for each person listed
for(var i = 0; i <= (@Elements(item)-1); i++){

    var newdoc:NotesDocument = database.createDocument()
    newdoc.replaceItemValue("Form", "Review");
    newdoc.replaceItemValue("datecreated", document1.getItemValueString("datecreated"));
    newdoc.replaceItemValue("formnumber", document1.getItemValueString("formnumber"));
    newdoc.replaceItemValue("title", document1.getItemValueString("title"));
    newdoc.replaceItemValue("department", document1.getItemValueString("department"));
    newdoc.replaceItemValue("description", document1.getItemValueString("description"));
    newdoc.replaceItemValue("assignedassociates", item[i]);

    newdoc.replaceItemValue("Status", "Sent");

    // attachment URL
    // TO DO - check for server name and put in appropriate server name and format
    // calculate file path
    // check for multiple attachments, create multiple URLs
    attach = "http://domtest01.hendricks.org/chad/bloodhound.nsf/0/" + document1.getDocument().getUniversalID() + 
    "/$file/" + @AttachmentNames() + "?openElement";

    newdoc.replaceItemValue("AttachmentURL", attach);

    // set the security fields on the new doc we created
    setSecurityFields(newdoc, item[i]);

    newdoc.save();

}

// update document history
var cuser:NotesName=session.createName(@UserName());  // gets user name in name field
updateHistoryField(document1.getDocument(),cuser.getCommon(),"EditHistory","form_create");  // call function


// show assigned docs
requestScope.put("showContainer", true);}]]></xp:this.script>
                    </xp:executeScript>
                    <xp:changeDocumentMode
                        mode="readOnly"
                        var="document1">
                    </xp:changeDocumentMode>
                </xp:actionGroup>
            </xp:this.action>
        </xp:eventHandler>
    </xp:button>&#160;<xp:button id="button1" value="Edit" rendered="#{javascript:!document1.isEditable()}">
        <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
            <xp:this.action>
                <xp:actionGroup>
                    <xp:changeDocumentMode mode="edit" var="document1">
                    </xp:changeDocumentMode>
                    <xp:executeScript>
                        <xp:this.script><![CDATA[#{javascript:// update document history
var cuser:NotesName=session.createName(@UserName());  // gets user name in name field
updateHistoryField(document1.getDocument(),cuser.getCommon(),"EditHistory","form_create");  // call function}]]></xp:this.script>
                    </xp:executeScript>
                </xp:actionGroup>
            </xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <xp:button id="button5">
        <xp:this.value><![CDATA[#{javascript:if (document1.isEditable()){
    return "Cancel";
}else{
    return "Close";
}}]]></xp:this.value>
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="complete" immediate="true">
        <xp:this.action>
            <xp:openPage name="$$PreviousPage"></xp:openPage>
        </xp:this.action></xp:eventHandler></xp:button>
    <xp:this.beforePageLoad><![CDATA[#{javascript:requestScope.put("showContainer", false)}]]></xp:this.beforePageLoad><xp:br></xp:br>
    <xp:br></xp:br>
    <xp:panel id="outerContainer">
        <xp:panel id="innerContainer">
            <xp:viewPanel
                rows="30"
                id="viewPanel1"
                var="rowData">
                <xp:this.facets>
                    <xp:pager
                        partialRefresh="true"
                        layout="Previous Group Next"
                        xp:key="headerPager"
                        id="pager1">
                    </xp:pager>
                    <xp:viewTitle
                        xp:key="viewTitle"
                        id="viewTitle1" value="Sent To">
                    </xp:viewTitle>
                </xp:this.facets>
                <xp:this.data>
                    <xp:dominoView
                        var="view1"
                        viewName="Children">
                        <xp:this.categoryFilter><![CDATA[#{javascript:document1.getItemValueString("Title");}]]></xp:this.categoryFilter>
                    </xp:dominoView>
                </xp:this.data>
                <xp:viewColumn
                    columnName="$6"
                    id="viewColumn1">
                    <xp:this.converter>
                        <xp:convertDateTime
                            type="date"
                            dateStyle="short">
                        </xp:convertDateTime>
                    </xp:this.converter>
                    <xp:viewColumnHeader
                        value="Date"
                        id="viewColumnHeader1">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:viewColumn
                    columnName="department"
                    id="viewColumn2">
                    <xp:viewColumnHeader
                        value="Department"
                        id="viewColumnHeader2">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:viewColumn
                    columnName="formnumber"
                    id="viewColumn3">
                    <xp:viewColumnHeader
                        value="Form #"
                        id="viewColumnHeader3">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:viewColumn
                    columnName="assignedassociates"
                    id="viewColumn4"
                    displayAs="link"
                    openDocAsReadonly="#{javascript:rowData.getColumnValue('Status') === 'Responded'}">
                    <xp:viewColumnHeader
                        value="Reviewer"
                        id="viewColumnHeader4">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:viewColumn
                    columnName="Status"
                    id="viewColumn5">
                    <xp:viewColumnHeader
                        value="Status"
                        id="viewColumnHeader5">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
            </xp:viewPanel>
        </xp:panel>
    </xp:panel>
    <xp:text escape="true" id="computedField3"><xp:this.value><![CDATA[#{javascript:"requestScope.showContainer=" + requestScope.get("showContainer")}]]></xp:this.value></xp:text>
    <xp:br></xp:br>

    <xc:layout_EditHistory></xc:layout_EditHistory></xp:view>

Answers


Try this (includes recycle):

 var item:java.util.Vector = document1.getItemValue("assignedassociates");

 if (!document1.hasItem("formnumber")) {
    document1.replaceItemValue("formnumber",session.evaluate("@Unique"));
 }

 for(var curItem in item) {
    try {
        var newdoc:NotesDocument = database.createDocument();
        newdoc.replaceItemValue("Form", "Review");
        newdoc.replaceItemValue("datecreated", document1.getItemValueString("datecreated"));
        newdoc.replaceItemValue("formnumber", document1.getItemValueString("formnumber"));
        newdoc.replaceItemValue("title", document1.getItemValueString("title"));
        newdoc.replaceItemValue("department", document1.getItemValueString("department"));
        newdoc.replaceItemValue("description", document1.getItemValueString("description"));
        newdoc.replaceItemValue("assignedassociates", curItem);
        newdoc.replaceItemValue("Status", "Sent");

        // attachment URL
        var attach = "http://domtest01.hendricks.org/chad/bloodhound.nsf/0/" + document1.getDocument().getUniversalID() + 
        "/$file/" + @AttachmentNames() + "?openElement";
        newdoc.replaceItemValue("AttachmentURL", attach);

        // set the security fields on the new doc we created
        setSecurityFields(newdoc, curItem);
        newdoc.save();
        // Cleanup!
        newdow.recycle();
    } catch (e) {
       // Some clever error handling here
    }
 }

// update document history
var cuser:NotesName=session.createName(@UserName());  // gets user name in name field
updateHistoryField(document1.getDocument(),cuser.getCommon(),"EditHistory","form_create");


// show assigned docs
requestScope.put("showContainer", true);

Let us know how it goes!

Update

Use this for the row - if the form number shouldn't be changed after creation

  <xp:tr>
        <xp:td style="font-weight:bold">Form Number:</xp:td>
        <xp:td>

           <xp:inputText
               id="inputText3"
               value="#{document1.formnumber}" disabled="true"
               required="true" rendered="#{javascript:document1.isNewNote()}">
               <xp:this.validators>
                   <xp:validateRequired message="You must enter a Form Number."></xp:validateRequired>
                   <xp:validateLength message="Form Number must be 8 characters in length."></xp:validateLength>
                </xp:this.validators>
                <xp:this.defaultValue><![CDATA[#{javascript:session.evaluate("@Unique")}]]></xp:this.defaultValue>
          </xp:inputText>
          <xp:text escape="true" id="displayText3"
                   value="#{document1.formnumber}"
                   rendered=""#{javascript:!document1.isNewNote()}"></xp:text>
        </xp:td>
    </xp:tr>

This is close to what your current approach. However I actually would only use the display field and add the value in the NewDocument event of the document1 data source. So your row would look like this:

    <xp:tr>
        <xp:td style="font-weight:bold">Form Number:</xp:td>
        <xp:td>
          <xp:text escape="true" id="displayText3"
                   value="#{document1.formnumber}"></xp:text>
        </xp:td>
    </xp:tr>

In the events tab under Data - document1 - queryNewDocument you add as JavaScript:

   document1.replaceItemValue("formnumber",session.evaluate("@Unique"));

That should do the trick. Another remark: Use LABELS for your labels and use CSS for the formatting. Bold labels are kind out of fashion, so having a single definition in CSS makes adoption easier. Also you need labels if you ever plan to make it multi lingual (there's Chinese and Spanish even in the US - or Hindi if your work for 7/11)


Need Your Help

jQuery sortable events

jquery

Im using some jQuery sortables and I basically have two event handlers set up in the sortable init (receive and stop) The problem is, If the receive event fires then I don't want the stop event to ...

What are thos expressions ${*:1:$#:1} and ${*:1:$# - 1}

bash input parameters scripting

can somebody explain to me what do this expressions mean ? Too tough for me interpreting this: