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
kjtsengkjtseng 

Javascript code in Custom Button

Hi,

I have two custom buttons: one in the list view and one in the detail page for a custom object. The onClick Javascript code for the list button was able to get all the record Ids. But the code for the detail page button was not able to get any id at all. Both buttons call the same Apex class. I followed the code sample from http://developer.force.com/cookbook/recipe/creating-a-button-with-apex.
onClick Javascript Code for the Custom List Button on the Listview:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}

var recordIdArray = {!GETRECORDIDS($ObjectType.Term_Sheet__c)};
console.log('the record Ids are' + recordIdArray);

if (recordIdArray.length) {
    alert("Calculating...");
    var retStr = sforce.apex.execute("LScurrMonthInterest",     "calculateInterestToDate",{ts_idList: recordIdArray});
    alert(result[0] + " Done!"); //response

} else if (recordIdArray.length == 0) {
    alert("Please select the Term Sheet to which" +
         " you would like to calculate current month interest as of today.");
}

~~~~~~~~~~~~
onClick Javascript Code for the Custom Detail Page Button on the Detail Page:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}

var recordIdArray = {{!Term_Sheet__c.Id}};
console.log('the record Ids are' + recordIdArray);

if (recordIdArray.length) {
    alert("Calculating...");
    var retStr = sforce.apex.execute("LScurrMonthInterest",     "calculateInterestToDate",{ts_idList: recordIdArray});
    alert(result[0] + " Done!"); //response

} else if (recordIdArray.length == 0) {
    alert("Please select the Term Sheet to which" +
         " you would like to calculate current month interest as of today.");
}

~~~~~~~~~~~~~
Apex Class:
global class LScurrMonthInterest{

  WebService static void calculateInterestToDate(List<Id> ts_idList) {
   .......
  }
}


Best Answer chosen by kjtseng
Pavan Kumar KajaPavan Kumar Kaja
Change the line

var recordIdArray = {{!Term_Sheet__c.Id}};

to 

var recordIdArray = ["{!Opportunity.Id}"];

And finaly it looks like below. pls let me know is it working or not.

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

var recordIdArray = ["{!Opportunity.Id}"];
console.log('the record Ids are' + recordIdArray);

if (recordIdArray.length) {
alert("Calculating...");
var retStr = sforce.apex.execute("LScurrMonthInterest", "calculateInterestToDate",{ts_idList: recordIdArray});
alert(result[0] + " Done!"); //response

} else if (recordIdArray.length == 0) {
alert("Please select the Term Sheet to which" +
" you would like to calculate current month interest as of today.");
}


All Answers

Pavan Kumar KajaPavan Kumar Kaja
Change the line

var recordIdArray = {{!Term_Sheet__c.Id}};

to 

var recordIdArray = ["{!Opportunity.Id}"];

And finaly it looks like below. pls let me know is it working or not.

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

var recordIdArray = ["{!Opportunity.Id}"];
console.log('the record Ids are' + recordIdArray);

if (recordIdArray.length) {
alert("Calculating...");
var retStr = sforce.apex.execute("LScurrMonthInterest", "calculateInterestToDate",{ts_idList: recordIdArray});
alert(result[0] + " Done!"); //response

} else if (recordIdArray.length == 0) {
alert("Please select the Term Sheet to which" +
" you would like to calculate current month interest as of today.");
}


This was selected as the best answer
kjtsengkjtseng
Yes, it works!!!! Thanks so much!