Write a code to find the average amount for all your opportunities by campaign?
Answer / Nidhi Gupta
Here's an example of a SOQL query that calculates the average amount for all opportunities grouped by campaign:n```nList<AggregateResult> results = [SELECT CampaignId, SUM(Amount) total_amount, COUNT() total_opportunities FROM Opportunity GROUP BY CampaignId]; List<Campaign> campaigns = [SELECT Id, Name FROM Campaign]; Map<Id, Campaign> campaignMap = new Map<Id, Campaign>(); for (Campaign c : campaigns) {n campaignMap.put(c.Id, c);n}nfor (AggregateResult ar : results) {n Campaign campaign = campaignMap.get((Id)ar.get('CampaignId'));n system.debug(String.format('Campaign: %s - Average Amount: %.2f', campaign.Name, ar.get('total_amount') / (double)ar.get('total_opportunities')));n}n```
| Is This Answer Correct ? | 0 Yes | 0 No |
What is standard list controller? : salesforce visualforce
What are the different workflow actions available in workflows?
What is Salesforce in simple terms?
What is a Case Queue?
What identifies a controller as being an extension? : salesforce visualforce
How many ways call controller from visualforce page ?
What are different chart types available for dashboards? : salesforce reports and dashboards
Can we create master detail relationship between two standard objects?
How Does Salesforce CRM?
What are the escalation rules?
Is there is any option to specify time zone for uploading records in apex data loader? : salesforce Data Management
What are the tasks of standard controller? : salesforce visualforce