Skip to content

Instantly share code, notes, and snippets.

@martyychang
Created January 31, 2020 17:40
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 martyychang/0082f4742b8539b1fde0c10af4f8d095 to your computer and use it in GitHub Desktop.
Save martyychang/0082f4742b8539b1fde0c10af4f8d095 to your computer and use it in GitHub Desktop.
List all field labels and field names for an object
import csv
import os
import xml.etree.ElementTree as ET
FIELDS_SUBDIR = 'fields'
METADATA_NAMESPACE = 'http://soap.sforce.com/2006/04/metadata'
OBJECTS_DIR = 'force-app/main/default/objects'
namespaces = {
'': METADATA_NAMESPACE
}
output_dir = '.sfdx/tmp'
sobject_name = 'Contact'
def get_field_label(field_et):
label_el = field_et.getroot().find('label', namespaces)
full_name_el = field_et.getroot().find('fullName', namespaces)
return '!%s' % full_name_el.text if label_el is None else label_el.text
def get_sobject_fields_dir(sobject_name):
return '%s/%s/%s' % (OBJECTS_DIR, sobject_name, FIELDS_SUBDIR)
# main script
if __name__ == '__main__':
# Initialize the CSV rows
field_list_csv = [['Field Label', 'Field API Name']]
# Populate the CSV rows
sobject_fields_dir = get_sobject_fields_dir(sobject_name)
for field_filename in os.listdir(sobject_fields_dir):
field_file = '%s/%s' % (sobject_fields_dir, field_filename)
print('found: %s' % field_file)
field_et = ET.parse(field_file)
field_list_csv.append([
get_field_label(field_et),
field_filename.split('.')[0]
])
# Write the CSV rows
with open('%s/%s.csv' % (output_dir, sobject_name), 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(field_list_csv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment