function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
venkateshyadav1243venkateshyadav1243 

Schedular class issue urgent

Hi
i have one schedular class am facing this problem from so many days but am not able to achive.
my query is not fetching any records but when i see in debug log its updating my old records.

My requirement is if user is not completed task with in 2 days i need to escalte this task to system admin

map<Id, User> crmusernewid =new map<Id, User>([SELECT Id, Name FROM User WHERE Asst_crm_user__c=true and Profile.Name IN ('System Admin')]);

listtask=[Select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority,
t.Status, t.Subject, t.WhatId, t.WhoId from Task t where Whatid=:CRID and Status='Not Started'  and Created_date__c=today and ActivityDate=null AND OwnerID IN :ID2User.keyset()];
system.debug('List of tasks that are not closed with in 3 days '+listtask);

am updating this way

public Map<id,task> listtask2=new Map<id,task>();

if(listtask.size()>0)
{
for(task t: listtask)
{
  for(user crm:crmusernewid.values())
{
t.OwnerId=crm.id;
listtask2.put(t.id,t);
}
}
}
update listtask2.values(); //updating the tasks


Thanks
Advance for your help
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

I have modified your SQOL query.
listtask=[Select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority,
t.Status, t.Subject, t.WhatId, t.WhoId from Task t where Status!='completed' and createdDate<LAST_N_DAYS:3 and ownerId not IN crmusernewid .keyset()];
Explanation
Here I have query only those tasks that are not completed and created before 3 days.
I added one where clause also “ownerId not IN crmusernewid .keyset() “ since soql query willnot return the task that is related to admin.

If you have any issue please let me know.

Ankit Gupta
venkateshyadav1243venkateshyadav1243
Hi Ankit Gupta ,
Thanks for giving replay,
below is my class its running fine first day,but in next dat onwords its fecthing old records and again its assinging to sys admin
please look into my below code and let me know where am doing mistake. thanks advance

// **** schedular class for task escaltion

global with sharing class Task_Escalation implements Schedulable {

public List<Client_Requirement__c> clientreq=new list<Client_Requirement__c>();


public list<task> listtask=new List<task>();
public Map<id,task> listtask2=new Map<id,task>();
public set<id> CRID=new set<id>();

global void execute(SchedulableContext SC) {

try{
map<Id, User> Id2User = new map<Id, User>([SELECT Id, Name FROM User WHERE Profile.Name IN ('East','Howrah','North and Central','Rajarhat','South I','South II','Newtown')]);
system.debug('@@@@ list users IDDDDD '+Id2User);

map<Id, User> crmusernewid =new map<Id, User>([SELECT Id, Name FROM User WHERE Asst_crm_user__c=true and Profile.Name IN ('System Admin')]);
system.debug('@@@@ CRM users IDDDDD '+crmusernewid);

//--------------------------------------------------------------------------------
// list of all client requirements
clientreq=[select id,name,RecordTypeid,Ownerid  from Client_Requirement__c];
system.debug('List of client requirement'+clientreq);
for(Client_Requirement__c cr: clientreq)
{
CRID.add(cr.id);// fetching the list of all client req IDS
}
// query for fetching the tasks that are not closed with in 3 days

listtask=[Select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority,
t.Status, t.Subject, t.WhatId, t.WhoId from Task t where Whatid=:CRID and Status='Not Started'  and Created_date__c=today and ActivityDate=null AND OwnerID IN :ID2User.keyset()];
system.debug('List of tasks that are not closed with in 3 days '+listtask);

if(listtask.size()>0)
{
for(task t: listtask)
{
  for(user crm:crmusernewid.values())
{
t.OwnerId=crm.id;

listtask2.put(t.id,t);
}
}
}

update listtask2.values(); //updating the tasks
system.debug('List of tasks that are updated to user crm head '+listtask2);

}catch(Exception e){system.debug('@@@@@@@@@@@@@@'+e);}
}
}





In debug log result is showing like this.

Aggregations:0|select t.CreatedDate, t.Id, t.LastModifiedById, t.LastModifiedDate, t.OwnerId, t.Priority, t.Status, t.Subject, t.WhatId, t.WhoId from Task t where (Whatid = :tmpVar1 and Status = 'Not Started' and Created_date__c = today and ActivityDate = null and OwnerID = :tmpVar2)
OQL_EXECUTE_END|[51]|Rows:0
DEBUG|List of tasks that are not closed with in 3 days ()


Op:Update|Type:Task|Rows:3
09:00:01.967 (1967809000)|DML_END|[71]
09:00:01.968 (1968044000)|SYSTEM_METHOD_ENTRY|[72]|String.valueOf(Object)
09:00:01.968 (1968221000)|SYSTEM_METHOD_EXIT|[72]|String.valueOf(Object)
09:00:01.968 (1968238000)|SYSTEM_METHOD_ENTRY|[72]|System.debug(ANY)
09:00:01.968 (1968246000)|USER_DEBUG|[72]|DEBUG|List of tasks that are updated to user crm head {00T9000000dnfYMEAY=Task:{Status=Not Started, WhatId=a0N90000006IPEgEAO, LastModifiedById=00590000001qNM7AAM, Subject=Call, OwnerId=00590000001qNKBAA2, CreatedDate=2014-01-30 07:11:50, LastModifiedDate=2014-01-30 07:12:41, Id=00T9000000dnfYMEAY, Priority=Normal}, 00T9000000dnsL7EAI=Task:{Status=Not Started, WhatId=a0N900000090Cx6EAE, LastModifiedById=00590000001qNNUAA2, Subject=Follow Up, OwnerId=00590000001qNKBAA2, CreatedDate=2014-01-30 10:17:16, LastModifiedDate=2014-01-30 10:17:40, Id=00T9000000dnsL7EAI, Priority=Normal}, 00T9000000dnuhrEAA=Task:{Status=Not Started, WhatId=a0N900000090D1HEAU, LastModifiedById=00590000001qi8hAAA, Subject=Follow Up, OwnerId=00590000001qNKBAA2, CreatedDate=2014-01-30 10:59:14, LastModifiedDate=2014-01-30 10:59:37, Id=00T9000000dnuhrEAA, Priority=Normal}}



my query is shwoing no rows but my update is showing 2 records,this 2 records are yesterday records
please tell me where am missing.
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,
Created all the list in execute method.
Like
public list<task> listtask=new List<task>();
public Map<id,task> listtask2=new Map<id,task>();
public set<id> CRID=new set<id>();