Created
September 1, 2020 19:58
-
-
Save alvarotuso/0ccb604d0cc77ffb9d52db12af6fd349 to your computer and use it in GitHub Desktop.
Format Salesforce Fields
This file contains hidden or 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
import json | |
TYPE_MAP = { | |
'boolean': 'bool', | |
'int': 'int', | |
'double': 'float', | |
'date': 'date', | |
'datetime': 'datetime', | |
'address': 'string', | |
'email': 'string', | |
'textarea': 'string', | |
'picklist': 'string', | |
'multipicklist': 'string', | |
'percent': 'number', | |
'phone': 'string', | |
'url': 'string', | |
'currency': 'string', | |
'string': 'string', | |
'id': 'string', | |
'reference': 'string', | |
} | |
LIST_FIELDS = {'multipicklist'} | |
for obj in ('campaign', 'account', 'opportunity'): | |
with open(f'./{obj}.json') as f: // this is equivalent to the API response of the describe endpoints | |
sdfc_definition = json.loads(f.read())['fields'] | |
parsed_fields = [] | |
for field in sdfc_definition: | |
parsed_field = { | |
'name': field['name'], | |
'display_name': field['label'], | |
'type': TYPE_MAP[field['type']], | |
'multivalued': field['name'] in LIST_FIELDS, | |
} | |
if field['type'] in ('picklist', 'multipicklist'): | |
parsed_field['enum'] = [ | |
{ | |
'value': v['value'], | |
'display_value': v['label'] | |
} for v in field['picklistValues'] if v['active'] | |
] | |
parsed_fields.append(parsed_field) | |
with open(f'./{obj}_attribute_definitions.json', 'w') as w: | |
w.write(json.dumps(parsed_fields)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment