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
Sourav PSourav P 

To create a Task counter in Opportunity

Hi ,
I want to create a Task counter in Opp, the counter should increase/count only if the subject is " Opp  follow-up" ( not for other subjects),  May i know how to do it ?
I was trying PB created on Task object, but couldnt able to put action that increase the " Opp Task Conter " field.
I am trying to write a trigger on Opp, If anyone can suggest , modify my below trigger which is not working to get a proper view, i am new to apex.
 
trigger TaskCount on Opportunity (before insert) {
if(newTaskList[0].WhatId != null && ((String)newTaskList[0].WhatId).substring(0,3)=='006')
        {
            opp = [SELECT Id,Name,OwnerId,Owner.Email,(SELECT Id FROM Tasks WHERE Status != 'Completed' and Subject='Opportunity follow-up) FROM Opportunity WHERE Id = :newTaskList[0].WhatId LIMIT 1];
        }
}



Any suggestions plz. Thnx
Best Answer chosen by Sourav P
ManojjenaManojjena
Hi Sourav,

Try with below code  it will help !!
trigger taskTrigger on Task(After insert,after delete){
  List<Opportunity> oppListToUpdate=new List<Opportunity>();
  Set<Id> oppIdSet= new Set<Id>();
  if(Trigger.isAfter){
     if(Trigger.isInsert){
         for(Task tsk : trigger.new){
            if(tsk.WhatId != null && String.valueOf(tsk.WhatId).startsWith('006') && tsk.subject=='Opportunity follow-up'){
                oppIdSet.add(tsk.WhatId);
            }
        }
     }
     if(Trigger.isDelete){
        for(Task tsk : trigger.old){
            if(tsk.WhatId != null && String.valueOf(tsk.WhatId).startsWith('006') && tsk.subject=='Opportunity follow-up'){
                oppIdSet.add(tsk.WhatId);
            }
        }
     }
  }
 
  if(!oppIdSet.isEmpty()){
    List<AggregateResult> aggRes=[SELECT count(Id)cnt,WhatId wht FROM Task WHERE WhatId IN : oppIdSet AND subject='Opportunity follow-up'  Group By WhatId];
    for(AggregateResult agg : aggRes){
       Opportunity opp =new Opportunity(id=(Id)agg.get('wht'),Task_Count__c = (Decimal)agg.get('cnt'));
        oppListToUpdate.add(opp);
    }
    if(!oppListToUpdate.isEmpty()){
       try{
           update oppListToUpdate;
       }catch(DmlException de ){
           System.debug(de.getMessage());
       }
    }
  }
}

Replace Task_Count__c with your Opportunity API field API Name .
Let me know if it helps !!
Thanks
Manoj
 

All Answers

ManojjenaManojjena
HI Sourav,

Better you can write a trigger in task with event after insert and after delete with condition task.WhatId != null && String.valueOf(task.WhatId).startsWith('006') && task.subject='Opportunity follow-up'

then collect all opportunity id and do a query and update accordingly to opportunity.

Let me know if u r facing problem .

Thanks
Manoj
Sourav PSourav P
Thanks Manoj, Let me try it.
ManojjenaManojjena
Hi Sourav,

Try with below code  it will help !!
trigger taskTrigger on Task(After insert,after delete){
  List<Opportunity> oppListToUpdate=new List<Opportunity>();
  Set<Id> oppIdSet= new Set<Id>();
  if(Trigger.isAfter){
     if(Trigger.isInsert){
         for(Task tsk : trigger.new){
            if(tsk.WhatId != null && String.valueOf(tsk.WhatId).startsWith('006') && tsk.subject=='Opportunity follow-up'){
                oppIdSet.add(tsk.WhatId);
            }
        }
     }
     if(Trigger.isDelete){
        for(Task tsk : trigger.old){
            if(tsk.WhatId != null && String.valueOf(tsk.WhatId).startsWith('006') && tsk.subject=='Opportunity follow-up'){
                oppIdSet.add(tsk.WhatId);
            }
        }
     }
  }
 
  if(!oppIdSet.isEmpty()){
    List<AggregateResult> aggRes=[SELECT count(Id)cnt,WhatId wht FROM Task WHERE WhatId IN : oppIdSet AND subject='Opportunity follow-up'  Group By WhatId];
    for(AggregateResult agg : aggRes){
       Opportunity opp =new Opportunity(id=(Id)agg.get('wht'),Task_Count__c = (Decimal)agg.get('cnt'));
        oppListToUpdate.add(opp);
    }
    if(!oppListToUpdate.isEmpty()){
       try{
           update oppListToUpdate;
       }catch(DmlException de ){
           System.debug(de.getMessage());
       }
    }
  }
}

Replace Task_Count__c with your Opportunity API field API Name .
Let me know if it helps !!
Thanks
Manoj
 
This was selected as the best answer
Sourav PSourav P
Hi Manoj, Thanks a lot, Let me try
Sourav PSourav P
Hi Manoj, thanks , Its working perfectly.
Just a small issue. I want to take this field value to that of Task object. So, I created just a formula field in Task, having the below, But i dont understand why its not working, Can you plz suggest,
Opportunity__r.Opp_Follow_Up_Task_Counter__c

 
ManojjenaManojjena
HI Saurav,

You can not create formula like this as task and opportunity is related through WhatId not Opportunity __c .
You can close this thread so that if any one else have same probelm they can use the code .

Let me know incase any
Thanks
Manoj
Sourav PSourav P
Hi Manoj, Thanks a lot.
Can you plz suggest a way to bring the field in Task page.