We can achieve this by maintaining a field on the Account like 'Last Activity Date' and write a Trigger on Task object to populate it so that always Account will have a field with latest task created date.
Below is the Code:
Here I considered:
(1) Bulkify the trigger. (2) Naming Convention. (3) Considered all possible scenarios. (4) Also tested the code in DE environment and it looks good.
//
// Trigger on Task to handle before insert event.
//
trigger TaskTrigger on Task (before insert) {
Set<Id> accIdSet = new set<Id>();
for(Task t: trigger.new) {
// To verify the created/updated Task is related to Account.
If(t.whatId != null && t.whatId.getsObjectType() == Account.sObjectType) {
accIdSet.add(t.whatId);
}
}
// Check if the Account Set is empty or not.
if(!accIdSet.isEmpty()) {
// Query the Accounts based on matching criterias.
List<Account> accList = [Select Id, Last_Activity_Date__c From Account where Id IN : accIdSet];
for (Account acc : accList){
acc.Last_Activity_Date__c = Date.today();
}
//Update the Accounts.
update accList;
}
}
Hi i have creted the batch.but not able to achive the scenario.Please help me.
global class Test_Batch implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext bc) { list<case> c=[select id, Last_ten_buss_days__c from Account]; system.debug('acc.....'+c); // String soql = 'SELECT whatid from task where createdDate <:c'; List<task> tsk = [SELECT id, whatid from task where createdDate <= LAST_N_DAYS :3 and whatid in :c ]; system.debug('task .....'+tsk );
return Database.getQueryLocator('select id, Check__c from Account where id in : tsk'); }
Please mark this as a solved by selecting the best answer and open a new discussion with your question on Scheduler so that it will be easy in the future for others to look for the answer.
Following are the steps to find the last created date of Task on Account.
Steps
Thanks,
Prem Anandh
We can achieve this by maintaining a field on the Account like 'Last Activity Date' and write a Trigger on Task object to populate it so that always Account will have a field with latest task created date.
Below is the Code:
Here I considered:
(1) Bulkify the trigger.
(2) Naming Convention.
(3) Considered all possible scenarios.
(4) Also tested the code in DE environment and it looks good.
Please do let me know if it helps you.
Regards,
Mahesh
global class Test_Batch implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext bc)
{
list<case> c=[select id, Last_ten_buss_days__c from Account];
system.debug('acc.....'+c);
// String soql = 'SELECT whatid from task where createdDate <:c';
List<task> tsk = [SELECT id, whatid from task where createdDate <= LAST_N_DAYS :3 and whatid in :c ];
system.debug('task .....'+tsk );
return Database.getQueryLocator('select id, Check__c from Account where id in : tsk');
}
global void execute(Database.BatchableContext bc, List<Account> recs)
{
List<Account> accList =new List<Account>();
for(Account cs : recs){
cs.Check__c = true;
accList.add(cs);
}
update accList;
}
global void finish(Database.BatchableContext bc)
{
}
}
Did you try with the solution which I provided above. I already tested the solution and it is working fine.
Regards,
Mahesh
Thnaks for ut help.
It is Working fine to me and How we can subtract business days from the Today date(Formula).
Thanks,
Mahi
As we already got the Last Activity Date, you can create the List view.
Regards,
Mahesh
I have a batch class called "Mybatchcls"(batch apex class) .I want to schedule the class everyday 12 am.How we can schedule using schedule apex.
Thanks,
Mahi
Please mark this as a solved by selecting the best answer and open a new discussion with your question on Scheduler so that it will be easy in the future for others to look for the answer.
Regards,
Mahesh