Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Generate random token number

Not applicable

I would like to generate a random token number something like UUID.randomUUID().

Is there a provision to generate or something java script equivalent.

Solved Solved
2 12 3,218
2 ACCEPTED SOLUTIONS

ceber
Former Googler

EDIT

While the original answer here still *works*, there is a better, probably easier way. You can use AssignMessage.

<AssignMessage name='AM-1'>
  <AssignVariable>
    <Name>uuid</Name>
    <Template>{createUuid()}</Template>
  </AssignVariable>
</AssignMessage>

original answer (2016)

Javascript can generate rfc4122-compliant guids. One example: https://gist.github.com/Nickdouille/8644841

I've done similar in a javascript policy without issue.

View solution in original post

Not applicable

You could try something like this:

var transactionId = "";
transactionId = generateUUID();

function generateUUID(){
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x7|0x8)).toString(16);
    });
    return uuid;
}

View solution in original post

12 REPLIES 12