Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

random uuid in user maco

I want to write a Confluence user macro, which should get a unique uuid. Currently I fail to randomly generate this UUID.
The format should look like "#c199f52d-9724-4911-ad47-479e937d3b88".
I guess I can solve this, if someone shows me how I can access the java function Math.random(); from my user macro.

2 answers

1 accepted

0 votes
Answer accepted

This is how I do it. I was never able to find a good way to generate a random number or find a way to get at Math.random() but this seems to work well.

#set( $id = $action.dateFormatter.calendar.timeInMillis )

Even when the user macro is used multiple times per page I find that I get different mills. So for velocity I reference it this way ...

<div id="mydiv-$id"></div>

 and then I can target it with JavaScript this way.

var id = '$id';

var stuff = AJS.$('#' + id).html();

Thanks for your help. I will test it on occasion. For now my hash solution (see above) works fine for me.

0 votes
Bill Bailey Community Leader Mar 23, 2021

For my user macros, I don't worry about that. Confluence will generate what it needs. Even if I am wrapping an existing macro, I delete that reference. It works fine.

I want to modify the rendered results via JavaScript so I need a html object id for reference the right object on the page if a user used the macro multiple times on the same page.

Bill Bailey Community Leader Mar 23, 2021

Hmm, seems a bit hard. You could create your own ID, but you would then need to increment it each time, which complicated. And seems like a brittle solution to rely on javascript to modify an object based on ID that could change.

Is there a way to do it within the user macro so that it is dynamic?

Maybe sharing a bit more detail on what you are trying to do would help?

I have now solved this by forming a hash over all input parameters. This should be sufficient for my purposes.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Confluence

Confluence Mythbusters: Does Atlassian even use Confluence?

Hi, Confluence collaborators! As part of #Confluence-Collaboratory month, we’ve created a very special Mythsbusters segment, where we're dive into an interesting myth and uncover the truth behind i...

1,474 views 7 29
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you