2

I have already asked a similar question that was initially answered: Generate another ID by encoding the Salesforce ID

The suggestion seemed to have fixed the issue but after extensive use and testing I can see that the special characters are less frequent but are still generated from time to time.

Long story short - I want to generate a unique identifier for a record that is not incremental (too simple) and is not the record ID. I intend to use this ID in a url parameter so it needs to be "URL friendly" - this was the issue with my last solution, it contained characters like "/" and "%" which broke the parameter.

EDIT/UPDATE Criteria:

  • I want to be able to generate a new ID based on the existing Salesforce ID. This way it will always be unique, as the Salesforce ID is always unique.
  • The solution in the above link was perfect. It cerated a new string based on a key and the Salesforce ID field BUT the only downfall was that I couldn't use this generated string as it contained foreign characters that break the parameter in a URL.
  • I would love to use the existing solution, but just ensure that no characters that are not URL parameter compatible are not included.

Does anyone have a method of doing this? Preferably in a trigger?

5
  • y can't u use a simple text field populated with numbers taken from created datetime value ? Commented Feb 17, 2015 at 2:58
  • While I can see your logic I was after a string that was completely disconnected from the data - completely random and guaranteed to be a)unique and b)url friendly Commented Feb 17, 2015 at 3:01
  • well the combined numbers u get r still unique and url friendly and simple to generate. but if u don't prefer that, will u be interested in guid style - salesforce.stackexchange.com/questions/4073/… Commented Feb 17, 2015 at 4:17
  • What's wrong with URL-encoding/decoding your url params? Commented Feb 18, 2015 at 23:57
  • it still generates strings with "%" and "/" in them which breaks the parameters. and If I remove them manually I am stripping the guarantee that these string will be unique... ie 'ab%c' and 'a%bc' without the '%' are both 'abc' Commented Feb 19, 2015 at 0:07

1 Answer 1

1

I had a task like this, and my solution was to generate a random string, combining Math.random() and String.fromCharArray() to generate fairly random account numbers that were unlikely to collide with existing values. In our case, we chose a six character alpha numeric string with no vowels, by choosing arbitrarily long random strings, and using String.replaceAll() to remove any unwanted characters.

It's true that Math.random() isn't cryptographically secure, but so far we have generated hundreds of thousands of unique values without a single collision (at least, after retries). You might also include as unique index on the field to make sure it's easy to query.

Also, see apex-lang for other examples of random string generation. See also my answer where I explore several algorithms of my own design (not unique, just independently discovered). Go down near the end to see my String.fromCharArray implementations.

1
  • Your answer shows some very interesting solutions but it is not quite what i am looking for. I will edit my above post to better specify the criteria of the solution I am looking for... Commented Feb 18, 2015 at 23:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .