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
MsKnightMsKnight 

Test Method for Outbound Email

Hi,

I'm trying to write a Test Method for a class that sends an email. I'm stuck at 73% - I can cover the addRoofMountsCheck, but can't get coverage on lines 8-18 (all of the code for sending the email). Any suggestions?

Code:
public class RoofMountsIn {
    public static void addRoofMountsCheck(SFDC_Project__c[] pros) {
       for (SFDC_Project__c p:pros) {
            if (p.Roof_Mounts_In__c != True){
                p.Roof_Mounts_In__c = True;
       
           for (User user : [SELECT Email FROM user WHERE UserRoleId = 'userId']){               
                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {user.Email};
                  mail.setToAddresses(toAddresses);

                mail.setSaveAsActivity(false);
                mail.setTargetObjectId('contactId');
                mail.setWhatId(p.Id);
                mail.setTemplateID('emailtemplateId');
               
        // Send the email you have created.
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                       }
                }
           }
    }
}

 
Thanks!

paul-lmipaul-lmi
wrap that logic in a seperate method, and then call that method in your testMethod.  It seems convoluted, but that's what SF is expecting in this scenario.

for instance, I do the same thing you are in all of my classes to debug via email vs. debugging through their interface, and to do so I created a seperate class to handle all outbound single emails.
MsKnightMsKnight
That worked perfectly - thanks!
paul-lmipaul-lmi
glad to hear.  cheers!
SimbaSimba
I would like to open SF Email window for the user through a Custom Scontrol button. I would like to default th eto address to it and after that the user should get the SF Email window and be then able to send e-mail ,change whatever they wish to.

I have lots of SControl email related example but I am not able to make them work , such that it simply opens the email SF window . The code that I have tried, successfully sends the e-mail but is not able to open th eSF Email window.

Can you give me an example to use SendEmail with their parameters through URLFOR ( I am not able to make it work].

Thanks

Regards


paul-lmipaul-lmi
/email/author/emailauthor.jsp?&p2_lkid={!Case.ContactId}&p24={!Case.ContactEmail__c}%3B%20{!Contact.Alternate_Email__c}&p3_lkid={!Case.Id}&retURL=%2F{!Case.Id}&template_id=00X30000000qn4Z

here's an example I use to go straight from a case, select a specific email template, and prepopulate some of the field with the value of a custom field on the case.

this is the code for a custom button.
SimbaSimba
Thanks a lot for you reply.

But does this mean it's not possible to achieve this via URLFor Method (something in the lines of URLFor($Action.Activity.SendEmail) ) ?

Thanks. I really appreciate your respond .