I am trying to delete selected records but I am getting System.LimitException: Too many DML statements: 1












1















 @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 {
update 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





























    1















     @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 {
    update 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



























      1












      1








      1








       @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 {
      update 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 {
      update 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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 50 mins ago









      Adrian Larson

      106k19113239




      106k19113239










      asked 51 mins ago









      keeplerkeepler

      18418




      18418






















          3 Answers
          3






          active

          oldest

          votes


















          5














          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

































            3














            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































              3














              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%2fi-am-trying-to-delete-selected-records-but-i-am-getting-system-limitexception-t%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









                5














                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






























                  5














                  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




























                    5












                    5








                    5







                    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 38 mins ago

























                    answered 45 mins ago









                    Adrian LarsonAdrian Larson

                    106k19113239




                    106k19113239

























                        3














                        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




























                          3














                          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


























                            3












                            3








                            3







                            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 45 mins ago









                            Manjot SinghManjot Singh

                            2,255522




                            2,255522























                                3














                                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




























                                  3














                                  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


























                                    3












                                    3








                                    3







                                    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 45 mins ago









                                    David ReedDavid Reed

                                    31.8k71746




                                    31.8k71746






























                                        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%2fi-am-trying-to-delete-selected-records-but-i-am-getting-system-limitexception-t%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

                                        Magento 2 controller redirect on button click in phtml file

                                        Polycentropodidae