Last active
December 18, 2015 03:49
-
-
Save kvarga/5721240 to your computer and use it in GitHub Desktop.
create salesforce fields from a csv
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
# THIS CODE IS VERY UGLY | |
# I AM NOT SORRY | |
# Copyright 2013 Kyle Varga | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
import sys | |
import os | |
import argparse | |
import csv | |
import re | |
parser = argparse.ArgumentParser(description='Create Salesforce Object Metadata from a CSV') | |
parser.add_argument('filepath', metavar='csvfile', type=str, | |
help='path to amex export csv') | |
args = parser.parse_args() | |
# TODO: Add support for more fields | |
# TODO: Add support for different date formats | |
salesforce_fields_regex = { | |
'^[0-9.]+$': 'Number', | |
'^[0-9]{2}-[0-9]{2}-[0-9]{2,4}$': 'Date', | |
'^.*$': 'Text' | |
#checkbox | |
#currency | |
#date | |
#datetime | |
#percent | |
#text | |
#text area | |
#text area long | |
#text area rich | |
# text encrypted | |
# url | |
} | |
# Read CSV | |
def salesforcify(name): | |
name = re.sub('[\W#]+', '', name) | |
name = re.sub('[_]+$', '', name) | |
name = name + '__c' | |
return name | |
#TODO: Would be nice if I could set Precision, Scale based off data in SFDC | |
def create_salesforce_field(fullname, type, example): | |
fieldname = salesforcify(fullname) | |
if type == 'Number': | |
return ''' | |
<fields> | |
<fullName>%s</fullName> | |
<defaultValue>0</defaultValue> | |
<description>Automatically created field. Example: %s</description> | |
<externalId>false</externalId> | |
<inlineHelpText></inlineHelpText> | |
<label>%s</label> | |
<precision>18</precision> | |
<scale>2</scale> | |
<type>%s</type> | |
</fields>''' % (fieldname, example, fullname, type) | |
elif type == 'Date': | |
return ''' | |
<fields> | |
<fullName>%s</fullName> | |
<defaultValue></defaultValue> | |
<description>Automatically created field. Example: %s</description> | |
<externalId>false</externalId> | |
<inlineHelpText></inlineHelpText> | |
<label>%s</label> | |
<type>%s</type> | |
</fields>''' % (fieldname, example, fullname, type) | |
else: | |
return ''' | |
<fields> | |
<fullName>%s</fullName> | |
<defaultValue></defaultValue> | |
<description>Automatically created field. Example: %s</description> | |
<externalId>false</externalId> | |
<inlineHelpText></inlineHelpText> | |
<label>%s</label> | |
<length>255</length> | |
<type>%s</type> | |
</fields>''' % (fieldname, example, fullname, type) | |
print '-- reading csv --' | |
reader = csv.DictReader(open(args.filepath)) | |
data = [ | |
row | |
for row in reader | |
] | |
keymap = {} | |
# Horribly inefficient | |
for key in data[0].keys(): | |
for row in data: | |
keydata = row[key] | |
if keydata != '': | |
#print 'keydata: ', keydata | |
for i,regex in enumerate(salesforce_fields_regex.keys()): | |
if key in keymap and keymap[key] > i: | |
continue | |
if re.match(regex, keydata): | |
keymap[key] = i | |
continue | |
# How does this look so far? | |
import pprint | |
pprint.pprint(keymap) | |
print '---' | |
# Convert into <fields> | |
keymap2 = { | |
key : salesforce_fields_regex[salesforce_fields_regex.keys()[keymap[key]]] | |
for key in keymap.keys() | |
if key != '' | |
} | |
sortedkeys = keymap2.keys() | |
sortedkeys.sort() | |
for key in sortedkeys: | |
for row in data: | |
if row[key] != '': | |
#print "%s : %s : %s" % (key, keymap2[key], row[key]) | |
fullname = key | |
example = row[key] | |
type = keymap2[key] | |
print create_salesforce_field(fullname, type, example) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment