You need to sign in to do that
Don't have an account?
Apex Trigger Count Help.
I have an Apex Trigger that counts the number of Projects that have been created on an Account, and i realized today that if you create the first Project on an Account the field displays 1, but then if you delete that Project the field still displays 1. I need it to go back to zero if there are no Projects on the Account anymore.
Any help would be greatly appreciated,
Any help would be greatly appreciated,
trigger RollupActiveProjectsCountToAccountTrigger on Project__c (after insert, after update, after delete, after undelete) {
set<Id> set_id = new set<Id>();
if(trigger.isInsert || trigger.isUpdate) {
for(Project__c prj: trigger.new) {
set_id.add(prj.Company_Name__c);
}
}
else if(trigger.isDelete) {
for(Project__c prj: trigger.old) {
set_id.add(prj.Company_Name__c);
}
}
if(trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isDelete)) {
acc_list = [SELECT id, Total_Active_Projects__c, (SELECT id,name FROM relationShipName) FROM Account WHERE Id IN :set_id];
for(Account acc: acc_list) {
if(acc.relationShipName.size() > 0)
acc.Total_Active_Projects__c = acc.relationShipName.size();
else
acc.Total_Active_Projects__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
All Answers
If you have a master-detail relationship between Account and Project, using a Roll-up Summary field of type Count should be the best solution.
If for any reason you have to use a trigger to count depending projects, you have to use a trigger to count down when projects are deleted or detached from their account.
Hope this helps,
Fred
It seems there is also a "tricky" way to use roll-up summary on lookup relationships https://developer.salesforce.com/page/Declarative_Rollup_Summary_Tool_for_Force.com_Lookup_Relationships.
Fred
trigger RollupActiveProjectsCountToAccountTrigger on Project__c (after insert, after update, after delete, after undelete) {
set<Id> set_id = new set<Id>();
for(Project__c prj: trigger.new) {
set_id.add(prj.Company_Name__c);
}
if(trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isDelete)) {
acc_list = [SELECT id, Total_Active_Projects__c, (SELECT id,name FROM relationShipName) FROM Account WHERE Id IN :set_id];
for(Account acc: acc_list) {
if(acc.relationShipName.size() > 0)
acc.Total_Active_Projects__c = acc.relationShipName.size();
else
acc.Total_Active_Projects__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
Note: 'relationShipName' is a relation ship API name which exist between Account and Project.
Correct me if i am wrong, if it works fine for ur requirement select this one as best answer
No time to try but, you've declared variable "AccountIds" as a list of ids (line 3) but push "Company_Name__c" to it (lines 7 and 13) and use it for filtering on company_name__c (line 17) and on Account's id (line 19).
You have 2 variables, 1 for company_name__c and 1 for account's ids.
Not checked the rest of your code but I think you should start with that.
Suggestion: look at Jeff Douglas's blog (1st link) for a complete example with best practice (separating move business logic out of code trigger using Apex class).
@bsunidhar1
Trigger.new can be used only on insert and update trigger
Fred
trigger RollupActiveProjectsCountToAccountTrigger on Project__c (after insert, after update, after delete, after undelete) {
set<Id> set_id = new set<Id>();
if(trigger.isInsert || trigger.isUpdate) {
for(Project__c prj: trigger.new) {
set_id.add(prj.Company_Name__c);
}
}
else if(trigger.isDelete) {
for(Project__c prj: trigger.old) {
set_id.add(prj.Company_Name__c);
}
}
if(trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isDelete)) {
acc_list = [SELECT id, Total_Active_Projects__c, (SELECT id,name FROM relationShipName) FROM Account WHERE Id IN :set_id];
for(Account acc: acc_list) {
if(acc.relationShipName.size() > 0)
acc.Total_Active_Projects__c = acc.relationShipName.size();
else
acc.Total_Active_Projects__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}
If that is what you are talking about. I substituted that in, and this is what my code looks like now:
But now I'm getting this error:
Compile Error: Didn't understand relationship 'Company_Name__c' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 16 column 20
likewise u need child relationship between Account and Project Object
Any ideas?
trigger RollupActiveProjectsCountToAccountTrigger on Project__c (after insert, after update, after delete, after undelete) {
set<Id> set_id = new set<Id>();
if(trigger.isInsert || trigger.isUpdate) {
for(Project__c prj: trigger.new) {
set_id.add(prj.Company_Name__c);
}
}
else if(trigger.isDelete) {
for(Project__c prj: trigger.old) {
set_id.add(prj.Company_Name__c);
}
}
if(trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isDelete)) {
acc_list = [SELECT id, Total_Active_Projects__c, (SELECT id,name,Active_Project__c FROM relationShipName) FROM Account WHERE Id IN :set_id];
Integer i;
for(Account acc: acc_list) {
i=0;
if(acc.relationShipName.size() > 0) {
for(Active_Project__c pr : acc.relationShipName) {
if(pr.Active_Project__c) {
i++;
}
}
acc.Total_Active_Projects__c = i;
}
else
acc.Total_Active_Projects__c = 0;
}
if(!acc_list.isEmpty())
update acc_list;
}
}