+ Start a Discussion
phantom1982phantom1982 

Open Activity Count on Accounts

Hi

 

I have a custom field which is Open Activity Count.

 

I want to have the count of all the tasks that are open on the account to appear on the field.

 

For example if I create an account , automatically 3 tasks get created. I want the number 3 to appear automatically on the Open Activity Count field.

 

If I create another account record type, then 4 tasks may get created automatically.

 

If I manually create a task from the Open Activities related list, then also the number should appear in the Open Activity Count field.

 

I want the count of all the tasks in the open activities related list which have been created automatically(i.e by a workflow rule, trigger etc) or manually(i.e. by creating it on the open activities related list ) to appear in the Open Activity Count field.

 

If I close the task then the count should get deducted.

 

Can anyone please help me with this.

 

Thanks in advance

Alok_NagarroAlok_Nagarro

Hi,

 

You need to write a trigger on Task (event would be : after insert, after update). Whenever you create a task just incease the count of account's field and update account and every time when you update a task (i.e close the task) just check the status of task if it is closed then account's field decreases by 1 and update account.

phantom1982phantom1982

The issue is I have tried this trigger from the boards which gives the count of the activities which are created manually.

 

But it is not giving the count of the tasks which have been created automatically  by the workflows attached to the accounts.

 

I have tried this one

 

trigger OpenActivityEventCount on Event (after insert, after update, before delete) {
 // system.debug('Inside the openactivityEvent trigger'+trigger.new[0]);
 if(!trigger.isdelete)
 {
  if(trigger.new[0].AccountId!=null)
  {
    Account ac=[select id, No_of_Open_Activities__c from Account where id=:trigger.new[0].AccountId];
    if(ac!=null)
     {
     if(ac.No_of_Open_Activities__c==null)
     {
      ac.No_of_Open_Activities__c=0; 
      }
     if(trigger.isinsert) {
     ac.No_of_Open_Activities__c +=1;
      }

     if(trigger.isupdate) {
     ac.No_of_Open_Activities__c -= 0.5;
      }
       update ac;
        }
    }
    }else
    {
   if(trigger.Old[0].AccountId!=null)
   {
 
    Account ac=[select id, No_of_Open_Activities__c from Account where id=:trigger.Old[0].AccountId];
    if(ac!=null)
     {
     if(trigger.isdelete){
      ac.No_of_Open_Activities__c -= 0.5;
      }
       update ac;
        }
    }  
    }

}
Navatar_DbSupNavatar_DbSup

Hi,

 

You have to write a trigger on Account as Well as on Task and event. Your trigger on Account should be like this:

Trigger  AutoActivityCreation on Account(after insert)

{

List<task> tsk=new list<task>();

Task t=new task(OwnerId = Trigger.new[0].ownerid,Subject = 'Call',ActivityDate = system.today(),WhatId = Trigger.new[0].id,Status = 'Not Started');   

tsk.add(t);

Task t2= new task(OwnerId = Trigger.new[0].ownerid,Subject = 'Call',ActivityDate = system.today(),WhatId = Trigger.new[0].id,Status = 'Not Started');   

tsk.add(t2);

Task t3= new task(OwnerId = Trigger.new[0].ownerid,Subject = 'Call',ActivityDate = system.today(),WhatId = Trigger.new[0].id,Status = 'Not Started');   

 tsk.add(t3);

insert tsk;

account ac=[select id,ActivityCount__c from account where id=:Trigger.new[0].id];

ac.ActivityCount__c =3;

update ac;

}

Now you have to write a trigger for Task and event which will increase the ActivityCount__c by 1 if new one is created and similarly will update.

 

 

Trigger OpenActivity on Task(after insert,after update)

{

               

                Account ac=[select id,numberactivity__c from account where id=:trigger.new[0].whatid];

                If(trigger.new[0].status!='Completed')

                {

                                if(Ac.numberactivity__c == null)

                                                Ac.numberactivity__c =1;

                                else

                                Ac.numberactivity__c +=1;

                                update ac;

                }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

phantom1982phantom1982

Dear S Jain

 

Thanks a lot for your reply.

 

The issue is the trigger you have given on account is to create automatic tasks.

 

What I need help on is I want to get the count of the tasks which have been created automatically.

 

For example, I create a new buyer account in the CRM. Say the company deals with properties in England. If the buyer is residing in France, we fill the Country of Residence field as France. Then 2 automatic tasks gets created by the workflow as soon as we create the account. Some times it may be 3 or 4 tasks depending on the criteria we choose in the account creation.

 

The issue is I have this trigger from the boards which you have given(Thanks to you for this) and I have used it to get the count of the open activities on the account which have been created manually.

 

trigger OpenActivityCount on Task(after insert,after update, before delete)
{
if(trigger.new[0].AccountId!=null)
{
Account ac=[select id, No_of_Open_Activities__c from Account where id=:trigger.new[0].AccountId];
if(ac!=null)
{
if(ac.No_of_Open_Activities__c == null)
ac.No_of_Open_Activities__c =0;

if(trigger.isinsert) {
if(trigger.new[0].status!='Completed') {
ac.No_of_Open_Activities__c +=1;
}
}


if(trigger.isupdate )
{
if(trigger.new[0].status == 'Completed')
ac.No_of_Open_Activities__c -= 1;
if(trigger.new[0].status != 'Completed')
ac.No_of_Open_Activities__c += 1;

}

if(trigger.isdelete && trigger.old[0].status !='Completed'){
ac.No_of_Open_Activities__c -= 1;
}
update ac;
}
 }

 }

 

The issue with this trigger is, when we create a task on the open activities related list, it is updating the field by 1 and when we close it it is decreasing by 1 which is absolutely great.

 

But when creating the account when 2 or 3 tasks are being created automatically, this trigger is firing just once and giving the count as 1 and when we close the 2 tasks it is going to -1 as we are closing the tasks manually twice and so the trigger is updating twice.

 

I want a solution to overcome this issue please. So when 3 tasks are getting created automatically, I want the count to be 3 in the Open Activities Count field.

 

I hope I am clear on this.

 

Can you please help me with this issue.

 

Thanks and Kind Regards