0

I am trying to change the due date on task from Developer console and here is my code so far:

list<case> sc = [select Id, casenumber, (select Id, ActivityDate from openactivities) FROM case LIMIT 50]; list<task> oa = new list<task>();

for(case c:sc){
    for(task o : c.task){
        system.debug(o.ActivityDate);
    }    
}

But I am getting an error

Invalid field task on case

So how to reference tasks from cases. Can someone help me on this.

Query in the first line is working perfect in workbench to pull records. But when I am using openactivities as child object getting error "Invalid field openactivities on case"

Update: Here is the query I am trying to use after reply by sfdcfox:

SELECT Id, ActivityDate, subject, WhatId FROM Task WHERE WhatId IN (SELECT Id FROM Case) 

Requirement: I have cases with case number from 1 to 100 and I need to pull cases with case numbers from 50 to 75. How to add where clause to pull all tasks with case numbers between 50 and 75.

When I try to add WHERE clause to inner query I get an error

Unknown error parsing query

Thanks

3 Answers 3

1

I think it might be something about how you structured your WHERE clause, perhaps? Usually unknown error parsing query is about wrong field name, missing chars etc. I've tried below code and it works just fine.

   Set<String> caseNumbers = new Set<String>();
    for(Integer i = 50; i < 76; i++){
        caseNumbers.add(String.valueOf(i));
    }
    Task[] tasks = [Select Id, ActivityDate, Subject, WhatId FROM Task
                   WHERE WhatId IN (SELECT Id FROM Case WHERE CaseNumber IN :caseNumbers)];

UPDATE: Firs please see updated code below.

for(Task t: tasks){
    t.ActivityDate = Date.newInstance(2015, 7, 23);


}

update tasks;

Instead of parsing date from string, it is a good idea to use newInstance method of Date class. For more info; https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Also as you can see update is happening outside of the for loop. You should always check this. Because of Salesforce's limits it is best to not use a SOQL query or a DML statement inside a for loop. (It is also better approach.)

2
  • Hi Tugce, Can you please look into below code and suggest improvement ? Thanks
    – sf_user
    Commented Jul 23, 2015 at 2:13
  • 1
    I've updated my answer. Hope that helps.
    – tugce
    Commented Jul 23, 2015 at 5:30
2

You're looking for "OpenActivites." That's what you queried for. However, I think it'd be a lot easier to just query from the other direction:

Task[] tasks = [SELECT ActivityDate, WhatId FROM Task WHERE What.Type = 'Case' AND IsClosed = FALSE];
for(Task record: tasks) {
    // Do what you will here
}
update tasks;

As for your original code:

for(Case caseRecord: sc) {
    for(Sobject activityRecord: caseRecord.OpenActivities) {
        if(activityRecord.getSObjectType() == Task.SObjectType) {
            Task taskRecord = (Task)activityRecord;
            // Do stuff here
        }
    }
}

"OpenActivites" and "ActivityHistories" are Activity types, not Task types. They contain both tasks and events, so you have to determine which you're looking at before you can use it as a task.

You should also be able to use a sub-query on the new code I suggested:

SELECT Id, ActivityDate, WhatId FROM Task WHERE WhatId IN (SELECT Id FROM Case WHERE ...) AND IsClosed = FALSE
8
  • Thanks sfdcfox for your prompt reply. I am trying to implement your first piece of code. But I am trying to pull with in a specific range for ex: cases with casenumber between 50 and 100. Can you please suggest how to do this. Thanks
    – sf_user
    Commented Jul 22, 2015 at 22:35
  • 1
    @pmvsdt Sure. See my edit.
    – sfdcfox
    Commented Jul 22, 2015 at 22:37
  • Hi sfdcfox, sorry for this newbie question, Here is what I am trying to do. I have cases from 1 to 100 but on cases between 50 to 75, I have to update duedate on tasks. So trying to pull those records in workbench using SOQL query. Your query "SELECT Id, ActivityDate, WhatId FROM Task WHERE WhatId IN (SELECT Id FROM Case)" works perfectly but when I am trying to add WHERE clause, no records are returned. Can you please help me in building that query. Thanks
    – sf_user
    Commented Jul 22, 2015 at 22:59
  • @pmvsdt can you post a copy of the whole query including the WHERE clause and would be happy to help.
    – TC Sutton
    Commented Jul 22, 2015 at 23:26
  • 1
    The query does provide all tasks that are related to cases. I think your error may be somewhere else in the code. Please update your code snip to provide the outline for your new code.
    – TC Sutton
    Commented Jul 23, 2015 at 1:15
0

Thanks Tugce for your reply.. seems like it is working.. but can you please correct me on this.. I am not getting any error but the due date is not changing after running this script.. Can you please look in to this..

    Set<String> caseNumbers = new Set<String>();
        for(Integer i = 129162; i < 129163; i++){
            caseNumbers.add(String.valueOf(i));
        }
    Task[] tasks = [Select Id, ActivityDate, Subject, WhatId 
                    FROM Task
                    WHERE WhatId IN 
                        (SELECT Id FROM Case 
                         WHERE CaseNumber IN :caseNumbers)];
    for(task t: tasks){
        t.activitydate = date.parse('07/23/2015');
update tasks;
    }

Thanks

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .