I'm trying to create a custom button which deletes a list of Tasks, depending on certain criteria.
Here's my custom button code:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var Acc = new sforce.SObject("Account");
Acc.Id = "{!Account.Id}";
Acc.Name = "{!Account.Name}";
Acc.Type = "{!Account.Type}";
var TaskList = sforce.apex.execute("AccountManagementTaskController","deleteObsoleteTasks",Acc);
sforce.connection.delete(TaskList);
and the method it's calling:
WebService static List<Task> deleteObsoleteTasks (Account Acc ) {
List<Task> obsoleteTasks = [
SELECT Id, Type, Status, WhatId
FROM Task
WHERE WhatId = Acc.Id
AND Type = 'Account Managment'
AND Status = 'Not Applicable'
];
return obsoleteTasks;
}
It doesn't seem to like this part of the SOQL query:
WHERE WhatId = Acc.Id
Any ideas on how I can fix this?
Any help appreciated.
Attribution to: Davin Casey
Possible Suggestion/Solution #1
Add a colon (:) in front of Acc.Id to make the SOQL statement substitute in the variable
List<Task> obsoleteTasks = [
SELECT Id, Type, Status, WhatId
FROM Task
WHERE WhatId = :Acc.Id
AND Type = 'Account Managment'
AND Status = 'Not Applicable'
];
There is also an error in the JS, When calling sforce.apex.execute
the 3rd parameter needs to be a json object:
var TaskList = sforce.apex.execute("AccountManagementTaskController","deleteObsoleteTasks", {"Acc": Acc});
Attribution to: Jon Hazan
Possible Suggestion/Solution #2
So I decided to delete the tasks in the apex class as suggested - seems to be working perfectly now.
apex class:
WebService static String deleteObsoleteTasks (Account Acc, String command) {
List<Task> obsoleteTasks = [
SELECT Id, Type, Status, WhatId
FROM Task
WHERE WhatId = :Acc.Id
AND Type = 'Account Management'
AND Status = 'Not Applicable'
];
if( obsoleteTasks.size() == 0 ) {
command = 'There are no relevant tasks related to this account so no action will be taken.';
delete obsoleteTasks;
} else if (obsoleteTasks.size() ==1 ) {
command = '1 relevent task will now be deleted.\n\nThe page will refresh in 5 seconds.';
delete obsoleteTasks;
} else if (obsoleteTasks.size() > 1 ) {
command = '';
command += obsoleteTasks.size();
command += ' relevant tasks will now be deleted.\n\nThe page will refresh in 5 seconds.';
delete obsoleteTasks;
}
return command;
}
and the JS button:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var Acc = new sforce.SObject("Account");
Acc.Id = "{!Account.Id}";
Acc.Name = "{!Account.Name}";
Acc.Type = "{!Account.Type}";
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// var TaskList = sforce.apex.execute("AccountManagementTaskController","deleteObsoleteTasks", {"Acc": Acc});
var r=confirm("This will remove all 'Not Applicable' Account Management tasks from the Activity History list and they will be permanently deleted - this action cannot be undone.");
if (r==true) {
command = sforce.apex.execute("AccountManagementTaskController","deleteObsoleteTasks", {"Acc": Acc});
alert(command);
timedRefresh(5000);
} else {
command = 'No action taken';
alert(command);
}
Thanks for all the help!
Attribution to: Davin Casey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1133