function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NikiG22NikiG22 

PLEASE HELP!!! - GUID (UUID) in Salesforce

Hello - I need help with building a GIUD (UUID) in SFDC? we are revamping our integration with our IT department and they want me to build a GUID in SFDC. I have never done this and have no idea where to start. I have found some sample code and this dosent work?

 

global class guidGenerator {

    private static String kHexChars = ’0123456789abcdefABCDEF’;

    global static String generateGUID(){

        String returnValue = ”;
        Integer nextByte = 0;
        for(Integer i = 0; i < 16; i++){
            if(i==4 || i==6 || i==8 || i==10){
                returnValue += ‘-’;
            }
            
            nextByte = (Math.round(Math.random() * 255)-128) & 255;

            if(i==6){
                nextByte = nextByte & 15;
                nextByte = nextByte | (4 <> 4);
            returnValue += charAt(kHexChars,nextByte & 15);
        }
        return returnValue;
    }

    public static String charAt(String str, Integer index){
        if(str == null){
            return null;
        }
        if(str.length() <= 0){
            return str;    
        }
        if(index = str.length()){
            return null;    
        }
        return str.substring(index, index+1);
    }
}

 

ANY help would be much appriciated.

 

Thank you!

NIki

Best Answer chosen by Admin (Salesforce Developers) 
NikiG22NikiG22

I have compleated the code. this code will generate a GUID and pdd it to my custom field called Employer_ID__c

 

trigger EmployerID on Account (before insert) {

For (Account a: Trigger.new){

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);

a.Employer_ID__c = guid;

 Cheers,

Niki

All Answers

kibitzerkibitzer

Try this:

 

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);

 

Dan

 

NikiG22NikiG22

thank you, so you have to forgive my ignorance, where do i add this? do i create a class and updayte my field? My Field is called Employer_ID__c. this field will need to be sent to my Back end system?

 

thanks agan for all your help.

 

Niki

kibitzerkibitzer

That's a harder question to answer. If all you need to do is place it on a field on an object, you'll likely use two steps:

1. Add the code to a before-create trigger on the object to set the field value.

2. Create a batch process to set the value on the field on existing objects.

 

Dan

 

 

NikiG22NikiG22

Perfect that did work thank you!

 

Last question how can i get the giud to show in my Employer_ID__c custom field? I need the field visable on the account?

kibitzerkibitzer

That's more of a UI question than an Apex code question. I assume you'll need to add it to the appropriate page layouts, make sure the field level security is set correctly to it's visible by the appropriate users, etc.

 

Dan

 

NikiG22NikiG22

I have compleated the code. this code will generate a GUID and pdd it to my custom field called Employer_ID__c

 

trigger EmployerID on Account (before insert) {

For (Account a: Trigger.new){

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);

a.Employer_ID__c = guid;

 Cheers,

Niki

This was selected as the best answer
Maureen EzekwugoMaureen Ezekwugo
This technique will not work for creating valid version 3+ UUIDs.  See:  https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_3_.28MD5_hash_.26_namespace.29