Bill F
Bill F

Reputation: 2087

Error when trying to setTitle on new DB

I'm working on an Archiving Application with this code in it:

var arcName:String = "Archives/" + aDBName + "-Backup-" + toDay + "-" + thisTime.slice(0,5);
var server:String = appDB.getServer();
var arcDB:NotesDatabase = appDB.createCopy(server, arcName);
arcDB.setTitle("This is a Test");

but it fails at the arcDB.setTitle - the new copy of the Database is created so there is no issue to that point.

This is from the IBM Knowledge base:

var db2:NotesDatabase = db.createCopy(null, "names2");
db2.setTitle("Copy of names");

I can't see the difference between these two pieces of code. Am I missing something?

Upvotes: 0

Views: 98

Answers (2)

Chris Toohey
Chris Toohey

Reputation: 146

If you don't care about the effectiveUser's access to the source database, their permissions to make a copy, and their access rights to create a NotesDatabase on the target Domino server, I'd absolutely second Fredrik's suggestion to use sessionAsSigner/WithFullAccess.

Additionally, I find it's best practice to use try/catches (helps with troubleshooting and errorHandling), object testing (.isOpen() when accessing a NotesDatabase), and returning an Object that can be read by the calling function.

Here's some example code that might help:

var copyNotesDatabase = function(dbName) {

    var isSuccessful = true;
    var responseMessage = "";
    try {
        //set appDB using dbName from function arguments
        var appDB = session.getDatabase(session.getServerName(),dbName);
        //var appDB = sessionAsSigner.getDatabase(session.getServerName(),"sourceDb.nsf");
        //var appDB = sessionAsSignerWithFullAccess.getDatabase(session.getServerName(),"sourceDb.nsf");
        if(appDB.isOpen()) {
            var arcName:String = "Archives/" + aDBName + "-Backup-" + toDay + "-" + thisTime.slice(0,5);
            var server:String = appDB.getServer();
            //arcDB will be created based on appDB permissions, ie effectiveUser, or sessionAsSigner, etc.
            var arcDB:NotesDatabase = appDB.createCopy(server, arcName);
            if(arcDB.isOpen()) {
                arcDB.setTitle("This is a Test");
                responseMessage = "Successfully copied NotesDatabase!"
            } else {
                isSuccessful = false;
                responseMessage = "Unable to open copied NotesDatabase.";
            }
        } else {
            isSuccessful = false;
            responseMessage = "Unable to open source NotesDatabase.";
        }
    } catch(e) {
        print("Error from SSJS: " + e);
        isSuccessful = false;
        responseMessage = e;
    }

    return { status : isSuccessful, message : responseMessage };
}

With this function, you could do something like this:

function makeCopy(appName) {
    var fObj = copyNotesDatabase(appName);
    if(fObj.status) {
        return "Successfully copied " + appName;
    } else {
        return fObj.message;
    }
}

... at the very least, using a try/catch and returning your error will at least tell you why your current code isn't working. Hope this helps!

Upvotes: 1

Fredrik Norling
Fredrik Norling

Reputation: 3484

Usually when something doesn't work with XPages that relates to database or design objects the first thing I check is the Maximum Internet Name and password Access https://stackoverflow.com/a/23045860/1187943

Or change so I do the work using sessionAsSigner or sessionAsSignerWithFullAccess

Upvotes: 4

Related Questions