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

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:

Leave a Reply