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
Jayson Faderanga 14Jayson Faderanga 14 

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;
       
       
}




}
sreenivasAppssreenivasApps
Hi Jayson,

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;  
    }

}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code 
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] ;
		List<Account> lstAccountToUpdate = new List<Account>();
        for(Account acc : lstacc)
		{
            acc.Revenue__c = acc.Projects__r.size();
			lstAccountToUpdate.add(acc);
		}
		if(lstAccountToUpdate.size() > 0)
		{
			update lstAccountToUpdate;
		}		
    }
    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];
		List<Account> lstAccountToUpdate = new List<Account>();
		for(Account a : lstacc)
		{
			a.Revenue__c = a.Projects__r.size();
			lstAccountToUpdate.add(a);
		}
		if(lstAccountToUpdate.size() > 0)
		{
			update lstAccountToUpdate;
		}		
    }

}


 
Jayson Faderanga 14Jayson Faderanga 14
guys both worked! I appreciate it..I'm actually worrying if my user undelete project records..it is not updating the field....
Amit Chaudhary 8Amit Chaudhary 8
Hi Jayson,

Please try below code I hope that will  help you
 
trigger projRev on Project__c(after insert,after delete ,after undelete)
{
    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] ;
		List<Account> lstAccountToUpdate = new List<Account>();
        for(Account acc : lstacc)
		{
            acc.Revenue__c = acc.Projects__r.size();
			lstAccountToUpdate.add(acc);
		}
		if(lstAccountToUpdate.size() > 0)
		{
			update lstAccountToUpdate;
		}		
    }
    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];
		List<Account> lstAccountToUpdate = new List<Account>();
		for(Account a : lstacc)
		{
			a.Revenue__c = a.Projects__r.size();
			lstAccountToUpdate.add(a);
		}
		if(lstAccountToUpdate.size() > 0)
		{
			update lstAccountToUpdate;
		}		
    }
    if(trigger.isAfter && trigger.isUndelete)
	{
		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];
		List<Account> lstAccountToUpdate = new List<Account>();
		for(Account a : lstacc)
		{
			a.Revenue__c = a.Projects__r.size();
			lstAccountToUpdate.add(a);
		}
		if(lstAccountToUpdate.size() > 0)
		{
			update lstAccountToUpdate;
		}		
    }

}

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
 
Jayson Faderanga 14Jayson Faderanga 14
Hi Amit,. thanks for the support. I did try the code you put above but it doesn't allow me to undelete a record.
and do you have any idea on how <Aggregate Result> works?
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Jayson,

I have tested below code in my Dev org. Now below code is working fine in all three event insert,delete and undelete.
trigger projRev on Project__c(after insert,after delete ,after undelete)
{
    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,Revenue__c, (select id from Projects__r) from Account where id in: sid] ;
        List<Account> lstAccountToUpdate = new List<Account>();
        for(Account acc : lstacc)
        {
            acc.Revenue__c = acc.Projects__r.size();
            lstAccountToUpdate.add(acc);
        }
        if(lstAccountToUpdate.size() > 0)
        {
            update lstAccountToUpdate;
        }       
    }
    if(trigger.isAfter && trigger.isDelete)
    {
        for(Project__c c : trigger.old)
        {
            sid.add(c.Account__c);
        }
        
        list<Account> lstacc = [select id,Revenue__c, (select id from Projects__r) from Account where id in:sid];
        List<Account> lstAccountToUpdate = new List<Account>();
        for(Account a : lstacc)
        {
            a.Revenue__c = a.Projects__r.size();
            lstAccountToUpdate.add(a);
        }
        if(lstAccountToUpdate.size() > 0)
        {
            update lstAccountToUpdate;
        }       
    }
    if(trigger.isAfter && trigger.isUndelete)
    {
        for(Project__c c : trigger.new)
        {
            sid.add(c.Account__c);
        }
        
        list<Account> lstacc = [select id,Revenue__c, (select id from Projects__r) from Account where id in:sid];
        List<Account> lstAccountToUpdate = new List<Account>();
        for(Account a : lstacc)
        {
            a.Revenue__c = a.Projects__r.size();
            lstAccountToUpdate.add(a);
        }
        if(lstAccountToUpdate.size() > 0)
        {
            update lstAccountToUpdate;
        }       
    }

}

NOTE :- Please check child relation API name according to you org "Projects__r"

Some exmple for AggregateResult for you :-
public class soqlfunc
{
public integer count12{set;get;}
public double  avg{set;get;}
public double  sum{set;get;}
public double  max{set;get;}
public double  min{set;get;}

	public soqlfunc()
	{
		count12 = [SELECT count()  FROM transaction__c ];
		AggregateResult[] groupedResults = [SELECT AVG(Amount__c) aver,sum(Amount__c) sum,max(Amount__c) max,min(Amount__c) min FROM transaction__c];

		avg = double.valueOf(groupedResults[0].get('aver'));
		sum = double.valueOf(groupedResults[0].get('sum'));  
		max = double.valueOf(groupedResults[0].get('max'));  
		min = double.valueOf(groupedResults[0].get('max'));

	}

}


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