How to call Apex method from a Custom Button in Salesforce?

Apex Code:

global class MemberNameCheck
{
    webservice static void check(ID memId)
    {
        Member__c mem = [SELECT Name from Member__c WHERE Id =: memId];
        if(mem.Name == 'Test')
        {
            mem.Name = 'Test Updated';
            update mem;
        }
    }
}


Custom button:

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

var r = confirm("Are you sure want to check the name?");
if(r == true)
{
    sforce.apex.execute("MemberNameCheck","check",{memId:"{!Member__c.Id}"});
    location.reload(true);
}




Output:




4 comments:

  1. Hello Magulan,As per this step here i got ErrorMessage after alert message....
    A problem with the OnClick JavaScript for this button or link was encountered:

    {faultcode:'soapenv:Client', faultstring:'No operation available for request {http://soap.sforce.com/schemas/package/MemberNameCheck}check, please check the WSDL for the service.', }

    ReplyDelete
  2. Did you solve this issue? if not, Please Change your Controller version 29 to 15

    ReplyDelete
  3. Hello All,

    I am also got this error.Please suggest me.

    ReplyDelete
  4. Hello Friend,

    Do you think is possible for you to show me a simple way to post something using AJAX?
    I mean, it could be a textfield. How could you execute the method of the class and then post the response on the textfield?
    The idea is to not load all page, could you help me friend?

    Thank you very much,
    Greetings.

    ReplyDelete