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
Rob LilleyRob Lilley 

custom email button on custom object - To address default to email address on object

Hello, I wonder if anyone could help on this please. Fairly new to the coding on buttons.

I have a custom object 'Match' which is not related to Contacts.  I'm trying to create a custom button that has two functions:
  1. Default the To email address to the value in the Volunteer_Email__c on the Match object
  2. When email is sent update the Volunteer_Email_Sent_Date__c on the Match object to the date sent
The code I currently have is:
location.replace('/email/author/emailauthor.jsp?retURL=/{!Match__c.Id}&p3_lkid={!Match__c.Id}&rtype=003&template_id=00X0Y000000IQvm');

It appears to me with (1) above that I can add parameters to default the 'Additional To' and 'CC' to the value you in Volunteer_Email__c but not the To address. (I think it it is looking for a contact which in this case I do not have).

Many thanks for any advice, Rob 
Prabhat Gangwar007Prabhat Gangwar007

Hi Lilley

I have done your senario so for this you need to follow some steps.

 

 

steps 1 : Create Detail Page Button on Match object.
step 2 : select Behavior of custom button execute javascript .
step 3 : write this code which i mentioned.
User-added image


{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var returnFlag = sforce.apex.execute("testhost.SendEmailUsingWebserive","sendEmail",{matchid:'{!match__c.Id}'}); 
location.reload(true); 



step 4 : save this and set this cutom button at layout

 

step 5 :  create class and put code i mentioned below.

 

 

global class SendEmailUsingWebserive 
{
     webService static void sendEmail(String matchid) 
    { 
           string varconid = matchid;
           List<match__C>listofmatch =[select id , Volunteer_Email__c , Name  from match__C where id =: varconid];
            List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
            if(listofmatch .size()>0){
                for(match__C obj : listofmatch ){
                        if(obj.Volunteer_Email__c  != null){
                        obj.testhost__Volunteer_Email_Sent_Date_c__c=system.today();
                        // Email code //
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        List<String> sendTo = new List<String>();
                        sendTo.add(obj .Email);
                        mail.setToAddresses(sendTo );
                        mail.setSenderDisplayName('Details');
                        List<String> ccTo = new List<String>();
                        mail.setCcAddresses(ccTo);
                        mail.setSubject('Hi ');
                        String body = 'Hi' +obj.Name  +', ';  
                        body += '<br/>';
                        body += '<br/>';
                        body += 'Congratulations on your acceptance has been done<br/>';
                        mail.setHtmlBody(body);
                        mails.add(mail);
                 }
             }
            }
            
            if(mails.size()>0){
                Messaging.sendEmail(mails);
                update listofContact ;
                
            }
      
    }
    
      
}


o/p :  See here is custom button whenever i will click here email will be go and here is one date field which will update as per your requirement.
User-added image

Prabhat Gangwar007Prabhat Gangwar007
please mark best answer if it is full fill your requirement.
Rob LilleyRob Lilley
Hello Prabhat,
Many thanks for your update, much appreciated and will try this tomorrow when back in office, best wishes, Rob 
Prabhat Gangwar007Prabhat Gangwar007

thanks  Rob

If you have any issue let me know .

Rob LilleyRob Lilley
Hello Prabhat,

I have just tried saving the class and I receive an error: 
Error: Compile Error: Variable does not exist: listofContact at line 33 column 24 (which I think is referrring to update listofContact ;?)

Many thanks, Rob 
Prabhat Gangwar007Prabhat Gangwar007
replace varible use listofmatch 
 
Rob LilleyRob Lilley
Hello Prabhat, I tried that thank you but now receive message "Error: Compile Error: unexpected token: '{' at line 1 column 0" - as far as I can tell by googling: 

REQUIRESCRIPT no longer working with Winter 16
As per release winter 16 release notes: http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/salesforce_release_notes.htm
 
REQUIRESCRIPT no longer executes JavaScript on Page Load.

I guess therefore:

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

No longer works and needs something else?

Best wishes, Rob