3

I'm trying to bulkify my code, and send all emails out as part of a list however I'm receiving the following error;

Method does not exist or incorrect signature: [List].setTargetObjectId(Id)

What do I need to amend so it works?

global class OpOwnerOldService_Scheduled Implements Schedulable
{
    global void execute(SchedulableContext sc)
    {
        sendEmailtoOppOwner();
    }

    public void sendEmailtoOppOwner()
    {
        List<Opportunity> listOpportunity = new List<Opportunity>();
        listOpportunity = [SELECT Id, OwnerId, Name, Owner.FirstName, (SELECT Id, Product2.Name FROM OpportunityLineItems WHERE Product2.Opportunity_Owner_Notification__c = FALSE
                        AND Product2.Isactive = FALSE
                        AND (Product2.RecordType.DeveloperName = 'Unavailable_R_D_Service' OR Product2.RecordType.DeveloperName = 'Unavailable_R_D_Project' ) LIMIT 1)
                       FROM Opportunity WHERE Id In
                       (SELECT OpportunityId FROM OpportunityLineItem WHERE Product2.Opportunity_Owner_Notification__c = FALSE
                        AND Product2.Isactive = FALSE
                        AND (Product2.RecordType.DeveloperName = 'Unavailable_R_D_Service' OR Product2.RecordType.DeveloperName = 'Unavailable_R_D_Project' ))];

        List<Opportunity>emailOpOwner = new list<Opportunity>();
        for(Opportunity opp : listOpportunity)
        {
          List <Messaging.SingleEmailMessage> mail = new list <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);
                emailOpOwner.add(mail);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
                    update listOpportunity;

Map<Id, Product2> products = new Map<Id, Product2>();
for(Opportunity opp: listOpportunity) {
    for(OpportunityLineItem oppLineItem: opp.opportunitylineitems) {
        products.put(oppLineItem.Product2Id, new Product2(Id=oppLineItem.Product2Id, Opportunity_Owner_Notification__c=true));
        }
    }
                    update products.values();
 }
}
9

It should instead be:

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for Opportunity record : records)
{
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage()
    // set properties on singular email
    emails.add(email);
}
Messaging.sendEmail(emails);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.