+ Start a Discussion
AlibabaAlibaba 

Calling Apex method from Detail Button

Hi,

 

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:

 

{!requireScript("/soap/ajax/15.0/connection.js")}
{!requireScript("/soap/ajax/15.0/apex.js")}
var acct = sforce.sObject("Account"); 
var id = sforce.apex.execute("myClass","makeContact",
                             {lastName:"Smith",
                              a:account});

 

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.

Cool_DevloperCool_Devloper

Ali,

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!!

Cool_D

pshemekpshemek

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">
<html>
<head>
    <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);
        window.parent.location.replace("/a0S/o");


       
    </script>
</head>

<body>
</body>
</html>

 

 

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...

Cool_DevloperCool_Devloper

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

Cool_D

creemejcreemej

Hi,

 

I have similar problems.

 

Use:

 

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

 

but I have still problems with:

 

sforce.apex.execute(...

 

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
Cool_DevloperCool_Devloper

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!!

Cool_D 

creemejcreemej

Cool_D,

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
Cool_DevloperCool_Devloper

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?

Cool_D 

Imran MohammedImran Mohammed

Are you still facing this issue.

Did you get the solution for this?