下面是您需要根據問題的一些評論的代碼。
function simplegetSequentialNumber(){
synchronized(applicationScope){
var newSeqNum:Int = 0;
if (applicationScope.containsKey("seqNumber")){
newSeqNum = applicationScope.get("seqNumber") + 1;
applicationScope.put("seqNumber", newSeqNum);
var seqView:NotesView = database.getView("vw_SequentialNumberStore");
var seqNumberDoc:NotesDocument = seqView.getFirstDocument();
seqNumberDoc.replaceItemValue("seqNumber",applicationScope.get("seqNumber"));
seqNumberDoc.save(true,true);
} else {
var seqView:NotesView = database.getView("vw_SequentialNumberStore");
try {
var seqNumberDoc:NotesDocument = seqView.getFirstDocument();
applicationScope.put("seqNumber",seqNumberDoc.getItemValueInteger("seqNumber") + 1);
seqNumberDoc.replaceItemValue("seqNumber",applicationScope.get("seqNumber"));
seqNumberDoc.save(true,true);
newSeqNum = applicationScope.get("seqNumber");
} catch(e) {
var seqNumberDoc:NotesDocument = database.createDocument();
seqNumberDoc.replaceItemValue("Form","cPanel");
seqNumberDoc.replaceItemValue("seqNumber",1);
applicationScope.put("seqNumber", 1);
seqNumberDoc.save(true,true);
newSeqNum = 1;
}
}
}
var seqNNNN:String = ("0000" + newSeqNum.toString()).slice(-4);
return seqNNNN;
}
正如你可以先看看它會在同步塊的下一個順序號,增加了一個,然後把數回applicationScope。
然後它將它轉換爲字符串,並添加額外的4個零,然後從中加上正確的4個字符。這將返回一個字符串並需要存儲在文本字段中。您不能將其存儲在數字字段中,因爲Notes會自動從該值中刪除前導零。
您可以通過將其添加到服務器端的JavaScript庫中,然後將其包含在一個運行重複控制來重複調用該函數的計算字段的簡單頁面中來測試此函數。這是我的測試頁面。
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.resources>
<xp:script src="/seqNum.jss" clientSide="false" />
</xp:this.resources>
<xp:repeat id="repeat1" rows="30" value="#{javascript:30}">
<xp:text escape="true" id="computedField1"
value="#{javascript:simplegetSequentialNumber();}" />
<xp:br id="br1" />
</xp:repeat>
</xp:view>
謝謝你的代碼,但是這隻增加了一個數字...... – gkidd 2012-03-22 13:50:10
我在代碼中發現了一個小錯字,我在第三行中使用了seqNNN而不是seqNNNN。 – 2012-03-22 14:11:58
我已經用一個如何調用它的例子更新了代碼。如果您只能將單個數字保存到文檔中,則意外將該字段設置爲數字字段。您不能在數字字段中存儲0001,筆記會自動將其轉換爲1.您必須將返回的值存儲在文本字段中。 – 2012-03-22 14:13:31