+ Start a Discussion
konigswaggerkonigswagger 

CREATE new SObject using Javascript

I am using the Force.com-JavaScript-REST-Toolkit to access my data stored at Salesforce (using OAuth) in my Javascript applcation and was wondering how I would go about creating new records. I've seen many examples for creating SObjects and making the CREATE call in Java, but I am not sure how to do this in JS. Does anybody have a small snippet of code to show how to do this?

Rahul SharmaRahul Sharma

Hi konigswagger,

 

This is small code to create a Account record Using Javascript.

You can try querying, inserting, updating and many things which we usually perform in apex.

 

<apex:page >
 <apex:form >
  <apex:includeScript value="/soap/ajax/20.0/connection.js"/>
  <script type="text/javascript">
    function CreateNewAccount()
    {
      sforce.connection.sessionId = '{!$Api.Session_ID}';    //    get Session Id
      var objAccount = new sforce.SObject("Account");      //    Instantiate Account object
      var getName = prompt("Enter Name of the Account","Test Account");
      objAccount.Name = getName;
      var result = sforce.connection.create([objAccount]);    //    Insert Operation
      window.open('/'+result[0].id);     //    Redirect to New Account
            }
  </script>
  <apex:outputText value="Click button to Create New Account"/><p/>
  <apex:commandButton onClick="CreateNewAccount()" value="New Account" />       
 </apex:form>
</apex:page>




Hope it Helps..

konigswaggerkonigswagger

does not seem to work despite following the specifications. Any tips? Should I include the __c? 

Is there any way to not use APEX? I am using the Force.com toolkit and it includes a method called create as follows:

 

    /*

     * Creates a new record of the given type.

     * @param objtype object type; e.g. "Account"

     * @param fields an object containing initial field names and values for 

     *               the record, e.g. {:Name "salesforce.com", :TickerSymbol 

     *               "CRM"}

     * @param callback function to which response will be passed

     * @param [error=null] function to which jqXHR will be passed in case of error

     */

    forcetk.Client.prototype.create = function(objtype, fields, callback, error) {

        this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/'

  , callback, error, "POST", JSON.stringify(fields));

    }

 

);

 

My code:

client.create("FinAccount__c",

  {

    :currency 'cash_money',

   },

  function(response){

alert('success');

  }

)

 

dkadordkador

What error are you getting?  And how are you getting aroud the same-domain policy?

konigswaggerkonigswagger

Figured it out thanks :) The syntax in the documentation was wrong. The second parameter is just a JSON object.

dkadordkador

Can you let me know where the error in the documentation is so I can get it fixed?

 

Thanks.

konigswaggerkonigswagger

 

    /*

     * Creates a new record of the given type.

     * @param objtype object type; e.g. "Account"

     * @param fields an object containing initial field names and values for 

     *               the record, e.g. {Name : "salesforce.com",

     *   TickerSymbol : "CRM"}

     * @param callback function to which response will be passed

     * @param [error=null] function to which jqXHR will be passed in case of error

 */

I changed the location of the colon and it now works.

dkadordkador

Ah, this is the documentation for that specific JS toolkit.  Thanks.