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
Ankit Chauhan 10Ankit Chauhan 10 

How to pass the sObject to Apex webService method using custom button javaScript 'onClick' section

Hi All,

I'm a novice to salesforce and facing some technical hiccups while implementing the custom button javascript onClick section. My problem statement is-

1. I have created a custom button for lead SO.
2. I will add this custom button on the Lead page layout (New).
3. When user creates a new Lead and click on this custom button, I would like to validate the email address what user has entered on the lead page.
4. I need to know in Javascript as to how to pass the sObject to the Apex- Webservice method, so that I can retrieve the email address. Also post verification I can update the same on the lead page. Please mind that the new record is still not saved.
5. Post validation user can click on SAVE button on Lead page and save the record.

Please note that this is a non-override approach.

Any help please?
you can simply pass the Id to webservice and in apex side just query the object and check if the email is empty or not
Ankit Chauhan 10Ankit Chauhan 10
For example:

var myvar = sforce.apex.execute("InteractiveEmailAddressVal", "validateEmailAddressRT", {i_sObj: {!lead}});

Apex-Webservice method
global class 
 webService static String validateEmaiAddressRT(SObject i_sObj)
        Lead lead = i_sObj;
        String emailValiadted = validateEmail(lead.Email);
        //update the record
        lead.Email = emailValiadted;
        return '';
Ankit Chauhan 10Ankit Chauhan 10
Will I get the ID even for the new record which is not saved yet?
looks like you just want to validate the email while saving the lead record. You can add this validation in trigger as well. why you need a Webservice? 
Ankit Chauhan 10Ankit Chauhan 10
I need this implementation so as to call my functionality without overriding the Lead standard page. The only way I understand is to put a custom button and add certain logic on it. Which in this case can be taken care by Custom button (on click- Javascript) which pass the records to Apex Webservice method.

Is there any other better way to achieve this?