I am trying to delete selected records but I am getting System.LimitException: Too many DML statements: 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;
});
apex lightning limitexception
add a comment |
@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
add a comment |
@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
@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
apex lightning limitexception
edited 50 mins ago
Adrian Larson♦
106k19113239
106k19113239
asked 51 mins ago
keeplerkeepler
18418
18418
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited 38 mins ago
answered 45 mins ago
Adrian Larson♦Adrian Larson
106k19113239
106k19113239
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 45 mins ago
Manjot SinghManjot Singh
2,255522
2,255522
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 45 mins ago
David ReedDavid Reed
31.8k71746
31.8k71746
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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