如果以全大写格式返回了Salesforce的18位ID(例如,通过第三方程序),则这将使Salesforce无法读取18位ID。如何使用python修复此全大写ID?
答案 0 :(得分:0)
本文是对问题“ Converting uppercased 18-digit Id to valid Id" on the Salesforce Stack Exchange”的重新构建。此处发布的解决方案将Adrian Larson的解决方案转换为将Upcase ID从APEX修复为Python 3。
将APEX解决方案转换为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
示例:
repairCasing('003i000001IIGW7AAH')
出局:
'003i000001IIGw7AAH'
注意:
对于学习者,
toUpper = [item for sublist in toUpper for item in sublist]
是一种将数组内的数组展平为单个数组的列表理解方法(在How to make a flat list out of list of lists?处归Alex Martinelli所有),等效于:
for sublist in toUpper:
for item in sublist:
newList.append(item)
toUpper = newList
类似地:
output = ''.join([x18DigitId[x].upper() if toUpper[x] else x18DigitId[x].lower() for x in range(0,15)]) + x18DigitId[15:].upper()
与列表理解等效:
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()