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
prats23prats23 

Outbound Email functionality

Hi,

 

I am trying to create a Email Functionality on click a custom button. i.e. I have a button that runs javascript on click and it executes the Apex class.

 

Button is:

-------------------

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

var result=sforce.apex.execute( "EmailMe", "EmailFunction", {});
alert(result);

-------------------

 

Apex Class is:

--------------------

public with sharing class EmailMe {
    
    private String[] toAddresses;
    
    public EmailMe(){
        List<User> currentUser = [Select Email from User where Id =:UserInfo.getUserId()];
        String[] toAddresses = new String[] {currentUser[0].Email};
    }
    
    public String EmailFunction(){
        
             Messaging.reserveSingleEmailCapacity(4);
        
             Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
        mail.setToAddresses(toAddresses);
        mail.setSubject('Test Mail');
        mail.setPlainTextBody('Your Case has been created for testing');
        
        Messaging.SendEmailResult[] res = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        if(res[0].isSuccess()){
            return 'Email is sent';    
        }
        else{
            return res[0].getErrors()[0].getMessage();
        }            
    }
}

--------------------

 

But i am recieving something like this error:

 

"{http:// soap.sforce.com/schemas/package/EmailMe}Emailfunction please check the WSDL for the service.."

 

Do you think I have to setup some connection or something??

I am using my DEV org.

 

Help!!

Best Answer chosen by Admin (Salesforce Developers) 
HaagenHaagen

Hi Prats,

 

your apex class/method must be setup as a web service. Have a look at this URL:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm

 

I hope it helps!

 

Cheers!

 

Martin

All Answers

HaagenHaagen

Hi Prats,

 

your apex class/method must be setup as a web service. Have a look at this URL:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm

 

I hope it helps!

 

Cheers!

 

Martin

This was selected as the best answer
prats23prats23

Hey thanks!

 

But now, even though i am receiving alert as "Email is sent".. I am not receiving any actual mail in mailbox!!

 

Do you reckon why??

HaagenHaagen

Hi,

 

I'm not sure. But I would add some System.debug() statements to the class to track what's going on. 

 

Cheers!

 

Martin

prats23prats23

There is not much that i could do with System.debug()

 

Hence I created a "debug log" for myself.. and this is the result after i clicked the button and got "Email is sent" message.

 

        Request Type   Application      Operation         Status           Duration (ms)                    Log Size (bytes)            Start Time
               Api                  Unknown      APEXSOAP     Success                 159                                    6,691                  04/21 14:53:07

 

 

Still no email in mailbox :( .. I guess there is no problem from SF side to send email.. Pls suggest!!

prats23prats23

Hey, I recieved the Email. Though not on my office id, but my personal mail, the problem must be because of Firewalls etc.!! YippY!!

Rasmus MenckeRasmus Mencke

You might want to check the Email Administration settings and see if you have "email security compliance" turned on, otherwise your emails might appear spoof'd and be blocked by your corp email server.