You need to sign in to do that
Don't have an account?
NikiG22
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
I have compleated the code. this code will generate a GUID and pdd it to my custom field called Employer_ID__c
Cheers,
Niki
All Answers
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
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
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
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?
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
I have compleated the code. this code will generate a GUID and pdd it to my custom field called Employer_ID__c
Cheers,
Niki