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
aaronbedwell.ax1665aaronbedwell.ax1665 

Need some help for activity count trigger

I would like to tweak the apex class in the link below to only count activities with a type of "Call". How can I do that?

 

Link: http://www.radialweb.com/2010/08/summarizing_salesforce_fields_with_triggers/#comments

Best Answer chosen by Admin (Salesforce Developers) 
Kevin SwiggumKevin Swiggum

Referencing the article...

 

Look for the line that says:

 

List<Opportunity> opps = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];

 

...and change it to:

 

 

List<Opportunity> opps = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks WHERE Type='Call'), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];

 

 

That will filter the tasks so that it only includes those that are calls, and ignores the rest. 

 

To make it work, you'll also need to modify the test method so that it matches the new criteria.

 

Look for the following code in testCountTask:

//Insert our first task
Task t = new Task(subject='Test Activity', whatId = opp.id);
insert t;

 and change it to:

//Insert our first task
Task t = new Task(subject='Test Activity', whatId = opp.id, Type='Call');
insert t;

Note we're updating the test code so that its inserting a Call task...otherwise the unit test would fail since the non-Call task would not get counted

 

Hope that helps

--Kevin

 

All Answers

Kevin SwiggumKevin Swiggum

Referencing the article...

 

Look for the line that says:

 

List<Opportunity> opps = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];

 

...and change it to:

 

 

List<Opportunity> opps = [SELECT ID, activity_count__c, (SELECT ID FROM Tasks WHERE Type='Call'), (SELECT ID FROM Events) FROM Opportunity WHERE ID IN :oppIds];

 

 

That will filter the tasks so that it only includes those that are calls, and ignores the rest. 

 

To make it work, you'll also need to modify the test method so that it matches the new criteria.

 

Look for the following code in testCountTask:

//Insert our first task
Task t = new Task(subject='Test Activity', whatId = opp.id);
insert t;

 and change it to:

//Insert our first task
Task t = new Task(subject='Test Activity', whatId = opp.id, Type='Call');
insert t;

Note we're updating the test code so that its inserting a Call task...otherwise the unit test would fail since the non-Call task would not get counted

 

Hope that helps

--Kevin

 

This was selected as the best answer
aaronbedwell.ax1665aaronbedwell.ax1665

Excellent! Thank you very much!

aaronbedwell.ax1665aaronbedwell.ax1665

I have a trigger that already exists in production but has 0% code coverage. Can you tell me how I can build an apex class to create a higher code coverage? Thanks.

 

trigger AvoidDuplicateUsageEntry on Question__c (before insert) 
{
    for (Question__c question: Trigger.new)
    {
        if (question.Override_Warning__c == false)
        {
            try
            {
              Question__c[] q = [SELECT q.CreatedById, q.CreatedDate, q.Question__c from Question__c q where q.Response__c = :question.Response__c and q.RFP__c = :question.RFP__c ORDER BY CreatedDate desc];
              if (q.size() > 0)
              {
                  User u = [SELECT u.Name from User u where id = :q[0].CreatedById];
                  String questionStr = String.escapeSingleQuotes(q[0].Question__c);
                  questionStr = questionStr.replace('\"', '\\\"');
                  String userStr = String.escapeSingleQuotes(u.Name);
                  userStr = userStr.replace('\"', '\\\"');
                  String dateStr = q[0].CreatedDate.format('MM/dd/yyyy hh:mm a');
                  String errorJSON = 'var errorJSON = {timesUsed: ' + q.size() + ', question: \"' + questionStr + '\", user: \"' + userStr + '\", time: \"' + dateStr + '\"};';  
                  question.Response__c.addError(errorJSON);
              } // endif
            }
            catch (QueryException e)
            {
                // This is actually the non-error case.  The Question should not 
                // already exist.  Do nothing.
            }
        } // endif
    } // endfor
}
aaronbedwell.ax1665aaronbedwell.ax1665

Nevermind, I figured it out. Thanks.

aaronbedwell.ax1665aaronbedwell.ax1665

Kevin,

 

Can I use the same class/trigger to count the number of campaigns on a lead/contact/opportunity? I know my way around some code but I am not a developer so is the change "easy"? If so, how would I make this change? Thanks.

 

-Aaron