+ Start a Discussion
RocketRocket 

Account and opportunity trigger

Hi friends,

 

I am trying to calculate related closed won opportunities on the account object.For example,account1 has 5 opportunities.I want to show that number on the account object.

When I debug I see null value in the statement .Don't know why

 

System.debug('the value in the zero variabke of a.zero is'+a.add(zero)); 

 

Can somebody help by either correcting it or suggesting better solution .

 

 

 

 

 

 

 

trigger status1 on Account (before insert,before update)
{
 
List<Account> accountWithOpptys = [select id, name,WonOpportunitiesRelatedToThisAccount__c, (select id, name, closedate, stagename from Opportunities  where accountId IN :Trigger.newMap.keySet()and StageName = 'Closed Won')from Account where Id IN :Trigger.newMap.keySet()];


list<account>l=new list<account>();
list<opportunity>a=new list<opportunity>();
    for(account app:accountWithOpptys)
        {
        for(opportunity zero: app.opportunities)
         {
         System.debug('Tasjdshjshdjshdsbefore is statement ');
        if(zero.stagename=='Closed Won')
            {
                a.add(zero);
                  
                System.debug('the value in the zero variabke of a.zero is'+a.add(zero)); 
                   
                   
               System.debug('this is in sdie for loop with the value in the zero.stagenemnt');
                //l.add(ask);                       
                if(a.size()>0)
                {
                
                app.WonOpportunitiesRelatedToThisAccount__c=l.size();
                
                
             
                
                }
                
            }
            
}
}
}

Sean TanSean Tan

A list methods add method doesn't return anything, so doing a debug on it will simply print out null. There are a couple of ways you can change the trigger to do the work, following your original logic you can try this one:

 

trigger status1 on Account (before insert,before update)
{
    List<Account> accountWithOpptys = [select id, name, WonOpportunitiesRelatedToThisAccount__c, (select id, name, closedate, stagename from Opportunities where StageName = 'Closed Won') from Account where Id IN :Trigger.newMap.keySet()];
    
    for (Account app : accountWithOpptys)
    {
        Account a = Trigger.newMap.get(app.Id);
        //Don't need to do another check for closed won since your query already filtered it
        if (app.Opportunities != null)
        {
            a.WonOpportunitiesRelatedToThisAccount__c = app.Opportunities.size();
        }
        else
        {
            a.WonOpportunitiesRelatedToThisAccount__c = 0;
        }
    }
}

 

 

 

Bhawani SharmaBhawani Sharma
Why don't you do this with Rollup summary formula field? You can simply create a a rollup field and find out count of opportunities in closed won stage.
SurpriseSurprise

Thank you sean ,

 

Tech force,

Rollup field is not available in all types of relationships in salesforce.Is that correct?

 

 

 

SurpriseSurprise

Hi Sean,

 

Can u please help me with the test class as well?

 

Bhawani SharmaBhawani Sharma
But you can definitely Rollup Opportunities on Account.
SurpriseSurprise

That is correct also.However,I would rather do these things with triggers since I am trying to get good at it.

 

Sean TanSean Tan

Tech Force's suggestion is the right way to be doing this type of calculation as you will have to trap other scenarios in the future (e.g. addition of opportunities / removals etc). However, to your question for the test class here is a roughly coded test class, this probably won't work immediately so you will need to adjust stuff as you go (and insert any required fields for the insert of those objects) but this should get you in the right direction.

 

@isTest(SeeAllData=false)
private class TestOpportunityRollup 
{
	static testMethod void myUnitTest()
	{
		Account a1 = new Account(Name='TEST ACCOUNT WITH OPPORTUNITIES');
		Account a2 = new Account(Name='TEST ACCOUNT WITHOUT OPPORTUNITIES');	
		Account[] aList = new Account[]{a1,a2};
insert aList; Opportunity[] oList = new Opportunity[]{}; for (Integer i = 0 ; i < 3 ; i++) { oList.add(new Opportunity(StageName='Closed Won', AccountId=a1.Id, ClosedDate=Date.today(), Name='Opportunity Test #' + (i + 1)); } insert oList; update aList; //Check to ensure the rollup worked for the ones with opportunities a1 = [ SELECT Id, WonOpportunitiesRelatedToThisAccount__c FROM Account WHERE Id = :a1.Id ]; System.assertEquals(a1.WonOpportunitiesRelatedToThisAccount__c, oList.size()); //Check for no opportunities a2 = [ SELECT Id, WonOpportunitiesRelatedToThisAccount__c FROM Account WHERE Id = :a2.Id ]; System.assertEquals(a2.WonOpportunitiesRelatedToThisAccount__c, 0); } }

 

SurpriseSurprise

Thanks a lot ,sean