You need to sign in to do that
Don't have an account?
Messaging.MassEmailMessage & setTargetObjectId SetWhatId
Hi I am writing an APEX Trigger to send MassEmail, I learned I could use SetTargetObjectId for MassEmailMessage, from this link:
http://www.forcetree.com/2009/07/sending-email-from-your-apex-class.html
I want to use setTargetObjectId, this method works very well in SingleEmailSeassage.
When I use it for MassEmailMessage,
It keeps showing the error
Method does not exist or incorrect signature
I thought I may give the wrong value for this method, I tried the List, Set<Id>, etc.
None of them works.
Does MassEmailMeassage supports this method, or it only supports it in APEX Class, not trigger?
the error came for SetWhatId, too.
Update:
Thanks Puja,
I forget to add things I did try the "setTargetObjectIds" and "setTargetObjectId", but both of them shows same error for me.
Hi,
yes you can send mail through trigger with MassEmailMessage .
Please use this code
public List<Id> contactIds = new List<Id>();
contactIds.add(Trigger.New[0].Id);
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(contactIds);
mail.setTemplateID('00X90000001EWno');
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
And if still get the error please send me your code snippet then i will check.
All Answers
Hi,
For massEmailMessage you use the "setTargetObjectIds" not "setTargetObjectId" .
Please take reference from the below link:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm
Hi Puja,
Thank you, I did try setTargetObjectIds, but it still shows the same error.
Does it support in a trigger?
Hi,
yes you can send mail through trigger with MassEmailMessage .
Please use this code
public List<Id> contactIds = new List<Id>();
contactIds.add(Trigger.New[0].Id);
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(contactIds);
mail.setTemplateID('00X90000001EWno');
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
And if still get the error please send me your code snippet then i will check.
Yes, I found that, too the various must be ID.
It should be List<ID> or ID[], I shouldn't use Set<ID>.
Hi Puja,
Thank you so much. But I am facing another error in my Unit Test.
It shows the error
The TargetId is the Contact Owner, it's User ID, right?
I have the code
But every time I run the test, it shows me the error.
Did you face this situation before.
Hi,
Yes this is the user Id.ok you take the reference from Here.
Trigger:
public List<Id> contactIds = new List<Id>();
for(Contact con : trigger.New ) {
contactIds.add( con.OwnerId);
}
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(contactIds);
mail.setTemplateID('00X90000001EWno');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
TestClass
Contact con = new Contact(
LastName = 'puja tt'
);
insert con;
You need to just create a new record of contact.
If still you gives an error please lete me know
And if this is helpful to you please give me the feedback with click on star at left side.
Hi Puja,
I feel there is better way to solve the error except post my code, please check below.
I worried that I shouldn't use add the Contact Owner ID directly.
Thank you so much.
Thank you so much for help.
Hi,
Ok I got the solution .
You need to check the
If( targetObjectIds != null && targetObjectIds.size() > 0 ) {
}
Because of this check if "targetObjectIds" list have no records it will never go to the if block.
You only check the null but if you initialize a list then it never equals to null,that's why it is go to the if bloack and give the exception
Hi Puja,
Thank you so much, but I have to give up MassEmailMessage.
Something share with you.
It happens with my mistakes.
1. when I test it in Sandbox, it gives me the error thatThen I checked the Setup sandbox doesn't have the permission to send mass email, thtat's quite funny. →_→!2. Then I try to deploy it into the production.
Then I got another error that
My WhatIds is Contact.
It seems that I have to use SingleMailMessage.
Whatever thanks for your help.
It's very hard to take notice that we should limite the size, I just feel Salesforce is not smart at this point.
Here is my new code,
I found it also works very well. sometime we may not need to use MassEmailMessage.
Hi,
Never write query inside for loop. it hits the governer limit of salesforce.If the "TargetObjectIds" length is more that 100 ,it gives error "too many sql query 101."
And there is a limit to call "SendEmail" method.
Then you create a list of "Messaging.SingleEmailMessage" and add the indiviual email to the created list
as:
String emailTemplateId = [SELECT id FROM EmailTemplate WHERE Name = 'updateClassOwner'].id;
List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
for(Integer i=0; i<targetObjectIds.size();i++ )
{
If(targetObjectIds[i]!=null)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(targetObjectIds[i]);
mail.setTemplateId(emailTemplateId);
mail.setWhatId(whatIds[i]);
mail.setsaveAsActivity(false);
mailList.add(mail);
}
}
Messaging.sendEmail(mailList);
Thank you so much.
It's my first time to know that SendEmail is one kind of Query.
I am using mass email message concept to send an email to record owner. In the code, I have queried the corresponding email template. Based on my conditions email is received. But I am not getting the merge fields that are used in the email template. Can anyone help me how to resolve this issue?