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 "https://ap1.salesforce.com/services/data/v20.0/sobjects/Account/

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 ?


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 = account.name;
            insert accountRecord;

            Contact contactRecord = new Contact ();
            contactRecord.AccountId = accountRecord.Id;
            contactRecord.FirstName = account.contact.firstname;
            contactRecord.LastName = account.contact.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 = account.contact.address.street;
            // 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 https://CSX.salesforce.com/services/apexrest/account 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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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