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
eric_wceric_wc 

send 2 different emails when clicking send, have one working

I managed to get my code to send out a mass email awhile back but now would like to enhance it to send another email with a different template at the same time.  (user clicks send both emails go out).  below is my code but it will not compile.  I get the error message:

"Save error: Method does not exist or incorrect signature: [Messaging.MassEmailMessage].setWhatIds(Id)"

 

Thanks for any help, maybe I am going about this the wrong way, I am not a developer...

 

Eric

 

public class MNEmail2 {
public String Template = [select MN_Email_Template__c from Case where id = :ApexPages.currentPage().getParameters().get('id') limit 1].MN_Email_Template__c;
public list <Contact> Test;
Contact[] contactList = new Contact[]{};
Id[] targetObjectIds = new Id[] {};
Id[] whatids = new Id[] {}; 
public id mn = [select ID from Case where id = :ApexPages.currentPage().getParameters().get('id')].id; 
public String custaff = [select MN_Customers_Affected__c from Case where id = :ApexPages.currentPage().getParameters().get('id') limit 1].MN_Customers_Affected__c;
String[] actnames = new List<String>();

public MNEmail2() {
	actnames = custaff.split(';');
	//System.debug ('***** List: ' + actnames + ', size: ' + actnames.size ());
         //Loop thrugh the whole ist of contacts an their emails
          for (Contact c : [Select account.name, name, email from Contact where Contact.Account.name = :actnames and Receive_support_notifications__c = True]) {
            targetObjectIds.add(c.Id);
            contactlist.add(c);
            whatIds.add(mn);
           }
}

//public Account getAccount() {
//return account;
//}
public string getTemplate() {
    return Template;
}
public id getmn() {
    return mn;
} 
 public list<Contact> getcontacts(){ 
        return ContactList;  
}

//cancel button action
public PageReference cancel() {
    PageReference pageRef = new PageReference('/'+ mn);
    pageRef.setRedirect(true);
    return pageRef;
}

//send button action
public PageReference send() {
// Define the email
Messaging.MassEmailMessage email1 = new Messaging.MassEmailMessage();
// Sets the paramaters of the email
	email1.setTargetObjectIds (targetObjectIds);
	email1.setWhatIds (whatids);
	email1.setTemplateId ([select id from EmailTemplate where Name = :template].id);
	email1.setreplyto ('noreply@demandtec.com');
	email1.setSenderDisplayName ('DemandTec Support No-Reply');
	//email.setSubject( subject );
	//email.setToAddresses( toAddresses );
	//email.setPlainTextBody( body );
// Sends the email 1
Messaging.SendEmailResult [] r1 =
Messaging.sendEmail(new Messaging.MassEmailMessage[] {email1});

// Define the second email
Messaging.massEmailMessage emaili = new Messaging.massEmailMessage();
// Sets the paramaters of the email
	emaili.setTargetObjectIds ([select id from Contact where email = 'support@gmail.com'].id);
	emaili.setWhatIds (mn);
	emaili.setTemplateId ([select id from EmailTemplate where Name = 'Internal Maintenance Notification'].id);
	emaili.setreplyto ('noreply@demandtec.com');
	emaili.setSenderDisplayName ('DemandTec Support No-Reply');
// Sends the email 2
Messaging.SendEmailResult [] ri =
Messaging.sendEmail(new Messaging.massEmailMessage[] {emaili});

//add case comment indicating email was sent.
CaseComment cmt1 = new CaseComment(
            ParentId = (mn),
            IsPublished = false,
            CommentBody = (custaff +'Were sent'+ Template +'email on '+ system.today()));
      Insert cmt1; 
//return to case page
PageReference pageRef = new PageReference('/'+ mn);
    pageRef.setRedirect(true);
    return pageRef;
return null;
}
static testMethod void testMNEmail2(){
   
        //make test data
        Account a = new Account(Name = 'Testing');
        Insert a;
        Contact con = new Contact(Firstname = 'testing',lastname = 'test',email = 'support@gmail.com',Receive_support_notifications__c = True,Account = a);
        Insert con;
        Case c = new Case(Subject = 'Test case',MN_Email_Template__c = 'Scheduled Maintenance',MN_Customers_Affected__c = 'Testing');
        Insert c;
       
        CaseComment cmt1 = new CaseComment(
            ParentId = c.id,
            IsPublished = false,
            CommentBody = 'A private comment from a user.'
        );
        CaseComment cmt2 = new CaseComment(
            ParentId = c.id,
            IsPublished = true,
            CommentBody = 'A public comment from a client.'
        );
        Insert cmt1;
        Insert cmt2;
       
         // Initiate class createOpsTicketClass with reference to created Support Case
             System.currentPageReference().getParameters().put('id', c.id);

             MNemail2 ee = new MNemail2();
             ee.targetObjectIds.add(con.Id);
             ee.whatIds.add(c.id);
             String nextPage = ee.send().getUrl();
             nextPage = ee.cancel().getUrl();
             System.assert(ee.mn == c.id);
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

It must be a Id[], not an Id. Thus, you can't use "mn" as a valid parameter.

 

You can either: 1) make the variable a list (just add the [] on the line where you first defined mn: Id[] mn = [select ...), or you can 2) construct the list dynamically on the line:

 

emaili.setWhatIds (new List<Id> { mn } );

In either case, that should just about fix the problem.

All Answers

sfdcfoxsfdcfox

It must be a Id[], not an Id. Thus, you can't use "mn" as a valid parameter.

 

You can either: 1) make the variable a list (just add the [] on the line where you first defined mn: Id[] mn = [select ...), or you can 2) construct the list dynamically on the line:

 

emaili.setWhatIds (new List<Id> { mn } );

In either case, that should just about fix the problem.

This was selected as the best answer
eric_wceric_wc

Knew I missed somthing simple.  THANK YOU!

 

Eric