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
SFDC_DeveloperSFDC_Developer 

Update Record on click of javascript button

I want to update one record on click of javascript button, I am able to fetch record but not able to update that one. Below is my code:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var myquery = "SELECT Field__c FROM Object WHERE Name = '1' limit 1"; 

result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

if(records[0]){ 
var myObj = records[0]; 
} 

alert(myObj.Field__c); 
myObj.Field__c = 'Hello'; 
alert(myObj.Field__c); 

var results = sforce.connection.update([myObj]); 
alert(results);

 
pconpcon
You also need to include the Id field in your query.  For example, the following will set the Languages__c field on a contact to English when clicked.
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

var query = "select Id, Languages__c from Contact where Id = '{!Contact.Id}' limit 1"; 
        
var result = sforce.connection.query(query); 
var records = result.getArray("records"); 
        
var myObj = records[0];

myObj.Languages__c  = 'English';
        
var results = sforce.connection.update([myObj]);
SFDC_DeveloperSFDC_Developer
Thanx pcon, It works.

But I want to know why I need to include Id in my query.
pconpcon
You need to include it because unlike the native Apex SOQL the Id does not come across with the query.  So when you call the update the object oes not have an Id so it does not know what instance to update unless you include the Id.
Juan CosceriJuan Cosceri
Hi pcon.
I am trying tu use this code but unfortunatelly, I cant Update!
As i could checked the trouble is into Result CODE
alert(nestedDiv.textContent);
                   
alert(nestedDiv.textContent);
                   
                        var myquery = "select Id, name, Purchase_Order__c from Rew_Order__c where Id = " + "'" + nestedDiv.textContent + "'" + " limit 1"; 
                        
                        alert(myquery);
                              
                            var result = sforce.connection.query(myquery); 
                            var records = result.getArray("records"); 
                            
                               alert(result);     
                               
                            var myObj = records[0];
                            myObj.Purchase_Order__c = 'a2m2C000000HdDs';
                                 
                                 alert(myObj);
                                    
                            var results = sforce.connection.update([myObj]);

How can i Invoque {!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

Regards
SFDC_DeveloperSFDC_Developer
what is this "alert(nestedDiv.textContent);" in your code.
for Invoking Requirescript just use it before the java script.
Juan CosceriJuan Cosceri
nestedDiv.textContent es el ID de Rew_Order__c.
Me lo traigo de una tabla y le puse un Alert para chequarlo y eso esta OK.
 
Juan CosceriJuan Cosceri
como me tiro error con el Required Script le agregue en la Apex Page:

<apex:includeScript value="/soap/ajax/30.0/connection.js"/>
<apex:includeScript value="/soap/ajax/30.0/apex.js"/>

es esa la forma de invocar el ajax? o el error puede ser eso mismo??
Juan CosceriJuan Cosceri
Hi!
nestedDiv.textContent is a variant value looked up by Id into another table with Orders Id to be updated.

I think so that the problem will be including Ajax into apex code

I used the following lines:
<apex:includeScript value="/soap/ajax/30.0/connection.js"/>
<apex:includeScript value="/soap/ajax/30.0/apex.js"/>
Is it ok?