Storable Action - System.LimitException: Too many DML statements: 1












3















 @AuraEnabled(cacheable=true)
public static String deleteContacts(List<String> contactIds) {
List<Contact> returnList = new List<Contact> ();
String query = ' SELECT Id,Name, LastName,Department,MobilePhone, Email FROM Contact WHERE id IN : contactIds ';
for (Contact thisContact: Database.Query(query)) {
returnList.add(thisContact);
}
try {
delete returnList;
return'deleted successfully';
}
catch(Exception ex){
return 'Problem occoured';
}

}

JS Method
deleteSelected(){
deleteContacts({
contactIds :this.selectedIds
})
.then(result => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: result,
variant: 'success',
}),
);
return refreshApex(this.getContactList, { searchKeyWord: '$searchKeyWord' });
})
.catch(error => {
this.error = error;
});









share|improve this question





























    3















     @AuraEnabled(cacheable=true)
    public static String deleteContacts(List<String> contactIds) {
    List<Contact> returnList = new List<Contact> ();
    String query = ' SELECT Id,Name, LastName,Department,MobilePhone, Email FROM Contact WHERE id IN : contactIds ';
    for (Contact thisContact: Database.Query(query)) {
    returnList.add(thisContact);
    }
    try {
    delete returnList;
    return'deleted successfully';
    }
    catch(Exception ex){
    return 'Problem occoured';
    }

    }

    JS Method
    deleteSelected(){
    deleteContacts({
    contactIds :this.selectedIds
    })
    .then(result => {
    this.dispatchEvent(
    new ShowToastEvent({
    title: 'Success',
    message: result,
    variant: 'success',
    }),
    );
    return refreshApex(this.getContactList, { searchKeyWord: '$searchKeyWord' });
    })
    .catch(error => {
    this.error = error;
    });









    share|improve this question



























      3












      3








      3


      0






       @AuraEnabled(cacheable=true)
      public static String deleteContacts(List<String> contactIds) {
      List<Contact> returnList = new List<Contact> ();
      String query = ' SELECT Id,Name, LastName,Department,MobilePhone, Email FROM Contact WHERE id IN : contactIds ';
      for (Contact thisContact: Database.Query(query)) {
      returnList.add(thisContact);
      }
      try {
      delete returnList;
      return'deleted successfully';
      }
      catch(Exception ex){
      return 'Problem occoured';
      }

      }

      JS Method
      deleteSelected(){
      deleteContacts({
      contactIds :this.selectedIds
      })
      .then(result => {
      this.dispatchEvent(
      new ShowToastEvent({
      title: 'Success',
      message: result,
      variant: 'success',
      }),
      );
      return refreshApex(this.getContactList, { searchKeyWord: '$searchKeyWord' });
      })
      .catch(error => {
      this.error = error;
      });









      share|improve this question
















       @AuraEnabled(cacheable=true)
      public static String deleteContacts(List<String> contactIds) {
      List<Contact> returnList = new List<Contact> ();
      String query = ' SELECT Id,Name, LastName,Department,MobilePhone, Email FROM Contact WHERE id IN : contactIds ';
      for (Contact thisContact: Database.Query(query)) {
      returnList.add(thisContact);
      }
      try {
      delete returnList;
      return'deleted successfully';
      }
      catch(Exception ex){
      return 'Problem occoured';
      }

      }

      JS Method
      deleteSelected(){
      deleteContacts({
      contactIds :this.selectedIds
      })
      .then(result => {
      this.dispatchEvent(
      new ShowToastEvent({
      title: 'Success',
      message: result,
      variant: 'success',
      }),
      );
      return refreshApex(this.getContactList, { searchKeyWord: '$searchKeyWord' });
      })
      .catch(error => {
      this.error = error;
      });






      apex lightning limitexception storable-action






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 17 at 16:14









      Adrian Larson

      107k19113241




      107k19113241










      asked Jan 16 at 18:15









      keeplerkeepler

      20128




      20128






















          3 Answers
          3






          active

          oldest

          votes


















          8














          Simply remove the (cacheable=true) parameter from your @AuraEnabled annotation. Caching a delete operation does not make any sense, and is not allowed.





          If you read Lightning Components Best Practices: Caching Data with Storable Actions, you will note this section:




          The general guideline is to cache (mark as storable) any action that is idempotent and non-mutating.



          An idempotent action is an action that produces the same result when called multiple times. For example:





          • getPage(1) is idempotent and should be cached


          • getNextPage() is not idempotent and should not be cached


          A non-mutating action is an action that doesn’t modify data. Never cache an action that can create, update, or delete data. For example:





          • updateAccount(sObject) is mutating and not idempotent and should not be cached




          Your method violates the non-mutating constraint. It looks like this constraint is a must, rather than a should.






          share|improve this answer

































            6














            Reason for that is your




            cacheable=true




            annotation. When you are doing caching you cannot mutate any data. You can only get data. So thats why no DML operation is allowed i.e Total DML operation allowed are 0.



            As given in this doc.



            https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_auraenabled_annotation.htm




            To improve runtime performance, set @AuraEnabled(cacheable=true) to
            cache the method results on the client. To set cacheable=true, a
            method must only get data, it can’t mutate data.







            share|improve this answer































              5














              You're performing DML in a context that is not supposed to mutate data - a cacheable @AuraEnabled method:




              To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data, it can’t mutate data.




              This code doesn't actually make sense, though. Why do you have a method called deleteContacts() that doesn't delete anything? The update DML it performs does nothing save to ping the audit fields, so it's not clear whether the correct approach is to remove the annotation, refactor the DML, or something else.






              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',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                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%2fsalesforce.stackexchange.com%2fquestions%2f246851%2fstorable-action-system-limitexception-too-many-dml-statements-1%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                8














                Simply remove the (cacheable=true) parameter from your @AuraEnabled annotation. Caching a delete operation does not make any sense, and is not allowed.





                If you read Lightning Components Best Practices: Caching Data with Storable Actions, you will note this section:




                The general guideline is to cache (mark as storable) any action that is idempotent and non-mutating.



                An idempotent action is an action that produces the same result when called multiple times. For example:





                • getPage(1) is idempotent and should be cached


                • getNextPage() is not idempotent and should not be cached


                A non-mutating action is an action that doesn’t modify data. Never cache an action that can create, update, or delete data. For example:





                • updateAccount(sObject) is mutating and not idempotent and should not be cached




                Your method violates the non-mutating constraint. It looks like this constraint is a must, rather than a should.






                share|improve this answer






























                  8














                  Simply remove the (cacheable=true) parameter from your @AuraEnabled annotation. Caching a delete operation does not make any sense, and is not allowed.





                  If you read Lightning Components Best Practices: Caching Data with Storable Actions, you will note this section:




                  The general guideline is to cache (mark as storable) any action that is idempotent and non-mutating.



                  An idempotent action is an action that produces the same result when called multiple times. For example:





                  • getPage(1) is idempotent and should be cached


                  • getNextPage() is not idempotent and should not be cached


                  A non-mutating action is an action that doesn’t modify data. Never cache an action that can create, update, or delete data. For example:





                  • updateAccount(sObject) is mutating and not idempotent and should not be cached




                  Your method violates the non-mutating constraint. It looks like this constraint is a must, rather than a should.






                  share|improve this answer




























                    8












                    8








                    8







                    Simply remove the (cacheable=true) parameter from your @AuraEnabled annotation. Caching a delete operation does not make any sense, and is not allowed.





                    If you read Lightning Components Best Practices: Caching Data with Storable Actions, you will note this section:




                    The general guideline is to cache (mark as storable) any action that is idempotent and non-mutating.



                    An idempotent action is an action that produces the same result when called multiple times. For example:





                    • getPage(1) is idempotent and should be cached


                    • getNextPage() is not idempotent and should not be cached


                    A non-mutating action is an action that doesn’t modify data. Never cache an action that can create, update, or delete data. For example:





                    • updateAccount(sObject) is mutating and not idempotent and should not be cached




                    Your method violates the non-mutating constraint. It looks like this constraint is a must, rather than a should.






                    share|improve this answer















                    Simply remove the (cacheable=true) parameter from your @AuraEnabled annotation. Caching a delete operation does not make any sense, and is not allowed.





                    If you read Lightning Components Best Practices: Caching Data with Storable Actions, you will note this section:




                    The general guideline is to cache (mark as storable) any action that is idempotent and non-mutating.



                    An idempotent action is an action that produces the same result when called multiple times. For example:





                    • getPage(1) is idempotent and should be cached


                    • getNextPage() is not idempotent and should not be cached


                    A non-mutating action is an action that doesn’t modify data. Never cache an action that can create, update, or delete data. For example:





                    • updateAccount(sObject) is mutating and not idempotent and should not be cached




                    Your method violates the non-mutating constraint. It looks like this constraint is a must, rather than a should.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 16 at 18:27

























                    answered Jan 16 at 18:21









                    Adrian LarsonAdrian Larson

                    107k19113241




                    107k19113241

























                        6














                        Reason for that is your




                        cacheable=true




                        annotation. When you are doing caching you cannot mutate any data. You can only get data. So thats why no DML operation is allowed i.e Total DML operation allowed are 0.



                        As given in this doc.



                        https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_auraenabled_annotation.htm




                        To improve runtime performance, set @AuraEnabled(cacheable=true) to
                        cache the method results on the client. To set cacheable=true, a
                        method must only get data, it can’t mutate data.







                        share|improve this answer




























                          6














                          Reason for that is your




                          cacheable=true




                          annotation. When you are doing caching you cannot mutate any data. You can only get data. So thats why no DML operation is allowed i.e Total DML operation allowed are 0.



                          As given in this doc.



                          https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_auraenabled_annotation.htm




                          To improve runtime performance, set @AuraEnabled(cacheable=true) to
                          cache the method results on the client. To set cacheable=true, a
                          method must only get data, it can’t mutate data.







                          share|improve this answer


























                            6












                            6








                            6







                            Reason for that is your




                            cacheable=true




                            annotation. When you are doing caching you cannot mutate any data. You can only get data. So thats why no DML operation is allowed i.e Total DML operation allowed are 0.



                            As given in this doc.



                            https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_auraenabled_annotation.htm




                            To improve runtime performance, set @AuraEnabled(cacheable=true) to
                            cache the method results on the client. To set cacheable=true, a
                            method must only get data, it can’t mutate data.







                            share|improve this answer













                            Reason for that is your




                            cacheable=true




                            annotation. When you are doing caching you cannot mutate any data. You can only get data. So thats why no DML operation is allowed i.e Total DML operation allowed are 0.



                            As given in this doc.



                            https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_auraenabled_annotation.htm




                            To improve runtime performance, set @AuraEnabled(cacheable=true) to
                            cache the method results on the client. To set cacheable=true, a
                            method must only get data, it can’t mutate data.








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 16 at 18:21









                            Manjot SinghManjot Singh

                            2,310622




                            2,310622























                                5














                                You're performing DML in a context that is not supposed to mutate data - a cacheable @AuraEnabled method:




                                To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data, it can’t mutate data.




                                This code doesn't actually make sense, though. Why do you have a method called deleteContacts() that doesn't delete anything? The update DML it performs does nothing save to ping the audit fields, so it's not clear whether the correct approach is to remove the annotation, refactor the DML, or something else.






                                share|improve this answer




























                                  5














                                  You're performing DML in a context that is not supposed to mutate data - a cacheable @AuraEnabled method:




                                  To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data, it can’t mutate data.




                                  This code doesn't actually make sense, though. Why do you have a method called deleteContacts() that doesn't delete anything? The update DML it performs does nothing save to ping the audit fields, so it's not clear whether the correct approach is to remove the annotation, refactor the DML, or something else.






                                  share|improve this answer


























                                    5












                                    5








                                    5







                                    You're performing DML in a context that is not supposed to mutate data - a cacheable @AuraEnabled method:




                                    To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data, it can’t mutate data.




                                    This code doesn't actually make sense, though. Why do you have a method called deleteContacts() that doesn't delete anything? The update DML it performs does nothing save to ping the audit fields, so it's not clear whether the correct approach is to remove the annotation, refactor the DML, or something else.






                                    share|improve this answer













                                    You're performing DML in a context that is not supposed to mutate data - a cacheable @AuraEnabled method:




                                    To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data, it can’t mutate data.




                                    This code doesn't actually make sense, though. Why do you have a method called deleteContacts() that doesn't delete anything? The update DML it performs does nothing save to ping the audit fields, so it's not clear whether the correct approach is to remove the annotation, refactor the DML, or something else.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 16 at 18:21









                                    David ReedDavid Reed

                                    32.9k72052




                                    32.9k72052






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Salesforce Stack Exchange!


                                        • 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%2fsalesforce.stackexchange.com%2fquestions%2f246851%2fstorable-action-system-limitexception-too-many-dml-statements-1%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

                                        How to change which sound is reproduced for terminal bell?

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents