To generate unique identifier (UUID) using Salesforce Apex, randomUUID() from UUID class can be used.
Sample Code:
UUID randomUUID = UUID.randomUUID();
System.debug(
'randomUUID is ' +
randomUUID
);
System.debug(
'randomUUID String value is ' +
randomUUID.toString()
);