Skip to content

Instantly share code, notes, and snippets.

@alvarotuso
Created September 1, 2020 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvarotuso/0ccb604d0cc77ffb9d52db12af6fd349 to your computer and use it in GitHub Desktop.
Save alvarotuso/0ccb604d0cc77ffb9d52db12af6fd349 to your computer and use it in GitHub Desktop.
Format Salesforce Fields
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