+ Start a Discussion
Glenn DalyGlenn Daly 

Bulification of SingleEmailMessage

I'm trying to bulkify my code, however I'm receiving the following error;

"Incompatible element type Messaging.SingleEmailMessage for collection of Opportunity".

What do I need to do to amend this so it works?   

    List<Opportunity>emailOpOwner = new list<Opportunity>();
            for(Opportunity opp : listOpportunity)
            {
               Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                    mail.setTargetObjectId(opp.OwnerId);
                    mail.setReplyTo('glenn.daly@live.com');
                    mail.setSenderDisplayName('Salesforce Support');
                for(opportunitylineitem oppLineItem :opp.opportunitylineitems)                
                    mail.setSubject('Historic Service attached to current opportunity : ' + oppLineItem.Product2.Name);
                    String body = 'Hi '+ opp.Owner.FirstName + ', ';     
                       body += '<br><br>Salesforce recognises you as the owner of the following opportunity:'+ opp.Name;
                  for(opportunitylineitem oppLineItem :opp.opportunitylineitems)
                      body += '<br><br>Attached to this opportunity is a former R&D service or project that is no longer available:' + oppLineItem.Product2.Name;
                       body += '<br><br> Please use the link below to view the opportunity record:';
                       body += '<br><br>test.salesforce.com/'+ opp.id;     
                     mail.setHtmlBody(body);
                    mail.setSaveAsActivity(false);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    emailOpOwner.add(mail);
                        update listOpportunity;
SalesFORCE_enFORCErSalesFORCE_enFORCEr
It is because of this line of code:
emailOpOwner.add(mail);
you are trying to add mail to a list of opportunity