1

Hi I have built a class and a test and I am getting close to getting the test to work. I am trying to send an email using a template. But I am getting an error on the last line of code.

Save error: Method does not exist or incorrect signature: proteusProposalCon.sendEmailFunction(Id, Id)

Here is my APEX there are 2 functions:

global class proteusProposalCon {
    global ApexPages.StandardController controller;
    global proteusProposalCon(ApexPages.StandardController controller) {
        this.controller = controller;
    }

      global PageReference saveAndCongrat() {
      controller.save(); // This takes care of the details for you.
      PageReference congratsPage = Page.thankyou;
      congratsPage.setRedirect(true);
      return congratsPage;
    }

    Webservice static void sendEmailFunction(){
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAddresses = new String[] {'xxx@xxxxxxxx.com'}; 
       mail.setToAddresses(toAddresses);
       mail.setReplyTo('info@xxxxxx.com');
       mail.setSenderDisplayName('My Name');
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setTemplateId('00X90000001FNCN');
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
     } 
}

And Here is my test:

@isTest
global class proteusProposalConTest {

    public static testMethod void testMyClass(){
        Opportunity a = new Opportunity(Name = 'Test Account', Client_Proposal_Confirmation__c = false, StageName = 'BRI', CloseDate =system.today()+30);
        insert a;

        PageReference pg = Page.thankyou;
        Test.setCurrentPage(pg);

        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
        proteusProposalCon customController = new proteusProposalCon(stdController);

        system.assertEquals(false, a.Client_Proposal_Confirmation__c);

        a.Client_Proposal_Confirmation__c = true;
        PageReference newPage = customController.saveAndCongrat();

        Opportunity results = [Select Id, Client_Proposal_Confirmation__c From Opportunity Where Id =: a.Id];
        system.assertEquals(true, results.Client_Proposal_Confirmation__c);

       System.assertEquals(Page.thankyou.getUrl(),newPage.getUrl());
    }

    //Test Email

   Webservice static void sendEmailFunction(string contactId,string oppId)
    {
       Messaging.SingleEmailMessage mail 
           = new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(contactId);
        mail.setWhatId(oppId);
        mail.setTemplateId('00X90000001FNCN'); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }  

    private static testMethod void myUnitTest() 
    {
        Contact con =  new Contact();
        con.FirstName = 'Anil';
        con.LastName = 'Dutt';
        con.Email = 'anil@swiftsetup.com';
        insert con;

        Opportunity oppNew =  new Opportunity();
        oppNew.Name = 'Test Opp';
        oppNew.StageName = 'Ticketing';
        oppNew.CloseDate = System.now().date();
        insert oppNew;

       ApexPages.StandardController sc = new ApexPages.standardController(oppNew);
       proteusProposalCon sc1 = new proteusProposalCon (sc); // test constructor

        // Not: sc1.SendEmail(); 
        // Because method is a webservice in a global class
      proteusProposalCon.sendEmailFunction(con.id,oppNew.id);
    }
}

Is it something to do with the global class? Thank you. Joe

1

From what I can see you're trying to call your sendEmailFunction method in your controller, which does not accept any parameters. If you want to call your method in your test class that takes contact ID and oppty ID, then instead of proteusProposalCon.sendEmailFunction call proteusProposalConTest.sendEmailFunction.

7
  • Thanks for your help Bachovski, that worked but the code isn't covering the Email code only 44% covered. ( Line 14,15,16,18,20,21,23,24,26,29 not covered ) – proteuscanvas Aug 18 '14 at 3:25
  • 1
    That's true because you never call proteusProposalCon.sendEmailFunction from your test method. So after proteusProposalConTest.sendEmailFunction(con.id,oppNew.id); also call proteusProposalCon.sendEmailFunction();. – Boris Bachovski Aug 18 '14 at 3:28
  • Thanks again, I now have this error System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing targetObjectId with template: [] - I will check what the required fields are and see what is missing. – proteuscanvas Aug 18 '14 at 3:39
  • 1
    You shouldn't be sending emails from a test class :). So send email only when you're not running tests: if (!Test.IsRunningTest()) Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); – Boris Bachovski Aug 18 '14 at 3:44
  • 1
    Since you're using "setTemplateId", you also need to specify "targetObjectId" which is ID of the record that the email template is gonig to be related to. – Boris Bachovski Aug 18 '14 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.