You need to sign in to do that
Don't have an account?
sonam guptha
Total Amount opportunites print on Account.
Hi,
In My account object “total_ amount__c" field is there,and in opportunity "amount" field is there,
I want to dispaly sum of amount of all child opportunities in the total _amount__c field in the parent account.
I written below trigger which is working fine,but here i need to fire this trigger when opportunity.stagename is set to closed won,but its not working accordingly.its firing for all the stages.
Can anyone help me to fix this.
Thanks Everyone!
In My account object “total_ amount__c" field is there,and in opportunity "amount" field is there,
I want to dispaly sum of amount of all child opportunities in the total _amount__c field in the parent account.
I written below trigger which is working fine,but here i need to fire this trigger when opportunity.stagename is set to closed won,but its not working accordingly.its firing for all the stages.
Can anyone help me to fix this.
trigger totalamount on Account (before insert,before update) { List<opportunity> Lstopp = [Select id,stagename from opportunity]; for(Account record: Trigger.new) { record.total_Amount__c = null; } for(Opportunity opp : Lstopp){ if(opp.stagename == ‘Closed - won'){ for(AggregateResult result: [SELECT SUM(Amount) Amt, AccountId Id FROM opportunity WHERE AccountId IN :Trigger.new GROUP BY AccountId]) { Trigger.newMap.get((Id)result.get('Id')).Sum_of_Opportunity_Amount__c = (Decimal)result.get('Amt'); } } }
Thanks Everyone!
Here is the sample code.
Also, your trigger is not bulkified to handle Bulk Insert or Update of the Accounts.
Hope this helps.
All Answers
Here is the sample code:
If it works, Mark it as best answer.
Thanks
Brahma
Here is the sample code.
Also, your trigger is not bulkified to handle Bulk Insert or Update of the Accounts.
Hope this helps.
Thank you its working like a charm, i need to call the same logic using class,then call that class from trigger.
Try these.
Let me know if this works.
And, mark your question as solved, if it does.
I am glad it helped.
Web Service Callouts have to be tested using CalloutMock implementation.
Here are some information on this.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
If you need more information on this, please post it a separate question. Because this is a different topic altogether.
One Last help for test class,am not able to cover the below lines,can you please check once.
Try this code.
Am Receving following error:--
On the same trigger i replaced the stagename with opp_check boolean value its working fine,but when it set to false i dont need to add that opp.amount to account.Sum_of_Opportunity_Amount__c,this is also working fine when there are multiple opportunites on that account,but if we have only one opportunity per account and if the Opp_check = false,then account.Sum_of_Opportunity_Amount__c is not setting to zero. i need to set it to null if the checkbox is false and have only one opportunity on that account,how can i achieve this.Please have a look. replaced with :--
Replace the for condition with the following code and try. If you are looking at setting the Sum_of_Opportunity_Amount__c field to 0 for the accounts without any opportunities (meeting the above mentioned condition), you may want to setup a default value in the field definition.
I replaced the code its working super fine,but the only problem is if again opp.Opp_check__c = false and had only one opp on account,that value is still am seeing on the account.Sum_of_Opportunity_Amount__c,that particualr value i want to make it null.
And for setting up default value i cannot see opp..Opp_check__c value on account,as the Sum_of_Opportunity_Amount__c is on account,can we do it the same thing in apex?