If a Salesforce 18 digit ID is returned (say, by a third-party program) in an all-capital format, this renders the 18 digit ID unreadable by Salesforce. How can I use python to fix this all-capital ID?
Asked
Active
Viewed 152 times
1 Answers
0
This post is a reconstruction of the question "Converting uppercased 18-digit Id to valid Id" on the Salesforce Stack Exchange". The solution posted here converts Adrian Larson's solution to repairing the upcase ID from APEX to Python 3.
APEX solution converted to Python 3:
def getBitPatterns(c):
CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345'
index = CHARS.find(c)
result = []
for bitNumber in range(0,5):
result.append((index & (1 << bitNumber)) != 0)
return result
def repairCasing(x18DigitId):
if len(x18DigitId) < 18:
return 'Error'
toUpper = []
toUpper.append(getBitPatterns(x18DigitId[15:16]))
toUpper.append(getBitPatterns(x18DigitId[16:17]))
toUpper.append(getBitPatterns(x18DigitId[17:18]))
toUpper = [item for sublist in toUpper for item in sublist]
output = ''.join([x18DigitId[x].upper() if toUpper[x] else x18DigitId[x].lower() for x in range(0,15)]) + x18DigitId[15:].upper()
return output
Example:
repairCasing('003i000001IIGW7AAH')
Out:
'003i000001IIGw7AAH'
Note:
For learners,
toUpper = [item for sublist in toUpper for item in sublist]
is a list-comprehension method of flattening arrays within an array into a single array (credit to Alex Martinelli at How to make a flat list out of list of lists?), and is the equivalent of:
for sublist in toUpper:
for item in sublist:
newList.append(item)
toUpper = newList
Similarly:
output = ''.join([x18DigitId[x].upper() if toUpper[x] else x18DigitId[x].lower() for x in range(0,15)]) + x18DigitId[15:].upper()
is the list-comprehension equivalent of:
output = ''
for i in range(0,15):
c = x18DigitId[i:i+1]
if toUpper[i]:
output += c.upper()
else:
output += c.lower()
output += x18DigitId[15:18].upper()

Jwok
- 646
- 9
- 23