I am trying to generate another ID in Salesforce using a after insert, after update trigger. I wish to use this unique identifier as a URL parameter so it has to contain characters that are url parameter friendly.
I have written a simple trigger that generates a unique string based on the id of the record:
trigger InvitationKeyAfterInsertUpdate on CampaignMember (after insert, after update) {
for (CampaignMember i : Trigger.new) {
//Only generate key when value not present
if (i.Key__c == null) {
//Random private key to ensure Key__c is secure (can't be guessed/generated)
Blob privateKey = EncodingUtil.base64Decode('046QdQEH24AgGafkkot8gg==');
//We use encryption on the Id instead of hashing because encryption will not produce collisions. This will never need to be decrypted.
update new CampaignMember (Id = i.Id, Key__c = EncodingUtil.base64Encode(Crypto.encryptWithManagedIV('AES128', privateKey, Blob.valueOf(i.Id))));
}
}
My issue is that when the string is generated, it contains characters such as "/" and "+" that break the url parameter, so in theory I would like the string to only contain A-Za-z0-9.
I can of course strip the characters once the string is generated, but my issue is that if I have the two string 'ab+c' and 'a+bc', when the character '+' is stripped they will both be 'abc' which defeats the whole purpose of ensuring the fields will be unique.
Can anyone help out?