+ Start a Discussion
Chad MoutesChad Moutes 

Apex Trigger To Count Based on Pick list Values

I have an Apex Trigger losted below. It counts the number of tasks on an account, and it works perfectly, but i want it to only count Attempted and Completed Call, I currently have it counting only Attempted Calls because everytime i try and add Completed Call it doesnt work at all. Any Ideas?

trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:Trigger.new){
            set_Id.add(T.WhatId);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.WhatId);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Subject = 'Attempted Call') FROM Account WHERE Id IN :set_Id];

     for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Call_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Call_Activities__c = 0;
     }
        if(!acc_list.isEmpty())
            update acc_list;
        }

}
Best Answer chosen by Chad Moutes
KaranrajKaranraj
Add OR condition in your SOQL query. Try the updated code
 
trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:Trigger.new){
            set_Id.add(T.WhatId);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.WhatId);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Subject = 'Attempted Call' or Subject = 'Completed Call') FROM Account WHERE Id IN :set_Id];

     for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Call_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Call_Activities__c = 0;
     }
        if(!acc_list.isEmpty())
            update acc_list;
        }

}

 

All Answers

KaranrajKaranraj
Add OR condition in your SOQL query. Try the updated code
 
trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:Trigger.new){
            set_Id.add(T.WhatId);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.WhatId);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Subject = 'Attempted Call' or Subject = 'Completed Call') FROM Account WHERE Id IN :set_Id];

     for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Call_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Call_Activities__c = 0;
     }
        if(!acc_list.isEmpty())
            update acc_list;
        }

}

 
This was selected as the best answer
SunidharSunidhar
you are looking something like this

trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:Trigger.new){
            set_Id.add(T.WhatId);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.WhatId);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE OR(Subject = 'Attempted Call',Subject = 'Completed Call')) FROM Account WHERE Id IN :set_Id];

     for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Call_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Call_Activities__c = 0;
     }
        if(!acc_list.isEmpty())
            update acc_list;
        }

}

if it sloves pls select as best answer.
Chad MoutesChad Moutes
Thanks Karanraj that did it. I had it like the one that sunidhar had and it wasnt working.
Chad MoutesChad Moutes
Kananraj,

I know have to add a clause so that it only counts tasks with a status of completed and the subject being either Completed Call, Attempted Call, Received Call, or Left Message. Heres my code:
 
trigger SumCallActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:Trigger.new){
            set_Id.add(T.WhatId);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.WhatId);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Status = 'Completed' WHERE Subject = 'Attempted Call' or Subject = 'Completed Call' or Subject = 'Received Call' or Subject = 'Left Message') FROM Account WHERE Id IN :set_Id];

     for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Call_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Call_Activities__c = 0;
     }
        if(!acc_list.isEmpty())
            update acc_list;
        }

}
Heres the error im getting:

unexpected token: 'WHERE' at line 21 column 102

anything thoughts?
KaranrajKaranraj
I think you wrongly included one more where in your query instead of OR condition. Check the below update query
 
[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Status = 'Completed' or Subject = 'Attempted Call' or Subject = 'Completed Call' or Subject = 'Received Call' or Subject = 'Left Message') FROM Account WHERE Id IN :set_Id]

I strongly recommend you to take look into the Salesforce trailhead, which helps you to understand about apex code and best practices https://developer.salesforce.com/trailhead
Chad MoutesChad Moutes
Karanraj,

That makes it so the status couled be completed or the subject could be one of the options, i need it to be the status is completed AND the subject is one of the options. But when i tried it that way it didnt work.
 
KaranrajKaranraj
Chad - Then you can use the AND condition in your soql query
 
[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Status = 'Completed' AND Subject = 'Attempted Call' or Subject = 'Completed Call' or Subject = 'Received Call' or Subject = 'Left Message') FROM Account WHERE Id IN :set_Id]

 
Chad MoutesChad Moutes
I have already tried that, I get this error expecting a right parentheses, found 'or'
Chad MoutesChad Moutes
Alright, I fixed it.
 
[SELECT Id, Sum_Call_Activities__c, (SELECT Id FROM Tasks WHERE Status = 'Completed' AND (Subject = 'Attempted Call' or Subject = 'Completed Call' or Subject = 'Received Call' or Subject = 'Left Message')) FROM Account WHERE Id IN :set_Id]