Last year, we talked about how to get an access token to make api request to Salesforce.

Now, we are going to use this token and create a something with it. Again, the method is written in Python. You may, of course, write it in a different language, but I found python to be a very handy programming language when I try to get things up and running quickly.

def createRecord(instance, sessionId, recordApiName, fields):
    url = 'https://'+instance+'.salesforce.com/services/data/v37.0/sobjects/'+recordApiName+'/'
    headers = {
        'Content-Type': "application/json",
        'Sforce-Auto-Assign': "FALSE",
        'Authorization': "Bearer " + sessionId
    }
    payload = json.dumps(fields, ensure_ascii=False)
    response = requests.request("POST", url, data=payload, headers=headers)
    return response.text

Notice the method takes four inputs,
– instance is simply the org you are using, ‘cs6’ for example;
– sessionId is what we got from the previous article;
– recordApiName is basically the api name for the object you are updating, e.g. Account
– fields, this is a JSON formatted string that contains the key value pairs of the data you want to insert/update in Salesforce.

There you have it, a simple and useful way to manipulate data in your Salesforce org.

Advertisements