Peter Della-Nebbia
Peter Della-Nebbia

Reputation: 827

How to generate Notes-like unique string in SSJS using @Unique()?

In Notes formulas the @Unique function return a unique string based on the Notes user's ID name and time. So when I create several documents in Notes the first part of the unique string is always derived from my name, Paul Della-NebbiA, like this: PDNA-AJBQYD PDNA-AJBQYL PDNA-AJBQYN

If I login to Notes with a different ID, App DEV2, the first part of the @Unique string is then as follows: ADEV-AJBQZG ADEV-AJBQZQ ADEV-AJBQZS

I want to produce a similar result in SSJS to generate a unique identifier for new documents when they are created. However, it appears that the first part of the @Unique() string is always derived from the server and domain name.

Here is the SSJS code I am using to generate the unique id:

newdoc.replaceItemValue("AssignmentId",session.evaluate("@Unique").elementAt(0) ); 

So is there a way in SSJS to get @Unique() to work like it does in Notes, and generate the first four characters based on the authenticated user name?

Upvotes: 1

Views: 538

Answers (2)

C. Lackey
C. Lackey

Reputation: 98

This is likely a long way around, but is what I use in my environment to mimic Notes unique ID.

var user = @Name("[CN]",session.getEffectiveUserName());
var last = @UpperCase(@Right(user," "));
var first = @UpperCase(@Left(user," "));
var a = @Left(first,1);
var b = @Left(last,2);
var c = @Right(last, 1);
var id = session.evaluate("@Unique").elementAt(0);
var d = @Right(id,6);

if (user == "Anonymous") {
return id;
}else{
a + b + c +  "-" + d;
};

Upvotes: 7

Frank van der Linden
Frank van der Linden

Reputation: 1271

Maybe you can try it via Java? In my code I use java.util.UUID.randomUUID().toString()

Upvotes: 2

Related Questions