function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sudhir_MeruSudhir_Meru 

Include List of Opportunity Name in Mail

Hi, 

 

  I need a  help in adding opportunity name inside a function. Below is the function which is actually a scheduler which runs every day.

 

global class TestScheduledApexFromTestMethod implements Schedulable {

 //public static String CRON_EXP = '0 0 0 3 9 ? 2013';
   
   global void execute(SchedulableContext ctx) 
   {
      
     List<IB_SUPPORT_CONTRACTS_MV__c> IBV =  [ SELECT  CONTRACT_NUMBER__c, SERIAL_NUMBER__c, SERVICE_END_DATE__c   
                                    FROM   IB_SUPPORT_CONTRACTS_MV__c 
                                    WHERE CONTRACT_NUMBER__c = 'MERU-7203' AND SERIAL_NUMBER__c != null  ];   
  
      List<Opportunity> lstOppInsert = new List<Opportunity>(); //List of opportunities to insert 
      
      for (IB_SUPPORT_CONTRACTS_MV__c ib : IBV  )
          
         {  
         
               //Single opp object to add in list
              Opportunity op = new Opportunity(
              name = ib.SERIAL_NUMBER__c,                  
              OwnerId = '00560000001vfE5AAI',
              Secondary_Owner__c = '00560000001vfE5AAI',
              discount_program__c = 'DEALREG',
              Abbv_Discount_Program__c = 'DR',
              CloseDate = Date.today(),
              StageName = 'Discovery', 
              Primary_Competitor__c = 'HP',
              type = 'Existing Customer',
              Government_Contract__c = 'None',
              leadsource = 'Deal Registration',
              Partner_Driven__c = 'yes',
              primary_distributor__c = '0016000000LmI0oAAF',
              primary_reseller__c = '0016000000N141hAAB',
              channel_source__c = 'Distributor',
              K_12__c  = 'No',
              Renewal_Opportunity__c = 'No',
              Renewal_Incumbant_Reseller__c = 'No',
              Renewal_K_12__c = 'No',    
              DEALREG_SEND_QUOTE__c = 'Do Not Send',
              RENEWAL_SEND_QUOTE__c = 'Do Not Send',  
              accountid = '001g0000004vLxmAAE',        
              Partner_Initiated_International__c = 'No',       
              Partner_Led__c = 'No');                         
              lstOppInsert.add(op); 
          }   

           //insert op;
            if(lstOppInsert.size()>0){
                Insert lstOppInsert; //This will insert all opportunities at once.
            }
             sendmail();
        }   
        
    public void sendmail()
     {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string [] toaddress= New string[]{'sudhirn@merunetworks.com','rberi@merunetworks.com','hradhakrishnan@merunetworks.com'};
        email.setSubject('Contracts Converted to Opportunities');
        email.setPlainTextBody('Testing Apex Scheduler-Body');  // I want to include all the opportunity name which got created.
        email.setToAddresses(toaddress);
        Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
        }
}

 

 In the above code inside sendmail() body i want to include all the opportunity which got created. Please suggest me how to add. 

 

Thanks
Sudhir

 

Best Answer chosen by Admin (Salesforce Developers) 
Grazitti InteractiveGrazitti Interactive

Hi,

 

Please define String allOppName globally as following. 

/**Whenever you get such problem declare variables or methods globally.**/

 

global class Sudhir_Send_Mail_Opp implements Schedulable {
//public static String CRON_EXP = '0 0 0 3 9 ? 2013';

public static String allOppName = '';
global void execute(SchedulableContext ctx)
{
List<IB_SUPPORT_CONTRACTS_MV__c> IBV = [ SELECT CONTRACT_NUMBER__c, SERIAL_NUMBER__c, SERVICE_END_DATE__c
FROM IB_SUPPORT_CONTRACTS_MV__c
WHERE CONTRACT_NUMBER__c = 'MERU-7203' AND SERIAL_NUMBER__c != null ];
List<Opportunity> lstOppInsert = new List<Opportunity>(); //List of opportunities to insert
for (IB_SUPPORT_CONTRACTS_MV__c ib : IBV )
{
//Single opp object to add in list
Opportunity op = new Opportunity(
name = ib.SERIAL_NUMBER__c,
OwnerId = '00560000001vfE5AAI',
Secondary_Owner__c = '00560000001vfE5AAI',
discount_program__c = 'DEALREG',
Abbv_Discount_Program__c = 'DR',
CloseDate = Date.today(),
StageName = 'Discovery',
Primary_Competitor__c = 'HP',
type = 'Existing Customer',
Government_Contract__c = 'None',
leadsource = 'Deal Registration',
Partner_Driven__c = 'yes',
primary_distributor__c = '0016000000LmI0oAAF',
primary_reseller__c = '0016000000N141hAAB',
channel_source__c = 'Distributor',
K_12__c = 'No',
Renewal_Opportunity__c = 'No',
Renewal_Incumbant_Reseller__c = 'No',
Renewal_K_12__c = 'No',
DEALREG_SEND_QUOTE__c = 'Do Not Send',
RENEWAL_SEND_QUOTE__c = 'Do Not Send',
accountid = '001g0000004vLxmAAE',
Partner_Initiated_International__c = 'No',
Partner_Led__c = 'No');
lstOppInsert.add(op);
}
//insert op;
/*if(lstOppInsert.size()>0){
Insert lstOppInsert; //This will insert all opportunities at once.
}*/
Set<Id> oppId = new Set<Id>();
if(!lstOppInsert.isEmpty()){
Database.SaveResult[] lsr = Database.update(lstOppInsert,false);
for(Database.SaveResult sr:lsr){
if(sr.isSuccess()){
oppId.add(sr.getId());
}
}
}
Set<String> oppName = new Set<String>();

for(Opportunity opp : [select Name from Opportunity where Id In : oppId ]){
// do whatever you want for example create a set and add name of Opportunities in set.
oppName.add(opp.Name);
//or you can create a string of concatenated name as following
allOppName += opp.Name+',';
}
sendmail();
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress= New string[]{'sudhirn@merunetworks.com','rberi@merunetworks.com','hradhakrishnan@merunetworks.com'};
email.setSubject('Contracts Converted to Opportunities');
//Now use here set oppName or string allOppName to display names of Opportunities.
email.setPlainTextBody('Testing Apex Scheduler-Body' + allOppName ); // I want to include all the opportunity name which got created.
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}

 

Thanks,

www.grazitti.com

All Answers

Grazitti InteractiveGrazitti Interactive

Hi Sudhir,

You can create a set of name of opportunity as following and can use in your method sendMail().

global class TestScheduledApexFromTestMethod implements Schedulable {
//public static String CRON_EXP = '0 0 0 3 9 ? 2013';
global void execute(SchedulableContext ctx)
{
List<IB_SUPPORT_CONTRACTS_MV__c> IBV = [ SELECT CONTRACT_NUMBER__c, SERIAL_NUMBER__c, SERVICE_END_DATE__c
FROM IB_SUPPORT_CONTRACTS_MV__c
WHERE CONTRACT_NUMBER__c = 'MERU-7203' AND SERIAL_NUMBER__c != null ];
List<Opportunity> lstOppInsert = new List<Opportunity>(); //List of opportunities to insert
for (IB_SUPPORT_CONTRACTS_MV__c ib : IBV )
{
//Single opp object to add in list
Opportunity op = new Opportunity(
name = ib.SERIAL_NUMBER__c,
OwnerId = '00560000001vfE5AAI',
Secondary_Owner__c = '00560000001vfE5AAI',
discount_program__c = 'DEALREG',
Abbv_Discount_Program__c = 'DR',
CloseDate = Date.today(),
StageName = 'Discovery',
Primary_Competitor__c = 'HP',
type = 'Existing Customer',
Government_Contract__c = 'None',
leadsource = 'Deal Registration',
Partner_Driven__c = 'yes',
primary_distributor__c = '0016000000LmI0oAAF',
primary_reseller__c = '0016000000N141hAAB',
channel_source__c = 'Distributor',
K_12__c = 'No',
Renewal_Opportunity__c = 'No',
Renewal_Incumbant_Reseller__c = 'No',
Renewal_K_12__c = 'No',
DEALREG_SEND_QUOTE__c = 'Do Not Send',
RENEWAL_SEND_QUOTE__c = 'Do Not Send',
accountid = '001g0000004vLxmAAE',
Partner_Initiated_International__c = 'No',
Partner_Led__c = 'No');
lstOppInsert.add(op);
}
//insert op;
/*if(lstOppInsert.size()>0){
Insert lstOppInsert; //This will insert all opportunities at once.
}*/
Set<Id> oppId = new Set<Id>();
if(!lstOppInsert.isEmpty()){
Database.SaveResult[] lsr = Database.update(lstOppInsert,false);
for(Database.SaveResult sr:lsr){
if(sr.isSuccess()){
oppId.add(sr.getId());
}
}
}
Set<String> oppName = new Set<String>();

String allOppName = '';
for(Opportunity opp : [select Name from Opportunity where Id In : oppId ]){
// do whatever you want for example create a set and add name of Opportunities in set.
oppName.add(opp.Name);

//or you can create a string of concatenated name as following

allOppName += opp.Name+','
}
sendmail();
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress= New string[]{'sudhirn@merunetworks.com','rberi@merunetworks.com','hradhakrishnan@merunetworks.com'};
email.setSubject('Contracts Converted to Opportunities');

//Now use here set oppName or string allOppName to display names of Opportunities.
email.setPlainTextBody('Testing Apex Scheduler-Body'); // I want to include all the opportunity name which got created.
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}


Grazitti
www.grazitti.com

Sudhir_MeruSudhir_Meru

Hi Grazitti,

 

  Thanks for your suggestion. Can you tell me how is the opportunity names are getting added inside body of the mail?

 

 Please suggest me how to add inside  body. 

 

email.setPlainTextBody('Testing Apex Scheduler-Body');

Thanks

Sudhir 

 

Grazitti InteractiveGrazitti Interactive

Hi Sudhir,

 

Already I have defined a string variable allOppName that contains all opportunity names that have been created .

Now you can pass this string in method setPlainTextBody().

 email.setPlainTextBody(allOppName);

 

Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.

Sudhir_MeruSudhir_Meru

Hi,

 

  Tried your method Insert is not happening also when i add allOppName in body of mail it giving variable out of bound. 

   

global class Sudhir_Send_Mail_Opp implements Schedulable {
//public static String CRON_EXP = '0 0 0 3 9 ? 2013';
global void execute(SchedulableContext ctx)
{
List<IB_SUPPORT_CONTRACTS_MV__c> IBV = [ SELECT CONTRACT_NUMBER__c, SERIAL_NUMBER__c, SERVICE_END_DATE__c
FROM IB_SUPPORT_CONTRACTS_MV__c
WHERE CONTRACT_NUMBER__c = 'MERU-7203' AND SERIAL_NUMBER__c != null ];
List<Opportunity> lstOppInsert = new List<Opportunity>(); //List of opportunities to insert
for (IB_SUPPORT_CONTRACTS_MV__c ib : IBV )
{
//Single opp object to add in list
Opportunity op = new Opportunity(
name = ib.SERIAL_NUMBER__c,
OwnerId = '00560000001vfE5AAI',
Secondary_Owner__c = '00560000001vfE5AAI',
discount_program__c = 'DEALREG',
Abbv_Discount_Program__c = 'DR',
CloseDate = Date.today(),
StageName = 'Discovery',
Primary_Competitor__c = 'HP',
type = 'Existing Customer',
Government_Contract__c = 'None',
leadsource = 'Deal Registration',
Partner_Driven__c = 'yes',
primary_distributor__c = '0016000000LmI0oAAF',
primary_reseller__c = '0016000000N141hAAB',
channel_source__c = 'Distributor',
K_12__c = 'No',
Renewal_Opportunity__c = 'No',
Renewal_Incumbant_Reseller__c = 'No',
Renewal_K_12__c = 'No',
DEALREG_SEND_QUOTE__c = 'Do Not Send',
RENEWAL_SEND_QUOTE__c = 'Do Not Send',
accountid = '001g0000004vLxmAAE',
Partner_Initiated_International__c = 'No',
Partner_Led__c = 'No');
lstOppInsert.add(op);
}
//insert op;
/*if(lstOppInsert.size()>0){
Insert lstOppInsert; //This will insert all opportunities at once.
}*/
Set<Id> oppId = new Set<Id>();
if(!lstOppInsert.isEmpty()){
Database.SaveResult[] lsr = Database.update(lstOppInsert,false);
for(Database.SaveResult sr:lsr){
if(sr.isSuccess()){
oppId.add(sr.getId());
}
}
}
Set<String> oppName = new Set<String>();
String allOppName = '';
for(Opportunity opp : [select Name from Opportunity where Id In : oppId ]){
// do whatever you want for example create a set and add name of Opportunities in set.
oppName.add(opp.Name);
//or you can create a string of concatenated name as following
allOppName += opp.Name+',';
}
sendmail();
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress= New string[]{'sudhirn@merunetworks.com','rberi@merunetworks.com','hradhakrishnan@merunetworks.com'};
email.setSubject('Contracts Converted to Opportunities');
//Now use here set oppName or string allOppName to display names of Opportunities.
email.setPlainTextBody('Testing Apex Scheduler-Body' + allOppName  ); // I want to include all the opportunity name which got created.
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}

 

 

Please tell me what is the mistake here

 

Thanks

Sudhir

Grazitti InteractiveGrazitti Interactive

Hi,

 

Please define String allOppName globally as following. 

/**Whenever you get such problem declare variables or methods globally.**/

 

global class Sudhir_Send_Mail_Opp implements Schedulable {
//public static String CRON_EXP = '0 0 0 3 9 ? 2013';

public static String allOppName = '';
global void execute(SchedulableContext ctx)
{
List<IB_SUPPORT_CONTRACTS_MV__c> IBV = [ SELECT CONTRACT_NUMBER__c, SERIAL_NUMBER__c, SERVICE_END_DATE__c
FROM IB_SUPPORT_CONTRACTS_MV__c
WHERE CONTRACT_NUMBER__c = 'MERU-7203' AND SERIAL_NUMBER__c != null ];
List<Opportunity> lstOppInsert = new List<Opportunity>(); //List of opportunities to insert
for (IB_SUPPORT_CONTRACTS_MV__c ib : IBV )
{
//Single opp object to add in list
Opportunity op = new Opportunity(
name = ib.SERIAL_NUMBER__c,
OwnerId = '00560000001vfE5AAI',
Secondary_Owner__c = '00560000001vfE5AAI',
discount_program__c = 'DEALREG',
Abbv_Discount_Program__c = 'DR',
CloseDate = Date.today(),
StageName = 'Discovery',
Primary_Competitor__c = 'HP',
type = 'Existing Customer',
Government_Contract__c = 'None',
leadsource = 'Deal Registration',
Partner_Driven__c = 'yes',
primary_distributor__c = '0016000000LmI0oAAF',
primary_reseller__c = '0016000000N141hAAB',
channel_source__c = 'Distributor',
K_12__c = 'No',
Renewal_Opportunity__c = 'No',
Renewal_Incumbant_Reseller__c = 'No',
Renewal_K_12__c = 'No',
DEALREG_SEND_QUOTE__c = 'Do Not Send',
RENEWAL_SEND_QUOTE__c = 'Do Not Send',
accountid = '001g0000004vLxmAAE',
Partner_Initiated_International__c = 'No',
Partner_Led__c = 'No');
lstOppInsert.add(op);
}
//insert op;
/*if(lstOppInsert.size()>0){
Insert lstOppInsert; //This will insert all opportunities at once.
}*/
Set<Id> oppId = new Set<Id>();
if(!lstOppInsert.isEmpty()){
Database.SaveResult[] lsr = Database.update(lstOppInsert,false);
for(Database.SaveResult sr:lsr){
if(sr.isSuccess()){
oppId.add(sr.getId());
}
}
}
Set<String> oppName = new Set<String>();

for(Opportunity opp : [select Name from Opportunity where Id In : oppId ]){
// do whatever you want for example create a set and add name of Opportunities in set.
oppName.add(opp.Name);
//or you can create a string of concatenated name as following
allOppName += opp.Name+',';
}
sendmail();
}
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
string [] toaddress= New string[]{'sudhirn@merunetworks.com','rberi@merunetworks.com','hradhakrishnan@merunetworks.com'};
email.setSubject('Contracts Converted to Opportunities');
//Now use here set oppName or string allOppName to display names of Opportunities.
email.setPlainTextBody('Testing Apex Scheduler-Body' + allOppName ); // I want to include all the opportunity name which got created.
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}

 

Thanks,

www.grazitti.com

This was selected as the best answer