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
arosysarosys 

insert data in custom object from javascript ?

I want to insert value of a javscript variable (SessionStorage html5 componenet) in to a custom object.

 

is it possible ?

Please suggest.

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
arosysarosys

I figured it out :

 

I aded these line in my vf page :

 

<apex:commandButton value="Place Order" onClick="addSessionStorage();"/>
<apex:includeScript value="/soap/ajax/28.0/connection.js"/>
<apex:includeScript value="/soap/ajax/28.0/apex.js"/>

 

and this in my javascript &colon;

 

sforce.connection.login("username", "passwordwithtoken");

 

 

and it worked.

All Answers

sfdcfoxsfdcfox

See:

 

http://www.salesforce.com/us/developer/docs/ajax/

 

But in short, yes.

 

var item = new sforce.SOBject('CustomObject__c');
item.Name = 'Hello World';
item.CustomField__c = new Date();
sforce.connection.insert([item],{onSuccess:alert, onFailure:alert});

 

arosysarosys

thanks for the reply , i am using following code to insert my data from java script to my custom object "Orderline_c"

But its not inserting data.

To debug i made alert after each line and found that 

after this line "var result = sforce.connection.create([orderlines]);" , alert is not coming and page is getting refreshed.

 

Following is my code.

 

function addSessionStorage() {
          
            alert('test');
            var userShoppingCart = sessionStorage.getObject('UserShoppingCart');
            alert('1');
           
           
            if (!userShoppingCart) {
             alert("No items in shopping cart");
              return;
          }
             
             var orderlines = [];
              alert('2');
             
              for (var i = 0; i < userShoppingCart.length; i++) 
              {
                alert('3');
                var orderline = new sforce.SObject("Orderline__c");
                alert('4');
                orderline.ProductId__c= userShoppingCart[i].ProductId;
                alert('5');
                orderline.ProductName__c= userShoppingCart[i].Name;
                alert('6');
                orderline.Quantity__c= userShoppingCart[i].Quantity;
                alert('7');
                orderline.Price__c= userShoppingCart[i].Price;
                alert('8');
                orderlines.push(orderline);                
              }
              alert(orderlines[1].ProductName__c);
              var result = sforce.connection.create([orderlines]);
              alert('10');
              saveorders();
          

}

 

I had included : 

<script src="../../soap/ajax/28.0/connection.js" type="text/javascript"></script>

Please help me .

Thanks

arosysarosys

One more thing i am caaling this function on click of a button from my vf page.

arosysarosys

I figured it out :

 

I aded these line in my vf page :

 

<apex:commandButton value="Place Order" onClick="addSessionStorage();"/>
<apex:includeScript value="/soap/ajax/28.0/connection.js"/>
<apex:includeScript value="/soap/ajax/28.0/apex.js"/>

 

and this in my javascript &colon;

 

sforce.connection.login("username", "passwordwithtoken");

 

 

and it worked.

This was selected as the best answer