0

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

Thanks

2
  • what is the error you are seeing ?
    – Himanshu
    Nov 26, 2015 at 19:52
  • Id value of incorrect type,Invalid_id_field Nov 26, 2015 at 19:58

1 Answer 1

2

You missed the Id in your query string which is causing this issue.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var myquery = "SELECT Id,Name FROM Account WHERE Name = 'aaa' limit 1"; 

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


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

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

alert(myObj.Name); 

var results = sforce.connection.update([myObj]); 
alert(results);
3
  • In JavaScript scope is bounded by functions not {} blocks so your change/explanation will not help.
    – Keith C
    Nov 27, 2015 at 0:23
  • @KeithC I understand your concern that is why i executed this code in my org prior to posting as answer. If I declare var myObj. Inside if block sfdc throws an error as question defined but if you declare var myObj outside if as shown in answer it works. Your thoughts?
    – Himanshu
    Nov 27, 2015 at 1:54
  • @KeithC yes you were correct it was not scope issue. actually I added Id in query which resolved the issue :). point taken, I have updated the anser after testing this.
    – Himanshu
    Nov 27, 2015 at 2:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .