Monday 26 May 2014

Updating Records Through Custom Button(Javascript and Ajax api)

How to update some field value by using custom button ?
Lets assume we need to update Rating field value of Account Object,for this we will create a custom button on Account and will add that button in page layout. 

Here is an image of creating custom button.
Add caption

I am also sharing button code for updating field through ajax. 

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

var url = parent.location.href;
var connection = sforce.connection;
var accountRating = '{!Account.Rating}';
var accountId = '{!Account.Id}';


if(accountRating == 'Cold') {
   var updateAccountRecords = [];
   var update_Account = new sforce.SObject("Account")
   update_Account.Id = accountId;
   update_Account.Rating= 'HOT';
   updateAccountRecords.push(update_Account );
   sforce.connection.update(updateAccountRecords);
   alert('Account Rating is converted to HOT');
   window.location.reload();
} else if(accountRating == 'HOT'){
   alert('Already HOT');
}
else {
  alert('Only Cold Account rating can be converted to HOT');
  
}


No comments: