1

How to update a single record in VF page using REST API on click of a button?

The requirement is to update an account status on click of a button. I dont want to use apex controller . Want to do it using javascript so thought of rest api.

| improve this question | | | | |
  • can you please post some code here so that its clear why we need to update through REST API? – Mohith Shrivastava Oct 7 '13 at 10:49
  • Can you say why you want to use the REST API? – greenstork Oct 7 '13 at 13:37
  • JavaScript remoting (@RemoteAction method in the controller) would be the appropriate way to go, I think: salesforce.com/us/developer/docs/pages/Content/… – metadaddy Oct 7 '13 at 16:36
  • @metadaddy So can JS Remoting controllers be called from Custom Button JavaScript? – Andrew Fawcett Nov 7 '13 at 0:47
  • @AndrewFawcett - yes - you can put the RemoteAction method in a controller extension. – metadaddy Nov 7 '13 at 3:41
1
    <script src="/soap/ajax/29.0/connection.js"></script>
 <script>
 var contactId = "{!Contact_ID}";
    function initPage() {
       try{
           var contact = sforce.connection.retrieve("Phone",[contactId])[0];
           contact.Phone = "12398238";
           result = sforce.connection.update([account]);
      if (result[0].getBoolean("success")) {
         log("account with id " + result[0].id + " updated");
     } else {
      log("failed to update account " + result[0]);
     }

    }

  }

</script>

Please follow the above for how to approach using purely java script.Note with ajaz toolkit you don't need REST API at all .

I don't recommend using JavaScript or ajax toolkit for this requirement or using any REST API .A simple visualforce page with an extension controller would do the needful

| improve this answer | | | | |

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.