0
  1. I have the email service class "Messaging.InboundEmailHandler"
  2. I create visualforce email template"CreateLead"
  3. I create visulforce component "createLead"

Now I want to call createLead() method from visualforce component bu button. the problem is Inside the createLead() method I called handleInboundEmail() method which give me error " Method does not exist or incorrect signature: void handleInboundEmail() from the type CreateLeadExample" please check code

global class CreateLeadExample implements Messaging.InboundEmailHandler {
    
    public void   createLead(){
        handleInboundEmail();
       
        
    }
 global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                       Messaging.InboundEnvelope env){
    // Create an InboundEmailResult object for returning the result of the  
    // Apex Email Service 
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    String myPlainText= '';

    // Add the email plain text into the local variable  
    myPlainText = email.plainTextBody;

    // Check for existing leads with this email address   
    Lead[] leads = [SELECT Id, Name, Email
      FROM Lead
      WHERE Email = :email.fromAddress];

    if (leads.size() == 0) {
      // New Lead object to be created - set LastName and Company to
      // dummy values for simplicity
      Lead newLead = new Lead(Email = email.fromAddress, 
        LastName = 'From Email', 
        Company = 'From Email');

      // Insert a new lead
      insert newLead;    

      System.debug('New Lead record: ' + newLead );   
    } else {
      System.debug('Incoming email duplicates existing Lead record(s): ' + leads );    
    }

    // Set the result to true. No need to send an email back to the user      
    // with an error message   
    result.success = true;

    // Return the result for the Apex Email Service 
    return result;
  }
}

Visualforce component From where I called

<apex:component  controller="CreateLeadExample" access="global" >
    <apex:commandButton action="{createLead}" />
    
</apex:component>

Actually, I want to add a button inside visualforce email template when the button is clicked its create new lead by using email sevice.I don't know how to call handleInboundEmail from another method

1 Answer 1

1

You'd have to manually construct the parameters in order to call the method:

public void   createLead(){
  Messaging.inboundEmail email = new Messaging.inboundEmail(); 
  Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
  // populate email and env with appropriate data/headers
  // then you can...
  handleInboundEmail(email, env);
}
1
  • Can u please give me an idea about button in visualforce email template which create new lead??
    – user91246
    May 9, 2021 at 13:58

You must log in to answer this question.