def handleAccountContact(record):
  """ 
    Input to this function is a single record from the import data
    Excel file.  Here, we perform two queries, one to get the AccountId
    and one to get the ContactId, then we add these as new fields to
    the record and pass it back to the caller.
    
    If the Account does not exit - create it, if the contact does not
    exist, then create that too.
  """
  global createMissingAccounts
  
  accountId = None
  result = sf.query(u"select Id, Name from Account where Name = '%s'" % (record['Company']).replace(u"'", u"\\'"))

  if result.size > 1:
    #r['Error'] = 'dup accounts'
    print "Warning: more then one Account record for %s - using first one" % record['Company'] 
    #writeErrorRec(record)
    #return None
    accountId = result.records[0].Id  #.encode('ascii', 'ignore')
    
  if result.size < 1:
    if createMissingAccounts == True:
      accountId = createAccount(record).id
    else:  
      record['Error'] = 'no account'
      print "Error: no Account record for %s" % record['Company']
      writeErrorRec(record)
      return None
  else:
    accountId = result.records[0].Id    

  record['AccountId'] = accountId

  # try contact lookup via email...
  if record['Email'] != None and len(record['Email']) > 0:
    result = sf.query(
      u"""
      select Id, FirstName, LastName, Title, Email from Contact
      where Email = '%s'
      """ % (record['Email'])
    )
  else:
    result = None

  # ...if none, try contact lookup via first & last name
  if result == None or result.size < 1:
    result = sf.query(
      u"""
      select Id, FirstName, LastName, Title, Email from Contact
      where FirstName = '%s' and LastName = '%s'
      """ % (record['First Name'].replace(u"'", u"\\'") , record['Last Name'].replace(u"'", u"\\'") )
    )

  if result.size == 1:
    record['ContactId'] = result.records[0].Id
  elif result.size > 1:
    print result
    print "Warning: more then one Contact record for %s, %s  %s - using first found." \
      % (record['Last Name'], record['First Name'], record['Email'])
    record['ContactId'] = result.records[0].Id
  else:
    if createMissingAccounts == True:
      newContactResult = createContact(record)
      record['ContactId'] = newContactResult.id
    else:
      record['Error'] = 'no contact'
      print "Error: no Contact record for %s, %s %s" \
        % (record['Last Name'], record['First Name'], record['Email'])
      writeErrorRec(record)
      return None

  return record
 
    Input to this function is a single record from the import data
    Excel file.  Here, we perform two queries, one to get the AccountId
    and one to get the ContactId, then we add these as new fields to
    the record and pass it back to the caller.
    
    If the Account does not exit - create it, if the contact does not
    exist, then create that too.
  
recipes/Python/577550_Custom_import_script/recipe-577550.py