2

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.

1
  • 1
    It's not technically part of the problem, but your deleteObsoleteTasks method has a very misleading name - getObsoleteTasksForDeletion would be more appropriate.
    – metadaddy
    Sep 4 '12 at 18:05
6

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});
5
  • Thanks Jon, that worked and successfully deployed the code. However now when I click the custom button I get the following error "A problem with the OnClick JavaScript for this button or link was encountered: {faultcode:'soapenv:Client', faultstring:'The content of elements must consist of well-formed character data or markup.', }" Sep 4 '12 at 16:23
  • I will update my answer now but the 3rd parameter of sforce.apex.execute needs to be a json object :)
    – Jon Hazan
    Sep 4 '12 at 16:47
  • Thanks again Jon, almost there I think but now it doesn't like the delete function: 'sforce.connection.delete is not a function'. Any ideas? Sep 4 '12 at 17:02
  • 3
    The call is actually deleteIds, since delete is a JavaScript keyword. You might also have to massage your list a little, since deleteIds takes an array of record IDs, and you're passing it a list of Task objects.
    – metadaddy
    Sep 4 '12 at 18:11
  • Might it be worth you doing the delete within the Apex webservice to save passing the Ids back to JS?
    – Jon Hazan
    Sep 5 '12 at 8:41
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.