+ Start a Discussion
venkateshyadav1243venkateshyadav1243 

how to restrict trigger fire only once when i run scheduler class

Hi
I have one trigger that updating activitydate
trigger GetUsableTaskDueDate on Task (before insert, before update)
{
Task[] checkTasks = Trigger.new;
  for(Task t : checkTasks)
  {
   t.Due_Date__c=t.ActivityDate;
   }
   }

I have scheduler class when i run my scheduler class in my query its fetching one record but when am updating its updating different records more than my query fecthing record am not able to undersatnd what is happing below is scheduler class please check any one tell me how can i do this.
I have one more doubt how many times trigger will call when am running scheduler class?



global with sharing class Task_Escalation_user_sys_created_dt_6 implements Schedulable
{

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

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

public list<task> listtask3=new List<task>();

public set<id> sysid=new set<id>();

global void execute(SchedulableContext SC) {

try{

map<Id, User> user_id_sysadm = new map<Id, User>([SELECT Id, Name FROM User WHERE users_and_crm__c=true and Profile.Name IN ('East','Howrah','North and Central','Rajarhat','South I','South II','Newtown','System Admin')]);
system.debug('@@@@ include asst crm list users IDDDDD '+user_id_sysadm);

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


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

//--------------------------------------------------------------------------------------------------

clientreq2=[select id,name,RecordTypeid,Ownerid  from Client_Requirement__c];
system.debug('List of client requirement'+clientreq2);
for(Client_Requirement__c cr: clientreq2)
{
sysid.add(cr.id);// fetching the list of all client req IDS and ownerid='00590000001qNKB'
}
system.debug('list of idssss for system admin'+sysid);

// query for fetching the task that crm head user not completed the tasks
listtask3=[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=:sysid and Status='Not Started' and Created_date_after_6_days__c=today and ActivityDate=null AND OwnerID IN :user_id_sysadm.keyset()];

system.debug('List of tasks that are not closed after 2 days '+listtask3);

if(listtask3.size()>0)
{
for(task t2: listtask3)
{
  for(user sysd1:syssernewid.values())
{

t2.OwnerId=sysd1.id;
// Assigning the system admin to the tasks that crm head user not completed

listtask4.put(t2.id,t2);
}
}
}

update listtask4.values();  // updating the tasks.
system.debug('List of tasks that are updated to user sysadmin head '+listtask4);
}catch(Exception e){system.debug('@@@@@@@@@@@@@@'+e);}
}
}


in my debug log its showing like this.

Query:

SOQL_EXECUTE_BEGIN|[36]|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_after_6_days__c = today and ActivityDate = null and OwnerID = :tmpVar2)
SOQL_EXECUTE_END|[36]|Rows:1

USER_DEBUG|[39]|DEBUG|List of tasks that are not closed after 2 days (Task:{Status=Not Started, LastModifiedById=00590000001qNM7AAM, WhatId=a0N90000008OHtoEAG, Subject=Follow Up, OwnerId=00590000001qNKQAA2, CreatedDate=2014-01-25 08:04:06, LastModifiedDate=2014-01-27 10:30:02, Id=00T9000000dHsTXEA0, Priority=Normal})
SYSTEM_METHOD_EXIT|[39]|System.debug(ANY)

In Updating
10:00:02.047 (1047157000)|CODE_UNIT_FINISHED|GetUsableTaskDueDate on Task trigger event BeforeUpdate for [00T9000000dGZUu, 00T9000000dHaq1, 00T9000000dHcUq, 00T9000000dHdkb, 00T9000000dHe1k, 00T9000000dHeD8, 00T9000000dHewB, 00T9000000dHf0w, 00T9000000dHfAW, 00T9000000dHfPO, 00T9000000dHsTX]
10:00:02.181 (1181818000)|DML_END|[56]
10:00:02.181 (1181962000)|SYSTEM_METHOD_ENTRY|[57]|String.valueOf(Object)
10:00:02.182 (1182440000)|SYSTEM_METHOD_EXIT|[57]|String.valueOf(Object)
10:00:02.182 (1182458000)|SYSTEM_METHOD_ENTRY|[57]|System.debug(ANY)
10:00:02.182 (1182466000)|USER_DEBUG|[57]|DEBUG|List of tasks that are updated to user sysadmin head {00T9000000dGZUuEAO=Task:{Status=Not Started, LastModifiedById=00590000001qi8hAAA, WhatId=a0N90000006IPHUEA4, Subject=Call, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-23 06:04:31, LastModifiedDate=2014-01-23 06:04:31, Id=00T9000000dGZUuEAO, Priority=Normal}, 00T9000000dHaq1EAC=Task:{Status=Not Started, WhatId=a0N90000006IOihEAG, LastModifiedById=00590000001qNM7AAM, Subject=Call, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 10:06:54, LastModifiedDate=2014-01-24 10:07:09, Id=00T9000000dHaq1EAC, Priority=Normal}, 00T9000000dHcUqEAK=Task:{Status=Not Started, WhatId=a0N90000008OHEYEA4, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 10:29:03, LastModifiedDate=2014-01-24 10:31:30, Id=00T9000000dHcUqEAK, Priority=Normal}, 00T9000000dHdkbEAC=Task:{Status=Not Started, WhatId=a0N90000008OHIKEA4, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 10:46:09, LastModifiedDate=2014-01-24 10:50:06, Id=00T9000000dHdkbEAC, Priority=Normal}, 00T9000000dHe1kEAC=Task:{Status=Not Started, WhatId=a0N90000008OHK1EAO, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 11:24:12, LastModifiedDate=2014-01-24 11:27:04, Id=00T9000000dHe1kEAC, Priority=Normal}, 00T9000000dHeD8EAK=Task:{Status=Not Started, WhatId=a0N90000008OHJ3EAO, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 11:03:07, LastModifiedDate=2014-01-24 11:06:01, Id=00T9000000dHeD8EAK, Priority=Normal}, 00T9000000dHewBEAS=Task:{Status=Not Started, WhatId=a0N90000008OHKkEAO, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 11:50:25, LastModifiedDate=2014-01-24 11:53:14, Id=00T9000000dHewBEAS, Priority=Normal}, 00T9000000dHf0wEAC=Task:{Status=Not Started, WhatId=a0N90000008OHL0EAO, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 11:57:34, LastModifiedDate=2014-01-24 11:58:11, Id=00T9000000dHf0wEAC, Priority=Normal}, 00T9000000dHfAWEA0=Task:{Status=Not Started, WhatId=a0N90000008OHOwEAO, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 12:24:57, LastModifiedDate=2014-01-24 12:26:39, Id=00T9000000dHfAWEA0, Priority=Normal}, 00T9000000dHfPOEA0=Task:{Status=Not Started, WhatId=a0N90000008OHOcEAO, LastModifiedById=00590000001qNM7AAM, Subject=Follow Up, OwnerId=00590000001WzWyAAK, CreatedDate=2014-01-24 12:14:06, LastModifiedDate=2014-01-24 12:14:22, Id=00T9000000dHfPOEA0, Priority=Normal}, ...}
10:00:02.182 (1182491000)|SYSTEM_METHOD_EXIT|[57]|System.debug(ANY)
Vinita_SFDCVinita_SFDC
Hello,

Please refer following help document for achieveing this functionality:

http://developer.force.com/cookbook/recipe/controlling-recursive-triggers