---
title: "Generate UUID in Apex"
url: "https://blog.beyondthecloud.dev/tips/generate-uuid-in-apex"
category: "Apex"
published: "2024-01-02"
---

# Generate UUID in Apex

UUID, or Universally Unique Identifier, is a 128-bit label with a probability of duplication considered close enough to zero to be negligible.

### Easily generate UUID in Apex

Ever needed a UUID in Salesforce?

UUID, or Universally Unique Identifier, is a 128-bit label with a probability of duplication considered close enough to zero to be negligible.

```bash
02c90c10-dc3f-4478-8ac5-fa869de4b061
```

You don't need to build your own class to obtain the UUID in Apex. To retrieve UUID v4, all you have to do is use this one-liner:

```apex
(String)((Map<String,Object>) JSON.deserializeUntyped(new Auth.JWT().toJSONString())).get('jti')
```

The code snippet is also pasted in the first comment, ready for you to copy!
