function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Behzad Bahadori 18Behzad Bahadori 18 

POST Ajax method with Jquery to create new account contact and Opportunity

I am trying to create New Account, Then Contact and Opportunity that is assigned to that account . but use Post method, so have them all in one page. I dont wanna create any class, I know I can do Account using method as it follows, however I am not sure if I can do the samething to create new contact and opportunity since I need account ID. Ill put fields on the page to ask user to enter new information
function createSobjectRecord() {
var accountInfo = {
    "Name": "Testing Jquery with Rest"
};
var accountInfoJson = JSON.stringify(accountInfo);
$j.ajax({
    type: "POST",
    url: "/services/data/v35.0/sobjects/Account",
    headers: {
        'Authorization': "OAuth " + sessionId,
        'Content-Type': 'application/json'
    },
    crossDomain: true,
    data: JSON.stringify(accountInfo),
    dataType: 'json',
    success: function(responseData, status, xhr) {
        console.log(responseData);
    },
    error: function(request, status, error) {
        console.log(request.responseText);
    }
});

​

 
Best Answer chosen by Behzad Bahadori 18
NagendraNagendra (Salesforce Developers) 
Hi Behadori,

You can use the SObject Tree resource to create nested records(https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_composite_sobject_tree_create.htm).
 
POST /services/data/v42.0/composite/tree/Account HTTP/1.1
Host: <instance>.my.salesforce.com
Authorization: OAuth ...
Content-Type: application/json
Accept: application/json
Content-Length: ...

{ "records": [{
   "attributes": { "type": "Account", "referenceId": "acc1" },
   "name": "Testing jQuery With Rest",
   "Contacts": {
     "records": [{
       "attributes": { "type": "Contact", "referenceId": "con1" },
       "firstname": "John",
       "lastname": "Doe"
     }]
   },
   "Opportunities": {
     "records": [{
       "attributes": { "type": "Opportunity", "referenceId": "opp1" },
       "name": "Closing Soon",
       "closedate": "2018-09-01",
       "stagename": "Prospecting"
     }]
   }
 }]
}
This will eliminate the round trips to the servers, allowing you to create all the records in one call. You'll want to read the documentation for more information.

The actual code will look very similar:
$j.ajax({
    type: "POST",
    url: "/services/data/v42.0/composite/tree/Account",
    headers: {
        'Authorization': "OAuth " + sessionId,
        'Content-Type': 'application/json'
    },
    crossDomain: true,
    data: JSON.stringify(payload),
    dataType: 'json',
    success: function(responseData, status, xhr) {
        console.log(responseData);
    },
    error: function(request, status, error) {
        console.log(request.responseText);
    }
});
Obviously, the data structure changes a bit (attributes, records), but it's not much more complicated than the multiple-trip pattern.

Hope this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra