Salesforce APEX method not bulkified










1















I have written an APEX Class that sends an email when a client is released. There is a method that I thought I had bulkified but I was told it does not. This is because this method calls another function which actually does the actual email creation and that is not bulkified. Can someone guide me as to how the SOQL queries can be taken out of the method?



global class LM_ChangeAccountRT {

private static final Profile sysAdmin = [select id from profile where name='System Administrator'];

@AuraEnabled
public static String resendEmails(List<String> accountIdList)
String response = null;
try catch(Exception e)
System.debug(e.getMessage());
response = 'Error sending emails';

return response;


public static void sendpdfgenerationEmails(Account acc)
system.debug('start of confirmation card and pdf generation');
//Logic to find which VF template is used to send an email.
list<EmailTemplate> templateId = new list<EmailTemplate>();
string temppartner;
String partner_opt_in_attachment;
boolean sendFCAmail;

List<Dealership_PDF_Generation__c> custsettingdata = Dealership_PDF_Generation__c.getall().values();
System.debug('custom setting size = ' + custsettingdata.size());
// Fetch State
if(acc.Dealership_State__c!=null && acc.Dealership_Partner__c!=null)

for(Dealership_PDF_Generation__c tempcustsetting :custsettingdata)


if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='WA' && acc.Dealership_State__c=='WA')

//For WA State
// temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
if(acc.Dealership_Spiff_Payment__c == '% premium')
partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
else
partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='TX' && acc.Dealership_State__c=='TX')
//For TX State
//temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
if(acc.Dealership_Spiff_Payment__c == '% premium')
partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
else
partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c!=tempcustsetting.State__c && tempcustsetting.State__c!='TX' && acc.Dealership_State__c!='TX' && acc.Dealership_State__c!='WA' &&tempcustsetting.State__c!='WA' )
//For Non TX State
//temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
if(acc.Dealership_Spiff_Payment__c == '% premium')
partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
else
partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;

system.debug('grabbed template: ' + temppartner);

if(acc.Dealership_Partner__c != null && temppartner!=null )
templateId.add([Select id,DeveloperName from EmailTemplate where DeveloperName = :temppartner]); //This will probably cause governor limit issues. First problem



if (partner_opt_in_attachment != null)
StaticResource sr = [Select s.Name, s.Id, s.Body From StaticResource s where s.Name =: partner_opt_in_attachment]; //'static_resource' is the name of the static resource PDF. This is another SOQL query that will cause problems

Blob tempBlob = sr.Body;

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setBody(tempBlob);
efa.setFileName('Opt-in.pdf');

List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
attachments.add(efa);
// add attachment to each email
for (Messaging.SingleEmailMessage email : emails)
email.setFileAttachments(attachments);



system.debug('email sent: ' + emails.size());
Messaging.sendEmail(emails);






The reason why I am trying to bulkify this is because I have written a APEX scheduler that calls the resendemails method everyday at 7am to check which records need to have an email sent. I am afraid that if there are more than a 100 clients then it will cause problems and not send the emails. Any suggestions on how I can optimize the sendpdfemailgeenration() method?
Thank you










share|improve this question


























    1















    I have written an APEX Class that sends an email when a client is released. There is a method that I thought I had bulkified but I was told it does not. This is because this method calls another function which actually does the actual email creation and that is not bulkified. Can someone guide me as to how the SOQL queries can be taken out of the method?



    global class LM_ChangeAccountRT {

    private static final Profile sysAdmin = [select id from profile where name='System Administrator'];

    @AuraEnabled
    public static String resendEmails(List<String> accountIdList)
    String response = null;
    try catch(Exception e)
    System.debug(e.getMessage());
    response = 'Error sending emails';

    return response;


    public static void sendpdfgenerationEmails(Account acc)
    system.debug('start of confirmation card and pdf generation');
    //Logic to find which VF template is used to send an email.
    list<EmailTemplate> templateId = new list<EmailTemplate>();
    string temppartner;
    String partner_opt_in_attachment;
    boolean sendFCAmail;

    List<Dealership_PDF_Generation__c> custsettingdata = Dealership_PDF_Generation__c.getall().values();
    System.debug('custom setting size = ' + custsettingdata.size());
    // Fetch State
    if(acc.Dealership_State__c!=null && acc.Dealership_Partner__c!=null)

    for(Dealership_PDF_Generation__c tempcustsetting :custsettingdata)


    if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='WA' && acc.Dealership_State__c=='WA')

    //For WA State
    // temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
    temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
    if(acc.Dealership_Spiff_Payment__c == '% premium')
    partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
    else
    partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


    else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='TX' && acc.Dealership_State__c=='TX')
    //For TX State
    //temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
    temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
    if(acc.Dealership_Spiff_Payment__c == '% premium')
    partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
    else
    partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


    else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c!=tempcustsetting.State__c && tempcustsetting.State__c!='TX' && acc.Dealership_State__c!='TX' && acc.Dealership_State__c!='WA' &&tempcustsetting.State__c!='WA' )
    //For Non TX State
    //temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
    temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
    if(acc.Dealership_Spiff_Payment__c == '% premium')
    partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
    else
    partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;

    system.debug('grabbed template: ' + temppartner);

    if(acc.Dealership_Partner__c != null && temppartner!=null )
    templateId.add([Select id,DeveloperName from EmailTemplate where DeveloperName = :temppartner]); //This will probably cause governor limit issues. First problem



    if (partner_opt_in_attachment != null)
    StaticResource sr = [Select s.Name, s.Id, s.Body From StaticResource s where s.Name =: partner_opt_in_attachment]; //'static_resource' is the name of the static resource PDF. This is another SOQL query that will cause problems

    Blob tempBlob = sr.Body;

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setBody(tempBlob);
    efa.setFileName('Opt-in.pdf');

    List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
    attachments.add(efa);
    // add attachment to each email
    for (Messaging.SingleEmailMessage email : emails)
    email.setFileAttachments(attachments);



    system.debug('email sent: ' + emails.size());
    Messaging.sendEmail(emails);






    The reason why I am trying to bulkify this is because I have written a APEX scheduler that calls the resendemails method everyday at 7am to check which records need to have an email sent. I am afraid that if there are more than a 100 clients then it will cause problems and not send the emails. Any suggestions on how I can optimize the sendpdfemailgeenration() method?
    Thank you










    share|improve this question
























      1












      1








      1


      0






      I have written an APEX Class that sends an email when a client is released. There is a method that I thought I had bulkified but I was told it does not. This is because this method calls another function which actually does the actual email creation and that is not bulkified. Can someone guide me as to how the SOQL queries can be taken out of the method?



      global class LM_ChangeAccountRT {

      private static final Profile sysAdmin = [select id from profile where name='System Administrator'];

      @AuraEnabled
      public static String resendEmails(List<String> accountIdList)
      String response = null;
      try catch(Exception e)
      System.debug(e.getMessage());
      response = 'Error sending emails';

      return response;


      public static void sendpdfgenerationEmails(Account acc)
      system.debug('start of confirmation card and pdf generation');
      //Logic to find which VF template is used to send an email.
      list<EmailTemplate> templateId = new list<EmailTemplate>();
      string temppartner;
      String partner_opt_in_attachment;
      boolean sendFCAmail;

      List<Dealership_PDF_Generation__c> custsettingdata = Dealership_PDF_Generation__c.getall().values();
      System.debug('custom setting size = ' + custsettingdata.size());
      // Fetch State
      if(acc.Dealership_State__c!=null && acc.Dealership_Partner__c!=null)

      for(Dealership_PDF_Generation__c tempcustsetting :custsettingdata)


      if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='WA' && acc.Dealership_State__c=='WA')

      //For WA State
      // temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
      temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
      if(acc.Dealership_Spiff_Payment__c == '% premium')
      partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
      else
      partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


      else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='TX' && acc.Dealership_State__c=='TX')
      //For TX State
      //temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
      temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
      if(acc.Dealership_Spiff_Payment__c == '% premium')
      partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
      else
      partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


      else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c!=tempcustsetting.State__c && tempcustsetting.State__c!='TX' && acc.Dealership_State__c!='TX' && acc.Dealership_State__c!='WA' &&tempcustsetting.State__c!='WA' )
      //For Non TX State
      //temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
      temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
      if(acc.Dealership_Spiff_Payment__c == '% premium')
      partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
      else
      partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;

      system.debug('grabbed template: ' + temppartner);

      if(acc.Dealership_Partner__c != null && temppartner!=null )
      templateId.add([Select id,DeveloperName from EmailTemplate where DeveloperName = :temppartner]); //This will probably cause governor limit issues. First problem



      if (partner_opt_in_attachment != null)
      StaticResource sr = [Select s.Name, s.Id, s.Body From StaticResource s where s.Name =: partner_opt_in_attachment]; //'static_resource' is the name of the static resource PDF. This is another SOQL query that will cause problems

      Blob tempBlob = sr.Body;

      Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
      efa.setBody(tempBlob);
      efa.setFileName('Opt-in.pdf');

      List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
      attachments.add(efa);
      // add attachment to each email
      for (Messaging.SingleEmailMessage email : emails)
      email.setFileAttachments(attachments);



      system.debug('email sent: ' + emails.size());
      Messaging.sendEmail(emails);






      The reason why I am trying to bulkify this is because I have written a APEX scheduler that calls the resendemails method everyday at 7am to check which records need to have an email sent. I am afraid that if there are more than a 100 clients then it will cause problems and not send the emails. Any suggestions on how I can optimize the sendpdfemailgeenration() method?
      Thank you










      share|improve this question














      I have written an APEX Class that sends an email when a client is released. There is a method that I thought I had bulkified but I was told it does not. This is because this method calls another function which actually does the actual email creation and that is not bulkified. Can someone guide me as to how the SOQL queries can be taken out of the method?



      global class LM_ChangeAccountRT {

      private static final Profile sysAdmin = [select id from profile where name='System Administrator'];

      @AuraEnabled
      public static String resendEmails(List<String> accountIdList)
      String response = null;
      try catch(Exception e)
      System.debug(e.getMessage());
      response = 'Error sending emails';

      return response;


      public static void sendpdfgenerationEmails(Account acc)
      system.debug('start of confirmation card and pdf generation');
      //Logic to find which VF template is used to send an email.
      list<EmailTemplate> templateId = new list<EmailTemplate>();
      string temppartner;
      String partner_opt_in_attachment;
      boolean sendFCAmail;

      List<Dealership_PDF_Generation__c> custsettingdata = Dealership_PDF_Generation__c.getall().values();
      System.debug('custom setting size = ' + custsettingdata.size());
      // Fetch State
      if(acc.Dealership_State__c!=null && acc.Dealership_Partner__c!=null)

      for(Dealership_PDF_Generation__c tempcustsetting :custsettingdata)


      if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='WA' && acc.Dealership_State__c=='WA')

      //For WA State
      // temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
      temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
      if(acc.Dealership_Spiff_Payment__c == '% premium')
      partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
      else
      partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


      else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c==tempcustsetting.State__c && tempcustsetting.State__c=='TX' && acc.Dealership_State__c=='TX')
      //For TX State
      //temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
      temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
      if(acc.Dealership_Spiff_Payment__c == '% premium')
      partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
      else
      partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;


      else if(acc.Dealership_Partner__c == tempcustsetting.Dealership_Partner__c && acc.Dealership_State__c!=tempcustsetting.State__c && tempcustsetting.State__c!='TX' && acc.Dealership_State__c!='TX' && acc.Dealership_State__c!='WA' &&tempcustsetting.State__c!='WA' )
      //For Non TX State
      //temppartner= '%' + tempcustsetting.TEMPLATE_Unique_name__c + '%';
      temppartner= tempcustsetting.TEMPLATE_Unique_name__c;
      if(acc.Dealership_Spiff_Payment__c == '% premium')
      partner_opt_in_attachment=tempcustsetting.opt_in_form_premium__c;
      else
      partner_opt_in_attachment=tempcustsetting.opt_in_form_nonpremium__c;

      system.debug('grabbed template: ' + temppartner);

      if(acc.Dealership_Partner__c != null && temppartner!=null )
      templateId.add([Select id,DeveloperName from EmailTemplate where DeveloperName = :temppartner]); //This will probably cause governor limit issues. First problem



      if (partner_opt_in_attachment != null)
      StaticResource sr = [Select s.Name, s.Id, s.Body From StaticResource s where s.Name =: partner_opt_in_attachment]; //'static_resource' is the name of the static resource PDF. This is another SOQL query that will cause problems

      Blob tempBlob = sr.Body;

      Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
      efa.setBody(tempBlob);
      efa.setFileName('Opt-in.pdf');

      List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();
      attachments.add(efa);
      // add attachment to each email
      for (Messaging.SingleEmailMessage email : emails)
      email.setFileAttachments(attachments);



      system.debug('email sent: ' + emails.size());
      Messaging.sendEmail(emails);






      The reason why I am trying to bulkify this is because I have written a APEX scheduler that calls the resendemails method everyday at 7am to check which records need to have an email sent. I am afraid that if there are more than a 100 clients then it will cause problems and not send the emails. Any suggestions on how I can optimize the sendpdfemailgeenration() method?
      Thank you







      salesforce apex






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 19:20









      user3685949user3685949

      84




      84






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Yes, you are right - your's resendEmails() method is not bulkified.




          Firstly, let me explain you why is that:



          • SOQL to get Accounts


          • Loop 1 on List of Account records

            • Call sendpdfgenerationEmails() method

            • Retrieve list of Dealership_PDF_Generation__c records


            • Loop 2 on List of Dealership_PDF_Generation__c records

              • SOQL to get StaticResources - Very bad! It's inside double loop!

              • Call Messaging.sendEmail() method - Very bad! It's inside double loop!



          • Update on List of Account records


          You need to remember that:



          1. You should never do SOQLs in loops! - Limit 100 SOQLs per transaction



          2. You should never call Messaging.sendEmail() in loops! - Limit 10 calls per transaction




          Now let me guide you how to refactor this method:



          @AuraEnabled
          public static String resendEmails(List<String> accountIdList)
          // 1. SOQL for List of Account records
          // 2. Retrieve list of Dealership_PDF_Generation__c records
          // 3. SOQL for List of StaticResources for all Names from Dealership_PDF_Generation__c records
          // 4. Declaration of new List variable for Messaging.SingleEmailMessage objects
          // 5. Loop 1 on List of Account records
          // 6. Call new "prepareEmailsForAccount()" method, which prepares and returns list of Messaging.SingleEmailMessage objects
          // 7. Add returned Messaging.SingleEmailMessage objects to list from point 4
          // 8. End of loop 1
          // 9. Call "Messaging.sendEmail()" method with list from point 4
          // 10. Update on List of Account records



          With this you will avoid SOQLs and calling Messaging.sendEmail() method in loops.






          share|improve this answer























          • Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

            – user3685949
            Jan 30 at 1:47











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326551%2fsalesforce-apex-method-not-bulkified%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Yes, you are right - your's resendEmails() method is not bulkified.




          Firstly, let me explain you why is that:



          • SOQL to get Accounts


          • Loop 1 on List of Account records

            • Call sendpdfgenerationEmails() method

            • Retrieve list of Dealership_PDF_Generation__c records


            • Loop 2 on List of Dealership_PDF_Generation__c records

              • SOQL to get StaticResources - Very bad! It's inside double loop!

              • Call Messaging.sendEmail() method - Very bad! It's inside double loop!



          • Update on List of Account records


          You need to remember that:



          1. You should never do SOQLs in loops! - Limit 100 SOQLs per transaction



          2. You should never call Messaging.sendEmail() in loops! - Limit 10 calls per transaction




          Now let me guide you how to refactor this method:



          @AuraEnabled
          public static String resendEmails(List<String> accountIdList)
          // 1. SOQL for List of Account records
          // 2. Retrieve list of Dealership_PDF_Generation__c records
          // 3. SOQL for List of StaticResources for all Names from Dealership_PDF_Generation__c records
          // 4. Declaration of new List variable for Messaging.SingleEmailMessage objects
          // 5. Loop 1 on List of Account records
          // 6. Call new "prepareEmailsForAccount()" method, which prepares and returns list of Messaging.SingleEmailMessage objects
          // 7. Add returned Messaging.SingleEmailMessage objects to list from point 4
          // 8. End of loop 1
          // 9. Call "Messaging.sendEmail()" method with list from point 4
          // 10. Update on List of Account records



          With this you will avoid SOQLs and calling Messaging.sendEmail() method in loops.






          share|improve this answer























          • Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

            – user3685949
            Jan 30 at 1:47
















          1














          Yes, you are right - your's resendEmails() method is not bulkified.




          Firstly, let me explain you why is that:



          • SOQL to get Accounts


          • Loop 1 on List of Account records

            • Call sendpdfgenerationEmails() method

            • Retrieve list of Dealership_PDF_Generation__c records


            • Loop 2 on List of Dealership_PDF_Generation__c records

              • SOQL to get StaticResources - Very bad! It's inside double loop!

              • Call Messaging.sendEmail() method - Very bad! It's inside double loop!



          • Update on List of Account records


          You need to remember that:



          1. You should never do SOQLs in loops! - Limit 100 SOQLs per transaction



          2. You should never call Messaging.sendEmail() in loops! - Limit 10 calls per transaction




          Now let me guide you how to refactor this method:



          @AuraEnabled
          public static String resendEmails(List<String> accountIdList)
          // 1. SOQL for List of Account records
          // 2. Retrieve list of Dealership_PDF_Generation__c records
          // 3. SOQL for List of StaticResources for all Names from Dealership_PDF_Generation__c records
          // 4. Declaration of new List variable for Messaging.SingleEmailMessage objects
          // 5. Loop 1 on List of Account records
          // 6. Call new "prepareEmailsForAccount()" method, which prepares and returns list of Messaging.SingleEmailMessage objects
          // 7. Add returned Messaging.SingleEmailMessage objects to list from point 4
          // 8. End of loop 1
          // 9. Call "Messaging.sendEmail()" method with list from point 4
          // 10. Update on List of Account records



          With this you will avoid SOQLs and calling Messaging.sendEmail() method in loops.






          share|improve this answer























          • Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

            – user3685949
            Jan 30 at 1:47














          1












          1








          1







          Yes, you are right - your's resendEmails() method is not bulkified.




          Firstly, let me explain you why is that:



          • SOQL to get Accounts


          • Loop 1 on List of Account records

            • Call sendpdfgenerationEmails() method

            • Retrieve list of Dealership_PDF_Generation__c records


            • Loop 2 on List of Dealership_PDF_Generation__c records

              • SOQL to get StaticResources - Very bad! It's inside double loop!

              • Call Messaging.sendEmail() method - Very bad! It's inside double loop!



          • Update on List of Account records


          You need to remember that:



          1. You should never do SOQLs in loops! - Limit 100 SOQLs per transaction



          2. You should never call Messaging.sendEmail() in loops! - Limit 10 calls per transaction




          Now let me guide you how to refactor this method:



          @AuraEnabled
          public static String resendEmails(List<String> accountIdList)
          // 1. SOQL for List of Account records
          // 2. Retrieve list of Dealership_PDF_Generation__c records
          // 3. SOQL for List of StaticResources for all Names from Dealership_PDF_Generation__c records
          // 4. Declaration of new List variable for Messaging.SingleEmailMessage objects
          // 5. Loop 1 on List of Account records
          // 6. Call new "prepareEmailsForAccount()" method, which prepares and returns list of Messaging.SingleEmailMessage objects
          // 7. Add returned Messaging.SingleEmailMessage objects to list from point 4
          // 8. End of loop 1
          // 9. Call "Messaging.sendEmail()" method with list from point 4
          // 10. Update on List of Account records



          With this you will avoid SOQLs and calling Messaging.sendEmail() method in loops.






          share|improve this answer













          Yes, you are right - your's resendEmails() method is not bulkified.




          Firstly, let me explain you why is that:



          • SOQL to get Accounts


          • Loop 1 on List of Account records

            • Call sendpdfgenerationEmails() method

            • Retrieve list of Dealership_PDF_Generation__c records


            • Loop 2 on List of Dealership_PDF_Generation__c records

              • SOQL to get StaticResources - Very bad! It's inside double loop!

              • Call Messaging.sendEmail() method - Very bad! It's inside double loop!



          • Update on List of Account records


          You need to remember that:



          1. You should never do SOQLs in loops! - Limit 100 SOQLs per transaction



          2. You should never call Messaging.sendEmail() in loops! - Limit 10 calls per transaction




          Now let me guide you how to refactor this method:



          @AuraEnabled
          public static String resendEmails(List<String> accountIdList)
          // 1. SOQL for List of Account records
          // 2. Retrieve list of Dealership_PDF_Generation__c records
          // 3. SOQL for List of StaticResources for all Names from Dealership_PDF_Generation__c records
          // 4. Declaration of new List variable for Messaging.SingleEmailMessage objects
          // 5. Loop 1 on List of Account records
          // 6. Call new "prepareEmailsForAccount()" method, which prepares and returns list of Messaging.SingleEmailMessage objects
          // 7. Add returned Messaging.SingleEmailMessage objects to list from point 4
          // 8. End of loop 1
          // 9. Call "Messaging.sendEmail()" method with list from point 4
          // 10. Update on List of Account records



          With this you will avoid SOQLs and calling Messaging.sendEmail() method in loops.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 17 '18 at 21:19









          Nobi992Nobi992

          564




          564












          • Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

            – user3685949
            Jan 30 at 1:47


















          • Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

            – user3685949
            Jan 30 at 1:47

















          Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

          – user3685949
          Jan 30 at 1:47






          Hi Nobi992, so in your directions are you saying that the sendpdfgeneeationpdf method needs to be bulkified? Or should the sendpdfgeneration be eliminated and everything be placed in the resendemails method? The resend email method calls the sendpdfgeneration method, so I’m a little confused as to which method I’m bulkfying. Also point 6,7,9 are a little unclear to me. Do you have any sample code by any chance?Thank you so much for your help

          – user3685949
          Jan 30 at 1:47




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326551%2fsalesforce-apex-method-not-bulkified%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党