You need to sign in to do that
Don't have an account?
error on my Trigger
guys I'm sorry for consecutive post of questions.
I'm new in Apex, this is the first language I'm trying to learn, bear with me please :D I'll treat beer and ice cream (whoa great combination!) if you get this. :D
I'm trying to create a trigger that worked like a rollup summary field, I wanted to calculate the summation of "total revenue" (custom field on a custom object) of all the related records to show on the Account(master) (lookup relationship). but before that I wanted to count first how many related records are there. I have a custom field name revenue on the Account. I want to place here the count of related record. Here are the code I have but it doesn't populate the field. The code only counts the related record, I'll create a new code to calculate the total revenue.
trigger projRev on Project__c (after insert, after update, after delete, after undelete) {
List<Project__c> newProject = new List<Project__c>();
Set<Id> AccId = new Set<Id>();
for (Project__c newProj : trigger.new) {
if(newProj.Account__r != null) {
AccId.add(newProj.Account__r.Id);
newProject.add(newProj);
}
}
List<Project__c> exProj = [Select Id from Project__c where Project__c.Account__r.Id IN :AccId];
integer treat = exProj.size();
for (Project__c newProjs : newProject) {
newProjs.Account__r.Revenue__c = treat;
}
}
I'm new in Apex, this is the first language I'm trying to learn, bear with me please :D I'll treat beer and ice cream (whoa great combination!) if you get this. :D
I'm trying to create a trigger that worked like a rollup summary field, I wanted to calculate the summation of "total revenue" (custom field on a custom object) of all the related records to show on the Account(master) (lookup relationship). but before that I wanted to count first how many related records are there. I have a custom field name revenue on the Account. I want to place here the count of related record. Here are the code I have but it doesn't populate the field. The code only counts the related record, I'll create a new code to calculate the total revenue.
trigger projRev on Project__c (after insert, after update, after delete, after undelete) {
List<Project__c> newProject = new List<Project__c>();
Set<Id> AccId = new Set<Id>();
for (Project__c newProj : trigger.new) {
if(newProj.Account__r != null) {
AccId.add(newProj.Account__r.Id);
newProject.add(newProj);
}
}
List<Project__c> exProj = [Select Id from Project__c where Project__c.Account__r.Id IN :AccId];
integer treat = exProj.size();
for (Project__c newProjs : newProject) {
newProjs.Account__r.Revenue__c = treat;
}
}
here it is...
trigger projRev on Project__c(after insert,after delete)
{
set<id> sid = new set<id>();
if(trigger.isAfter && trigger.isInsert)
{
for(Project__c c : trigger.new)
sid.add(c.account__c);
list<Account> lstacc = [select id, (select id from Projects__r) from Account where id in: sid] ;
for(Account acc : lstacc){
acc.Revenue__c = acc.Projects__r.size();
}
update lstacc;
}
if(trigger.isAfter && trigger.isDelete)
{
for(Project__c c : trigger.old)
sid.add(c.Account__c);
list<Account> lstacc = [select id, (select id from projects__r) from Account where id in:sid];
for(Account a : lstacc)
a.Revenue__c = a.Projects__r.size();
update lstacc;
}
}
Please try below code I hope that will help you
Please mark this as solution if this will help you
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html
Thanks
Amit Chaudhary
and do you have any idea on how <Aggregate Result> works?
I have tested below code in my Dev org. Now below code is working fine in all three event insert,delete and undelete.
NOTE :- Please check child relation API name according to you org "Projects__r"
Some exmple for AggregateResult for you :-
Please check below post for more details:-
http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions.htm
Please mark this as solution if this will help you.
Thanks,
Amit Chaudhary