1

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

      <body>


    <div class="container">
<label for="name">Account Name</label>
      <input type="text" name="Name" id="name" />
      <br></br>
      <button id="button">Submit</button>
      <div id="status"></div>
    </div> <td class="labelCol">
                            <label for="j_id0:j_id3:sessionId" style="color:#4a4a56;font-size:91%;">
     session ID
                            </label>
                        </td>
                        <td class="data2Col">
                        <input id="j_id0:j_id3:sessionId" type="text" name="sessionId" class="PrequalStreet" style="width:246px;"
                                value="{!getSessionID()}" />
                        </td>

    </body>


    <script>
    $('#button').click(function(){
var sessionId = $( "input[id*='sessionId']" ).val();
console.log('sessionID is' + sessionId); 

  console.log('I am in ');
   var accountInfo = { "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"
     }]
   }
 }]
};
console.log('accountInfoJson ', accountInfo );
//var accessToken = decodeURI(GetURLParameter('access_token'));

var accountInfoJson = JSON.stringify(accountInfo);
console.log('accountInfoJson ' + accountInfoJson );
$.ajax({    type: "POST",
    url: "/services/data/v35.0/sobjects/Account",
    headers : {
        'Authorization' : "Bearer " + "{!$Api.Session_Id}" ,
         'Content-Type' : 'application/json'
                  },
       data : accountInfoJson,
       success : function(response){

                    console.log('response',response);
                            },
       error: function(response){
                    displayError(response);
                            }
                        });


            })
</script>
3

You can use the SObject Tree resource to create nested records.

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.

2

The response of the first call (Account) will contain the Id you need to then make the 2nd (Contact) and 3rd (Opportunity) calls. See here for the format.

So, after making your first call, if successful, then you can retrieve the Id out of those results and make the rest of the calls you want to make.

As to the JS syntax to get those calls.... well, that's for stack overflow. :)

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.