Search code examples
pythonjsonflasksalesforcesimple-salesforce

Simple-Salesforce sf.Contact.create JSON response


I'm new to SF, Python and Flask, which are the tools I'm using to build up a little web UI for registering (creating a new contact on SF).

I found Simple-Salesforce and it works pretty well for me. So, right now I have this code that is working perfectly so far:

# User Registration
@app.route('/register', methods=['GET', 'POST'])
def register():
  form = RegisterForm(request.form)
  if request.method == 'POST' and form.validate():
    FirstName = request.form['FirstName']
    LastName = request.form['LastName']
    AccountId = request.form['AccountId']
    Birthdate = request.form['Birthdate']
    # password = sha256_crypt.encrypt(str(form.password.data))
    Home_Endpoint__c = request.form['Home_Endpoint__c']
    # Execute query
    sf.Contact.create({'LastName':LastName,'FirstName':FirstName,'AccountId':AccountId, 'Birthdate':Birthdate, 'Home_Endpoint__c':Home_Endpoint__c, 'Test_User__c':1 })
    flash('You are now registered', 'success')
  return render_template('register.html', form=form)

What I need is to catch/read the SF dictionary JSON response with the new created id as shown in the Record Management section:

Record Management

To create a new ‘Contact’ in Salesforce:

sf.Contact.create({'LastName':'Smith','Email':'example@example.com'})

This will return a dictionary such as {u'errors': [], u'id': u'003e0000003GuNXAA0', u'success': True}

So that I can keep working with the new registered member on other processes.

I will appreciate if someone gives me a hand on this.


Solution

  • Assign the function call to a variable and then check the value of the variable.

    foo = sf.Contact.create({'LastName':'Smith','Email':'example@example.com'})