You need to sign in to do that
Don't have an account?
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();
}
}
}
}
}
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:
Thank you sean ,
Tech force,
Rollup field is not available in all types of relationships in salesforce.Is that correct?
Hi Sean,
Can u please help me with the test class as well?
That is correct also.However,I would rather do these things with triggers since I am trying to get good at it.
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.
Thanks a lot ,sean