Last active
February 6, 2024 19:45
-
-
Save dpilafian/cbae8400a43a971063b4c480416e1b64 to your computer and use it in GitHub Desktop.
Groovy function to convert a Salesforce id15 to id18
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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