Write a code to find the average amount for all your opportunities by campaign?



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

Post New Answer

More Salesforce Interview Questions

What is standard list controller? : salesforce visualforce

1 Answers  


What are the different workflow actions available in workflows?

1 Answers   Cognizant,


What is Salesforce in simple terms?

1 Answers  


What is a Case Queue?

1 Answers  


What identifies a controller as being an extension? : salesforce visualforce

1 Answers  


How many ways call controller from visualforce page ?

1 Answers  


What are different chart types available for dashboards? : salesforce reports and dashboards

1 Answers  


Can we create master detail relationship between two standard objects?

1 Answers  


How Does Salesforce CRM?

1 Answers  


What are the escalation rules?

1 Answers  


Is there is any option to specify time zone for uploading records in apex data loader? : salesforce Data Management

1 Answers  


What are the tasks of standard controller? : salesforce visualforce

1 Answers  


Categories