Create Case thread Id using Apex (SOQL limit-proof)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












There are many posts about creating Case threadIds with Apex, notably Case.Thread_Id, is Thread_Id a field on Case object?



As the posters have noted, the SFDC rules of the threadId are subject to change over time and the only reliable way that I've seen is to do this:



// To be replaced with a real case Id.
Id caseId = '500c0000008kJL5';

List<Messaging.RenderEmailTemplateBodyResult> renderResults =
Messaging.renderEmailTemplate(null, caseId, new List<String>'!Case.Thread_Id');

System.debug('this is the Case Thread Id: ' + renderResults[0].getMergedBody());


HOWEVER ...
The method call Messaging.renderEmailTemplate burns a SOQL (see doc)



So you can't prepopulate your Cases in an afterInsert or beforeUpdate Trigger because a transaction can be up to 2,000 (Platform Events), up to 10,000 (direct Apex DML) or more, typically, 200 rows. All of these are > 100 SOQL limit.




So, before I go to the trouble of doing this in a chained set of async
tasks; is there more recent thinking on this subject of creating ThreadIds?











share|improve this question



























    up vote
    5
    down vote

    favorite












    There are many posts about creating Case threadIds with Apex, notably Case.Thread_Id, is Thread_Id a field on Case object?



    As the posters have noted, the SFDC rules of the threadId are subject to change over time and the only reliable way that I've seen is to do this:



    // To be replaced with a real case Id.
    Id caseId = '500c0000008kJL5';

    List<Messaging.RenderEmailTemplateBodyResult> renderResults =
    Messaging.renderEmailTemplate(null, caseId, new List<String>'!Case.Thread_Id');

    System.debug('this is the Case Thread Id: ' + renderResults[0].getMergedBody());


    HOWEVER ...
    The method call Messaging.renderEmailTemplate burns a SOQL (see doc)



    So you can't prepopulate your Cases in an afterInsert or beforeUpdate Trigger because a transaction can be up to 2,000 (Platform Events), up to 10,000 (direct Apex DML) or more, typically, 200 rows. All of these are > 100 SOQL limit.




    So, before I go to the trouble of doing this in a chained set of async
    tasks; is there more recent thinking on this subject of creating ThreadIds?











    share|improve this question























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      There are many posts about creating Case threadIds with Apex, notably Case.Thread_Id, is Thread_Id a field on Case object?



      As the posters have noted, the SFDC rules of the threadId are subject to change over time and the only reliable way that I've seen is to do this:



      // To be replaced with a real case Id.
      Id caseId = '500c0000008kJL5';

      List<Messaging.RenderEmailTemplateBodyResult> renderResults =
      Messaging.renderEmailTemplate(null, caseId, new List<String>'!Case.Thread_Id');

      System.debug('this is the Case Thread Id: ' + renderResults[0].getMergedBody());


      HOWEVER ...
      The method call Messaging.renderEmailTemplate burns a SOQL (see doc)



      So you can't prepopulate your Cases in an afterInsert or beforeUpdate Trigger because a transaction can be up to 2,000 (Platform Events), up to 10,000 (direct Apex DML) or more, typically, 200 rows. All of these are > 100 SOQL limit.




      So, before I go to the trouble of doing this in a chained set of async
      tasks; is there more recent thinking on this subject of creating ThreadIds?











      share|improve this question













      There are many posts about creating Case threadIds with Apex, notably Case.Thread_Id, is Thread_Id a field on Case object?



      As the posters have noted, the SFDC rules of the threadId are subject to change over time and the only reliable way that I've seen is to do this:



      // To be replaced with a real case Id.
      Id caseId = '500c0000008kJL5';

      List<Messaging.RenderEmailTemplateBodyResult> renderResults =
      Messaging.renderEmailTemplate(null, caseId, new List<String>'!Case.Thread_Id');

      System.debug('this is the Case Thread Id: ' + renderResults[0].getMergedBody());


      HOWEVER ...
      The method call Messaging.renderEmailTemplate burns a SOQL (see doc)



      So you can't prepopulate your Cases in an afterInsert or beforeUpdate Trigger because a transaction can be up to 2,000 (Platform Events), up to 10,000 (direct Apex DML) or more, typically, 200 rows. All of these are > 100 SOQL limit.




      So, before I go to the trouble of doing this in a chained set of async
      tasks; is there more recent thinking on this subject of creating ThreadIds?








      apex soql-limit-exception






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      cropredy

      34.3k439117




      34.3k439117




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          For now, if you need this, you'll have to go asynchronous. Queueable would probably be best, but that's up to your discretion. There's an idea to enable a field that could be used, and presumably that could solve the problem at a more fundamental level, so you might want to vote on it.






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237194%2fcreate-case-thread-id-using-apex-soql-limit-proof%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            For now, if you need this, you'll have to go asynchronous. Queueable would probably be best, but that's up to your discretion. There's an idea to enable a field that could be used, and presumably that could solve the problem at a more fundamental level, so you might want to vote on it.






            share|improve this answer
























              up vote
              2
              down vote













              For now, if you need this, you'll have to go asynchronous. Queueable would probably be best, but that's up to your discretion. There's an idea to enable a field that could be used, and presumably that could solve the problem at a more fundamental level, so you might want to vote on it.






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                For now, if you need this, you'll have to go asynchronous. Queueable would probably be best, but that's up to your discretion. There's an idea to enable a field that could be used, and presumably that could solve the problem at a more fundamental level, so you might want to vote on it.






                share|improve this answer












                For now, if you need this, you'll have to go asynchronous. Queueable would probably be best, but that's up to your discretion. There's an idea to enable a field that could be used, and presumably that could solve the problem at a more fundamental level, so you might want to vote on it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 3 hours ago









                sfdcfox

                234k10180396




                234k10180396



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f237194%2fcreate-case-thread-id-using-apex-soql-limit-proof%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke