Skip to content

Instantly share code, notes, and snippets.

@dpilafian
Last active February 6, 2024 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpilafian/cbae8400a43a971063b4c480416e1b64 to your computer and use it in GitHub Desktop.
Save dpilafian/cbae8400a43a971063b4c480416e1b64 to your computer and use it in GitHub Desktop.
Groovy function to convert a Salesforce id15 to id18
def toSalesforceId18(String id) {
// Converts a Salesforce id15 to id18.
// Example:
// assert toSalesforceId18("001C000000o4Ooi") == "001C000000o4OoiIAE"
// Details:
// https://blog.centerkey.com/2014/08/groovy-convert-salesforce-id15-id18.html
// MIT License (c) 2019 Pilafian
def valueToCode = { ((it < 26 ? "A" : "0") as char) + it % 26 }
def binaryToCode = { valueToCode(Integer.parseInt(it, 2)) as char }
def tripletToBinary = { it.reverse().replaceAll(/[^A-Z]/, "0").replaceAll(/[^0]/, "1") }
def threeCodes = { it.replaceAll(/(.....)/, { binaryToCode(tripletToBinary(it[0])) }) }
return id?.size() == 15 ? id + threeCodes(id) : id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment