Algorithm to generate the unique id in salesforce using Apex

There might be scenarios where we as a developer needs to generate random unique ID’s to be used for external integrations in Apex.

Here is a simple method which generates the unique code everytime.


public static String getUUID()
{
        Blob b = Crypto.GenerateAESKey(128);
        String h = EncodingUtil.ConvertTohex(b);
        String guid = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20);
        system.debug(guid);
        return guid;
    }

Sumanth A [03/08/2019]