2

So I am using Jquery Ajax with rest API

trying to create Account Record using POST method but getting error

<apex:page>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <div>
        <input name="getList" class="btn" value="Create Account Record" type="button" onclick="createSobjectRecord()" />
    </div>
    <div id="warehouseList">
    </div>
    <script type="text/javascript">
        var $j = jQuery.noConflict();
        var sessionId = '{!$Api.Session_ID}';
        function createSobjectRecord() {
             var person = new Object();
             person.name = 'Test Ajax Jquery';
             $j.ajax({
                type: "POST",
                url: "/services/data/v35.0/sobjects/Account",
                headers: {"Authorization" : "OAuth " + sessionId},
                crossDomain : true,
                data: person,
                dataType: 'json',
                success: function (responseData, status, xhr) {
                    console.log(responseData);
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
    </script>
</apex:page>

This is the code I am trying to create Account using rest API but getting error.

enter image description here

  • r u jus exploring.. if its in same org u shd use js remoting / remote objects – Vamsi Krishna Jan 4 '16 at 10:29
  • @VamsiKrishna Yes I am just exploring. – Ratan Paul Jan 4 '16 at 10:30
5

Oh Got the solution. I think I need to spend more time with my research before asking question here.

Here is the working code. Actually after adding content type in header(also JSON.stringify()). It works

Below is my working code

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);
        }
    });

Complete code to help other may be it will helpful for someone.

GET, POST AND DELETE Method using Jquery

<apex:page>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <div>
        <input  class="btn" value="Get Account list" type="button" onclick="getSobjectList()" />
        <input  class="btn" value="Create Account Record" type="button" onclick="createSobjectRecord()" />
        <input class="btn" value="Delete Account Record" type="button" onclick="deleteSobjectRecord()" />
    </div>
    <div id="warehouseList">
    </div>
    <script type="text/javascript">
        var $j = jQuery.noConflict();
        var sessionId = '{!$Api.Session_ID}';
        function getSobjectList() {
            $j.ajax({
                type: "GET",
                url: "/services/data/v35.0/query?q="+''+"{!URLENCODE('SELECT Id, Name FROM Account')}",
                headers: {"Authorization" : "OAuth " + sessionId},
                crossDomain : true,
                dataType: 'application/json',
                success: function (responseData) {
                    console.log(responseData);
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
        var recordId;
        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.id);
                    recordId = responseData.id;
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
        function deleteSobjectRecord(){
            $j.ajax({
                type: "Delete",
                url: "/services/data/v35.0/sobjects/Account/"+recordId,
                headers : {
                        'Authorization' : "OAuth " + sessionId,
                        'Content-Type' : 'application/json'
                  },
                crossDomain : true,
                success: function (responseData, status, xhr) {
                    console.log(responseData);
                },
                error: function (request, status, error) {
                    console.log(request.responseText);
                }
            });
        }
    </script>
</apex:page>

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.