Sign up ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am using following soObject call to insert new row in the Account table at Salesforce server :

curl -H "Authorization:OAuth use your session_id" -H "Content-Type: application/json" -d @Test.json "

Now I need to insert an Account, Contacts and Address (in Address__c) as a single transaction means If insertion to any of the Account, Contact or Address fails, all three insertion should roll back.

How can I achieve this ?

share|improve this question
@nagarjuna given link is talking about apex code, While I am inserting data in salesforce from an external system using sobjects. – r.bhardwaj Aug 19 '14 at 5:43
a custom apex rest service that accepts a json POST payload of all the objects you need to insert is the way to go – crop1645 Aug 19 '14 at 6:19
see also @sfdcfox answer here - – crop1645 Aug 19 '14 at 6:32

1 Answer 1

If you want all that to be in 1 single transaction and 1 callout, you might need to write APEX REST Webservice that will get the all the data in a single call and store it as account, contact and any other object.

In this particular scenario let's assume your JSON looks something like this:

    "account" : {
        "name" : "test account name",
        "contact" : {
            "firstname" : "John",
            "lastname" : "Smith",
            "address" : {
                "street" : "Flinders St",
                "city" : "Melbourne",
                "state" : "VIC",
                "country" : "Australia",
                "postcode" : "3000"

If that's the case then I recommend you write your webservice like this:

global with sharing class YourWebServiceName
    global class GenericResponse
        public Boolean success;
        public String errorMessage;
        public String result;

        public GenericResponse(Boolean s, String e)
            if (s == true)
                this.errorMessage = '';
                this.result = e;
                this.errorMessage = e;
                this.result = '';

            this.success = s;

    global class CustomAccount
        public String name;
        public CustomContact contact;

    global class CustomContact
        public String firstname;
        public String lastname;
        public CustomAddress address;

    global class CustomAddress
        public String street;
        public String city;
        public String state;
        public String country;
        public String postcode;

    global static GenericResponse createNewAccount(CustomAccount account)
        // do your ifs and elses and check whether the JSON structure is right

        Savepoint savePoint = Database.setSavepoint();

            Account accountRecord = new Account ();
            accountRecord.Name =;
            insert accountRecord;

            Contact contactRecord = new Contact ();
            contactRecord.AccountId = accountRecord.Id;
            contactRecord.FirstName =;
            contactRecord.LastName =;
            insert contactRecord;

            // I believe it's a custom object and it's related to the contact
            Address__c addressRecord = new Address__c ();
            addressRecord.Contact__c = contactRecord.Id
            addressRecord.Street__c =;
            // and so forth
            insert addressRecord;

            return new GenericResponse(true, 'Success');
        catch (Exception e)
            return new GenericResponse(false, 'Error occured creating the records - ' + e.getMessage());

And finally you can call this method by doing a POST request to where CSX is your org name.

Don't forget to authenticate first and get the session id / access token and specify it in the header of your http request :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.