+ Start a Discussion

Calling Apex method from Detail Button



I'm trying a fairly simple call of an Apex method from a custom detail button in Account. The argument I need to send is the Account object itself. I don't know how to reference it. I tried the example from the Apex development guide, which is:


var acct = sforce.sObject("Account"); 
var id = sforce.apex.execute("myClass","makeContact",


Even if I comment out the call to apex, the line

var acct = sforce.sObject ("Account");

Gives me an error:  Object doesn't support this property or method.


I also tried through a global variable

var acct = $ObjectType.Account;

But that doesn't work either.


What am I missing? How do I reference the Account object in a button?


Thanks very much.



I am afraid you cannot get the object in JS directly:(

You have to pass the ID to your apex class and query for getting the other fields based on that ID within the apex class!!



I have similar problem, I don't know how to pass a value from a detail page to apex class. How can I pass the Id?


I have the following code: 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <script type="text/javascript" src="/js/functions.js"></script>
    <script src="/soap/ajax/17.0/connection.js" type="text/javascript"></script>
    <script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
    <script type="text/javascript">

        var landmark = sforce.sObject("Landmark__c");
        var id = sforce.apex.execute("ShiftLandmark","test",{land: landmark});       
        alert('id ' +id);





and the class is simply:

global class ShiftLandmark {

  webService static Id test(Landmark__c land) {
        return land.id;


doesn't work :(

cannot get the id back...


you can pass the ID into your apex class using the object merge field in your JS code like "{!object__c.field__c}"





I have similar problems.




var acct = new sforce.SObject ("Account");  // for: var acct = sforce.sObject("Account");


but I have still problems with:




Is this still possible/valid syntax?

I don't find any documentation in the AJAX Toolkit.



Message Edited by creemej on 08-12-2009 12:06 PM

As, i posted earlier, you have to pass the ID/Field of the object to your apex class method from JS and then you have to query for getting the object based on this field/id!!




This gives me a new object in javascript&colon;

var acct = new sforce.SObject ("Account");


That was the first problem (Object doesn't support this property or method).

But I have no answer for the sforce.apex.execute

Message Edited by creemej on 08-12-2009 04:34 PM
Message Edited by creemej on 08-12-2009 04:37 PM

Yes, i agree you can create a new object like that, but its just a fresh instance!

You cannot have the field values associated with the record.BTW ... y do u wanna send the object itself to your class method?


Imran MohammedImran Mohammed

Are you still facing this issue.

Did you get the solution for this?