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
Nidhi Sharma 17Nidhi Sharma 17 

Passing fields of Contact object as parameters into an Apex Class via Custom Button in a Task

I have an Apex Class to send an Email to someone as a Contact.
I want to send the 'Email' and the 'FirstName' fields of a particular Contact associated to a Task as parameters to the Apex Class when I click the Custom Button - 'Send' on the task detail page, so that I can send the email from the Apex Class using these field values.

My Apex Class : 
 
global class TestMailClass
{
    WebService static Integer TestMailMethod(String emailAddress, String fname1)
    {
          // Create a new Email
          
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 
      String[] toAddresses = new String[] {emailAddress};
      mail.setToAddresses(toAddresses);
    
      // Set who the email is sent from
      mail.setReplyTo('myemail@abc.com');
      mail.setSenderDisplayName('ABC');
    
      // Set email contents - used variables!
      mail.setSubject('TEST EMAIL');
      String body = 'Dear ' + fname1 + ', ';
      body += 'This is a sample mail that I am trying for the first time.';
      body += 'I write to send a random email ';
      mail.setHtmlBody(body);
    
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    
    return 0;

    }

}

My Custom Buttom in Task :
 
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}

var cntct = new sforce.SObject("Contact");

cntct.Id = "{!Contact.Id}";

var eid = new String(cntct.Email);
var fname = new String(cntct.FirstName);

var result = sforce.apex.execute("TestMailClass", "TestMailMethod",{emailAddress:eid, fname1:fname});

The problem here is the variables 'eid' and 'fname' remains undefined and is not passing into the Apex Class.
Need some help on this. Hope I made the question clear.
Best Answer chosen by Nidhi Sharma 17
Bhanu MaheshBhanu Mahesh
Hi Nidhi,

try below java script code
 
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}

var cntct = new sforce.SObject("Contact");

cntct.Id = '{!Contact.Id}';

var eid = '{!Contact.Email}';
var fname = '{!Contact.FirstName}';

var result = sforce.apex.execute("TestMailClass", "TestMailMethod",{emailAddress:eid, fname1:fname});

Regards,
Bhanu Mahesh

All Answers

Bhanu MaheshBhanu Mahesh
Hi Nidhi,

try below java script code
 
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}

var cntct = new sforce.SObject("Contact");

cntct.Id = '{!Contact.Id}';

var eid = '{!Contact.Email}';
var fname = '{!Contact.FirstName}';

var result = sforce.apex.execute("TestMailClass", "TestMailMethod",{emailAddress:eid, fname1:fname});

Regards,
Bhanu Mahesh
This was selected as the best answer
Nidhi Sharma 17Nidhi Sharma 17
Thanks a lot.... :) Seems I never needed the cntct variable...