Using @Unique() in XPages

Hi.

In “Traditional” Notes Development I might often put a unique field on a document using a computed when composed field with the value of @Unique. The format of the value is something like this:

DLEY-7R4LHH.

I know the format is first based on username/servername and the second part is based on time. But I really don’t care about that as long is it’s a unique value.

In XPages if I try to set the default value of a field to the JavaScript implementation of @Unique(), I get back a value, but in a vastly different format.

Another running of this produces this:

Does anyone know what this value is? It looks more random then unique. Can I expect it to be unique? Is there a way to keep the format of “Traditional” Notes @Unique for consistency? Or is this new value more robust?

I know I can get the Traditional value if I tell the XPage to ComputewithForm, but I’m trying to avoid that if possible to I can keep as much of the logic as possible in one place.

Thanks for any information!!

Subject: Have you tried…

… calling session.evaluate(“@Unique()”). This should give you the expected result.@Unique() is implemented a little bit differently in Java.

Subject: @unique in XPages

Phillipe,Thanks for the response! Sorry I didn’t see it sooner.

I did try session.evaluate(“@Unique()”) and get a Command Not Handled Exeception Error.

I’m using:

session.evaluate(“@Unique()”)

As the code in the default value of the field.

I’ll try to play with this more. I’d rather not have to rely on Compute With Form is there is an XPages method to get the same result.

Subject: the right data type

I know the post is kinda old, but to help other new comers with the answer.

evaluate returns a java.util.Vector. So you’ll want to create a String variable and cast the result to a String. See below:

String ID = (String)session.evaluate(“@Unique”).elementAt(0);

if you use session.evaluate(“@Unique”).toString() then your result will be a string wrapped with [ and ]