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
AdamSBealAdamSBeal 

Mass Email Message Setting WhatIds

I am creating a MassEmailMessage and attempting to send a message to a list of contactIds. This code sends the emails as long as I don't set a whatId but the template isn't filled with data from my deviceID. When I set the whatIDs I get the "Attempt to de-reference a null object". I do call this after I have done the DML insert(devicerequest) and in debugging I can see a deviceRequest.Id is there when I call mail.WhatIds.add(deviceRequestId). Here is the code any help is greatly appreciated:

 

insert(deviceRequest);
EmailTemplate emailTemplate = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'Device_Request'];
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setSenderDisplayName(senderName); 
mail.setReplyTo(senderEmail);	
						
mail.setTargetObjectIds(contactIds); 
mail.WhatIds.add(deviceRequest.Id);
		
mail.setTemplateId(emailTemplate.id); 
			
mail.saveAsActivity = false;
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });

 

Best Answer chosen by Admin (Salesforce Developers) 
AdamSBealAdamSBeal

No device request is a custom object so I guess I can't use a template merge field from apex with a custom object even though it works with email alerts. I guess I could just re-construct the email from a string myself. You would think SF would allow this.

All Answers

thedabblerthedabbler

May be you already checked this but according to this article 

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

 

Isn't setWhatIds the method that you should be using.I haven't done this before.Also is that deviceRequest one of the Contract , Case ,Opportunity or Product type object? It has to be one of those according to the docs for it to work.

Also you have to specify one whatId for each TargetObjectId.

 


AdamSBealAdamSBeal

No device request is a custom object so I guess I can't use a template merge field from apex with a custom object even though it works with email alerts. I guess I could just re-construct the email from a string myself. You would think SF would allow this.

This was selected as the best answer
SF DEVSF DEV

Hi,

 

Im in the same boat, able to send the template but could not merge or show the values in email template.

 

Have you got the solution.