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
Muni.sfdcMuni.sfdc 

Account list view

I need to get the Accounts where there is no task is created in last 15 business days.I need to show those accounts on list view.

 
Prem Anandh 1Prem Anandh 1
Hi Mahi, 

Following are the steps to find the last created date of Task on Account. 

Steps
  • Create a check box field on Account
  • Create a batch for Task and run the scheduler on daily basis. 
  • Through batch you can check the Last Created Date ofTask and if it's meet your criteria, update the checkbox to true. 
  • In list view check the criteria on check box is true, show those accounts.

Thanks,
Prem Anandh 
Mahesh DMahesh D
Hi Mahi,

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;
    }    
}


Please do let me know if it helps you.

Regards,
Mahesh
Muni.sfdcMuni.sfdc
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');
    }
   
    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)
    {
    }
}
Mahesh DMahesh D
Hi Mahi,

Did you try with the solution which I provided above. I already tested the solution and it is working fine.

Regards,
Mahesh
Muni.sfdcMuni.sfdc
Hi 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
Mahesh DMahesh D
Hi Mahi,

As we already got the Last Activity Date, you can create the List view.

Regards,
Mahesh
Muni.sfdcMuni.sfdc
HI all.

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
 
Mahesh DMahesh D
Hi 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