+ Start a Discussion
SambitNayakSambitNayak 

Opportunity Apex to iterate over products.

Hi,
I need to send email to the Opportunity owner with the list of Products (added in the Opportunity) when the Opportunity is Closed Won. I am unable to write the logic for iterating over the products.
Thanks.
Best Answer chosen by SambitNayak
Lukesh KarmoreLukesh Karmore
Hello Sambit Nayak 8, 
Try below code it works ,Hope it will solve your issue And don't forget to mark the best answer ,so it will helps other.
 

trigger SendEmailToOppOwnerWithProduct on Opportunity (after insert ,after update) {
    if(Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate){
        list<Messaging.SingleEmailMessage> mailList=new list<Messaging.SingleEmailMessage>();
        
        for(Opportunity opp:trigger.new){
        if(opp.stagename!=null &&  opp.stagename=='Closed Won'){
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            list<string> toaddress=new list<string>();
            toaddress.add(UserInfo.getUserEmail());
            message.setToAddresses(toaddress);
            message.setSubject('Opp id Closed Won');
            string body='Your Opportunity is closed Won having Products<br> ';
        Integer i=0;
        for(OpportunityLineitem oli:[SELECT product2.name FROM OpportunityLineitem WHERE OpportunityId=:opp.id]){
            i++;
       body += String.valueOf(i)+'.' +oli.product2.name+ '<br>';
        }
        message.setPlainTextBody(body);
        mailList.add(message);
            }
        }
            if(!mailList.isEmpty()){
                Messaging.sendEmail(mailList);
            }
    }
}
        
Thanks

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Sambit,

Opportunity Product(The API name is OpportunityLineItem.) is a Junction Object between Opportunity and Product2 objects. 

Please check below reference for fetching product:
https://developer.salesforce.com/forums/?id=906F0000000BMcAIAW

If it helps , Please mark it as best answer.

Thanks!


 
mukesh guptamukesh gupta
Hi Sambit,

Please follow below code
 
List <Opportunity>oppList = [SELECT Id, Name, Account.Name, 
  (SELECT Quantity, UnitPrice, TotalPrice, 
   PricebookEntry.Name, PricebookEntry.Product2.Family, PricebookEntry.Product2.Name  FROM 
   OpportunityLineItems) 
FROM Opportunity WHERE Id = :YourOppId];

for(OpportunityLineItems OLI : oppList.OpportunityLineItems){

system.debug(OLI.PricebookEntry.Product2.Name );
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Lukesh KarmoreLukesh Karmore
Hello Sambit Nayak 8, 
Try below code it works ,Hope it will solve your issue And don't forget to mark the best answer ,so it will helps other.
 

trigger SendEmailToOppOwnerWithProduct on Opportunity (after insert ,after update) {
    if(Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate){
        list<Messaging.SingleEmailMessage> mailList=new list<Messaging.SingleEmailMessage>();
        
        for(Opportunity opp:trigger.new){
        if(opp.stagename!=null &&  opp.stagename=='Closed Won'){
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            list<string> toaddress=new list<string>();
            toaddress.add(UserInfo.getUserEmail());
            message.setToAddresses(toaddress);
            message.setSubject('Opp id Closed Won');
            string body='Your Opportunity is closed Won having Products<br> ';
        Integer i=0;
        for(OpportunityLineitem oli:[SELECT product2.name FROM OpportunityLineitem WHERE OpportunityId=:opp.id]){
            i++;
       body += String.valueOf(i)+'.' +oli.product2.name+ '<br>';
        }
        message.setPlainTextBody(body);
        mailList.add(message);
            }
        }
            if(!mailList.isEmpty()){
                Messaging.sendEmail(mailList);
            }
    }
}
        
Thanks
This was selected as the best answer
SambitNayakSambitNayak

Thanks Lukesh.... This is exactly what I was looking for....

Really appreciate your command on the Apex.