0

I am trying to send email through apex trigger. But it is not sending the proper list of opportunity ptoduct.

trigger OpportunityStageChange on Opportunity (after update) {

Set <ID> OpportunityID = new Set<ID>();

for(Opportunity objOpp:Trigger.new){
     OpportunityID.add(objOpp.id);
}

List<OpportunityLineItem> ListOpportunityLineItem = new List<OpportunityLineItem>();
ListOpportunityLineItem = [SELECT id,Product2.Name,Quantity,UnitPrice,TotalPrice FROM OpportunityLineItem WHERE OpportunityId IN:OpportunityID];
List<String> SendToAddress = new List<String>();
system.debug('ListOpportunityLineItem ------------------>'+ListOpportunityLineItem);
List<Messaging.SingleEmailMessage> lstEmailMsg = new List<Messaging.SingleEmailMessage>();

if(ListOpportunityLineItem.size()>0){

for(Opportunity objOpp:Trigger.new){
    String htmlbody;
    String Name;
    Decimal  Quantity;
    Decimal UnitPrice;
    Decimal TotalPrice;
    String ToAddress = '[email protected]';

     if(objOpp.StageName=='Closed Won'){

     htmlbody = '<table border="1" style=" border-collapse:collapse;border-spacing: 0;width:100%;height:100%;margin:0px;padding:0px;"><tr style="background-color:#aad4ff"><th>Product Name</th><th>Quantity</th><th>Unit Price</th><th>Total Price</th></tr>';

          for(OpportunityLineItem objOpportunity:ListOpportunityLineItem){                  
                  Name = objOpportunity.Product2.Name;
                  Quantity = objOpportunity.Quantity;
                  UnitPrice = objOpportunity.UnitPrice;
                  TotalPrice = objOpportunity.TotalPrice;

         if(Quantity==null && UnitPrice==null && TotalPrice==null){
              Quantity = 0;
              UnitPrice = 0;
              TotalPrice = 0;            
          }

           htmlBody +=  '<tr style="border:0px solid #000000;text-align:center; font-weight: bold;"><td>' + Name + '</td><td>' + Quantity + '</td><td>' + UnitPrice + '</td><td>' + TotalPrice + '</td></tr>';

         Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
         SendToAddress.add(ToAddress);
         message.setToAddresses(SendToAddress);
         message.subject = 'Product for'+'-'+ objOpp.Name;
         message.setHtmlBody(htmlBody);
         message.SaveAsActivity = false;
         system.debug('Message--------------------->'+message);
         lstEmailMsg.add(message);
        }






        system.debug('lstEmailMsg----------->'+lstEmailMsg.size());
    }
 }

}

   if(lstEmailMsg.size()>0){
           Messaging.sendEmail(lstEmailMsg);
   }

}

2
  • Looks like you're sending one email per OpportunityLineItem....is that your intention? Also, you're not closing your <table> Sep 19, 2016 at 10:33
  • This is not my intention, I want to send all OpportunityLineItem when Opportunity Stage is Closed Won Sep 19, 2016 at 10:41

1 Answer 1

1

I think you need to change the nesting of your code to this:

trigger OpportunityStageChange on Opportunity (after update) {

  Set <ID> OpportunityID = new Set<ID>();

  for(Opportunity objOpp:Trigger.new){
     OpportunityID.add(objOpp.id);
  }

  List<OpportunityLineItem> ListOpportunityLineItem = new List<OpportunityLineItem>();
  ListOpportunityLineItem = [SELECT id,Product2.Name,Quantity,UnitPrice,TotalPrice FROM OpportunityLineItem WHERE OpportunityId IN:OpportunityID];
  List<String> SendToAddress = new List<String>();
  system.debug('ListOpportunityLineItem ------------------>'+ListOpportunityLineItem);
  List<Messaging.SingleEmailMessage> lstEmailMsg = new List<Messaging.SingleEmailMessage>();

  if(ListOpportunityLineItem.size()>0){

    for(Opportunity objOpp:Trigger.new){
      String htmlbody;
      String Name;
      Decimal  Quantity;
      Decimal UnitPrice;
      Decimal TotalPrice;
      String ToAddress = '[email protected]';

      if(objOpp.StageName=='Closed Won'){

        htmlbody = '<table border="1" style=" border-collapse:collapse;border-spacing: 0;width:100%;height:100%;margin:0px;padding:0px;"><tr style="background-color:#aad4ff"><th>Product Name</th><th>Quantity</th><th>Unit Price</th><th>Total Price</th></tr>';

        for(OpportunityLineItem objOpportunity:ListOpportunityLineItem){                  
          Name = objOpportunity.Product2.Name;
          Quantity = objOpportunity.Quantity;
          UnitPrice = objOpportunity.UnitPrice;
          TotalPrice = objOpportunity.TotalPrice;

          if(Quantity==null && UnitPrice==null && TotalPrice==null){
              Quantity = 0;
              UnitPrice = 0;
              TotalPrice = 0;            
          }

          htmlBody +=  '<tr style="border:0px solid #000000;text-align:center; font-weight: bold;"><td>' + Name + '</td><td>' + Quantity + '</td><td>' + UnitPrice + '</td><td>' + TotalPrice + '</td></tr>';
        } // --> Close your OpportunityLineItem loop

        // This section to come out of the OpportunityLineItem group
        htmlbody += '</table>'

        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        SendToAddress.add(ToAddress);
        message.setToAddresses(SendToAddress);
        message.subject = 'Product for'+'-'+ objOpp.Name;
        message.setHtmlBody(htmlBody);
        message.SaveAsActivity = false;
        system.debug('Message--------------------->'+message);
        lstEmailMsg.add(message);

      }
    }
    system.debug('lstEmailMsg----------->'+lstEmailMsg.size());
    if(lstEmailMsg.size()>0){
       Messaging.sendEmail(lstEmailMsg);
    }  
  }
}

You must log in to answer this question.

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