You need to sign in to do that
Don't have an account?
SambitNayak
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.
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.
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
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!
Please follow below code
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
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
Thanks Lukesh.... This is exactly what I was looking for....
Really appreciate your command on the Apex.