├── sf15to18.py ├── sf15to18.bas ├── sf15to18.gs ├── sf15to18.js ├── LICENSE ├── sf15to18.go ├── sf15to18.php └── README.md /sf15to18.py: -------------------------------------------------------------------------------- 1 | def sf15to18 (id): 2 | if not id: 3 | raise ValueError('No id given.') 4 | if not isinstance(id, str): 5 | raise TypeError('The given id isn\'t a string') 6 | if len(id) == 18: 7 | return id 8 | if len(id) != 15: 9 | raise ValueError('The given id isn\'t 15 characters long.') 10 | 11 | # Generate three last digits of the id 12 | for i in range(0,3): 13 | f = 0 14 | 15 | # For every 5-digit block of the given id 16 | for j in range(0,5): 17 | # Assign the j-th chracter of the i-th 5-digit block to c 18 | c = id[i * 5 + j] 19 | 20 | # Check if c is an uppercase letter 21 | if c >= 'A' and c <= 'Z': 22 | # Set a 1 at the character's position in the reversed segment 23 | f += 1 << j 24 | 25 | # Add the calculated character for the current block to the id 26 | id += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'[f] 27 | 28 | return id 29 | -------------------------------------------------------------------------------- /sf15to18.bas: -------------------------------------------------------------------------------- 1 | Function sf15to18(id As String) As String 2 | If Len(id) = 15 Then 3 | ' Generate three last digits of the id 4 | For i = 0 To 2 5 | Dim f 6 | f = 0 7 | 8 | ' For every 5-digit block of the given id 9 | For j = 0 To 4 10 | ' Assign the j-th chracter of the i-th 5-digit block to c 11 | Dim c 12 | c = Asc(Mid(id, (i * 5 + j + 1), 1)) 13 | 14 | ' Check if c is an uppercase letter 15 | If c >= 65 And c <= 90 Then 16 | ' Set a 1 at the character's position in the reversed segment 17 | f = f + (1 * (2 ^ j)) 18 | End If 19 | Next j 20 | ' Add the calculated character for the current block to the id 21 | id = id + Mid("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345", (f + 1), 1) 22 | Next i 23 | sf15to18 = id 24 | Else 25 | If Len(id) = 18 Then 26 | sf15to18 = id 27 | Else 28 | sf15to18 = "" 29 | End If 30 | End If 31 | End Function 32 | -------------------------------------------------------------------------------- /sf15to18.gs: -------------------------------------------------------------------------------- 1 | function sf15to18(id) { 2 | if (!id) throw new TypeError('No id given.'); 3 | if (typeof id !== 'string') throw new TypeError('The given id isn\'t a string'); 4 | if (id.length === 18) return id; 5 | if (id.length !== 15) throw new RangeError('The given id isn\'t 15 characters long.'); 6 | 7 | // Generate three last digits of the id 8 | for (var i = 0; i < 3; i++) { 9 | var f = 0; 10 | 11 | // For every 5-digit block of the given id 12 | for (var j = 0; j < 5; j++) { 13 | // Assign the j-th chracter of the i-th 5-digit block to c 14 | var c = id.charAt(i * 5 + j); 15 | 16 | // Check if c is an uppercase letter 17 | if (c >= 'A' && c <= 'Z') { 18 | // Set a 1 at the character's position in the reversed segment 19 | f += 1 << j; 20 | } 21 | } 22 | 23 | // Add the calculated character for the current block to the id 24 | id += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'.charAt(f); 25 | } 26 | 27 | return id; 28 | } 29 | -------------------------------------------------------------------------------- /sf15to18.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = (id) => { 4 | if (!id) throw new TypeError('No id given.'); 5 | if (typeof id !== 'string') throw new TypeError('The given id isn\'t a string'); 6 | if (id.length === 18) return id; 7 | if (id.length !== 15) throw new RangeError('The given id isn\'t 15 characters long.'); 8 | 9 | // Generate three last digits of the id 10 | for (let i = 0; i < 3; i++) { 11 | let f = 0; 12 | 13 | // For every 5-digit block of the given id 14 | for (let j = 0; j < 5; j++) { 15 | // Assign the j-th chracter of the i-th 5-digit block to c 16 | let c = id.charAt(i * 5 + j); 17 | 18 | // Check if c is an uppercase letter 19 | if (c >= 'A' && c <= 'Z') { 20 | // Set a 1 at the character's position in the reversed segment 21 | f += 1 << j; 22 | } 23 | } 24 | 25 | // Add the calculated character for the current block to the id 26 | id += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'.charAt(f); 27 | } 28 | 29 | return id; 30 | }; 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Markus Slabina 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /sf15to18.go: -------------------------------------------------------------------------------- 1 | package sf15to18 2 | 3 | import ( 4 | "unicode" 5 | "bytes" 6 | "errors" 7 | ) 8 | 9 | func Convert(id string) (string, error) { 10 | if len(id) == 18 { 11 | return id, nil 12 | } 13 | 14 | if len(id) != 15 { 15 | return "", errors.New("The given id isn't 15 characters long.") 16 | } 17 | 18 | const alphabet string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345" 19 | var longId bytes.Buffer 20 | 21 | // Write input id to output buffer as a starting point 22 | longId.WriteString(id) 23 | 24 | // Generate three last digits of the id 25 | for i := 0; i < 3; i++ { 26 | var f int = 0; 27 | 28 | // For every 5-digit block of the given id 29 | for j := 0; j < 5; j++ { 30 | // Assign the j-th chracter of the i-th 5-digit block to c 31 | var c rune = rune(id[i * 5 + j]); 32 | 33 | // Check if c is an uppercase letter 34 | if unicode.IsUpper(c) { 35 | // Set a 1 at the character's position in the reversed segment 36 | f += 1 << uint(j); 37 | } 38 | } 39 | 40 | // Add the calculated character for the current block to the id 41 | longId.WriteString(string(alphabet[f])); 42 | } 43 | 44 | return longId.String(), nil 45 | } 46 | -------------------------------------------------------------------------------- /sf15to18.php: -------------------------------------------------------------------------------- 1 | 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Salesforce 15-digit to 18-digit Id converter 2 | 3 | ## Introduction 4 | 5 | This repository contains some small code snippets in diffenent programming languages, that all have one purpose: converting Salesforce's case sensitive 15-digit Ids into the 18-digit Ids which aren't case sensitive. This might come in handy if you are working with a file system that isn't case sensitive and you want to write information to a file using the salesforce ids as filename for example. 6 | 7 | If you want to read more about the usage and conversion of Salesforce IDs or want to use an online converter based on the JavaScript implementation of this repository, go check out [the related article on my blog markus.codes](https://markus.codes/2021/02/03/salesforce-id-15-18-digit-lengths) 8 | 9 | ## Available Versions 10 | 11 | - JavaScript 12 | - Python 13 | - Go 14 | - OpenOffice/LibreOffice Macro 15 | - Google Spreadsheets script 16 | - PHP 17 | 18 | ## Usage 19 | 20 | ### Adding sf15to18.bas to Excel 21 | 22 | 1. Open or create a spreadsheet in Excel 23 | 2. Go to *Tools > Macro > Visual Basic Editor* 24 | 3. Right click on *This Workbook* and select *Insert > Module* 25 | 4. Copy and paste the contents of `sf15to18.bas` to the editor 26 | 5. Save the macro 27 | 6. Use it in your spreadsheet. If you want to convert the value of *Cell B1*, you need to use the following formula for example: `=sf15to18(B1)` 28 | 29 | ### Adding sf15to18.bas to OpenOffice/LibreOffice 30 | 31 | 1. Open or create a spreadsheet in OpenOffice/LibreOffice 32 | 2. Press *Alt-F11* and create a new Basic Macro 33 | 3. Create a new Macro 34 | 4. Copy and paste the contents of `sf15to18.bas` to the editor 35 | 5. Save the macro 36 | 6. Use it in your spreadsheet. If you want to convert the value of *Cell B1*, you need to use the following formula for example: `=sf15to18(B1)` 37 | 38 | ### Adding sf15to18.gs to Google Spreadsheets 39 | 40 | 1. Open or create a spreadsheet on Google Spreadsheets 41 | 2. Go to *Tools -> Script editor* 42 | 3. Select *Blank Project* 43 | 4. Copy and paste the contents of `sf15to18.gs` to the script editor 44 | 5. Save the script as `sf15to18` 45 | 6. Use it in your spreadsheet. If you want to convert the value of *Cell B1*, you need to use the following formula for example: `=sf15to18(B1)` 46 | 47 | ## How it works 48 | 49 | Getting the case insensitive 18-digit Id is an easy process which basically works like this: 50 | 51 | 1. The 15-digit Id is split into three 5-digit segments. 52 | 2. Each of these segments is then reversed. 53 | 3. Every uppercase letter is replaced with a 1, every other digit with a 0. 54 | 4. The three suffix-digits for the Id can then be looked up in a table, segment by segment (first segment gives the first digit, etc.). The table below shows an excerpt of such a table, which can be easily completed. 55 | 56 | ### Lookup table 57 | 58 | | segment result | suffix digit | 59 | |---|---| 60 | | 00000 | A | 61 | | 00001 | B | 62 | | 00010 | C | 63 | | 00011 | D | 64 | | 00100 | E | 65 | | 00101 | F | 66 | | ... | ... | 67 | | 11010 | 0 | 68 | | 11011 | 1 | 69 | | 11100 | 2 | 70 | | 11101 | 3 | 71 | | 11110 | 4 | 72 | | 11111 | 5 | 73 | 74 | ## Contributors 75 | 76 | |Major Contributors | | 77 | |:----|----:| 78 | |Markus Dang (formerly Slabina) |[![mslabina on Twitter](https://raw.githubusercontent.com/ExactTarget/fuelux/gh-pages/invertobird-sm.png)](https://twitter.com/mslabina) [![mslabina on Github](https://raw.githubusercontent.com/ExactTarget/fuelux/gh-pages/invertocat-sm.png)](https://github.com/mslabina) | 79 | 80 | ## License (MIT) 81 | 82 | __Copyright © 2018-2021 [Markus Dang (formerly Slabina)](https://github.com/mslabina)__ 83 | 84 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 85 | 86 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 87 | 88 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 89 | --------------------------------------------------------------------------------