0

I figured out the code, but I get two errors. I googled one and apperently I need permission from Salesforce, but the second one I dont get it. I am still learning about this stuff . I also wanna know how do you chose the Sender of the email. The receiver will be coming from Contacts, but Sender I want to be from Opportunity Owner, could it be?

Class

 public with sharing class TestEmail {
    public void TestEmail() {
    }

    public void SendEmail() {

            List<Opportunity> oppList = [SELECT StageName, Ownerid,accountId
                                         FROM Opportunity WHERE StageName = 'Something' OR StageName = 'SomethingElse' OR  StageName = 'Else'];

Set<Id> accountIds = new Set<Id>(); //set to keep the id of all the accounts for our opps
for(Opportunity op:oppList)
{
    accountIds.add(op.accountId);
}

List<Id> contactIds = new List<Id>(); //set to keep the contact ids for those accounts

List<Contact> conList = [Select Id,Email From Contact Where AccountId IN :accountIds];
for(Contact c:conList)
{
    if(c.Email !=null && c.Email !='')  //if this contact has email
    {
        contactIds.add(c.Id);
    }
}


        if(contactIds.size() >0){ 

            Messaging.MassEmailMessage emailToSend = new Messaging.MassEmailMessage();
                emailToSend.setTargetObjectIds(contactIds);
                emailToSend.setSubject('Test Subject');
                emailToSend.setTemplateID('00Xq0000000M7ra');
                Messaging.SendEmail(New Messaging.MassEmailMessage[]{emailToSend});
        }

}

Visual Force Page

<apex:page showHeader="true" sidebar="true" controller="TestEmail" >
    <apex:form>
        <apex:commandButton value="Send Email!" action="{!SendEmail}"/>
    </apex:form>
</apex:page>

The Errors

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_FIELD, Subject is not allowed for a mass email.: [] Error is in expression '{!SendEmail}' in component in page TestEmail: Class.TestEmail.SendEmail: line 40, column 1

Class.TestEmail.SendEmail: line 40, column 1

1 Answer 1

1

setSubject() method is not available for mass emails. Please check below link for supported methods in mass email,

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm#apex_Messaging_MassEmailMessage_constructors

1
  • You are absolutely right. Is there a way to make the Sender of the email , Dynamic, So each opportunity owner will have their email showing up? So right now the system uses My email to send out this, but I want to be able to push that send button but the sender be the opportunity owner' email rather than my email
    – Metawaa
    Apr 3, 2015 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .