The result should display a table of all accounts from a specific user and the aggregateresult of the related opportunities and sales.

  1. I create a map (AccList) of all accounts and add the Ids to a Set.
  2. I create a map (OppMap) where I aggregate values which are related to the set Ids.
  3. I create a map (SalMap) where I aggregate values which are related to the set Ids.

How can I add the values of the OppMap and the SalMap to the Map AccList?

I tried to create a new aggregateresult, but than I get an error "Illegal assignment from Account to AggregateResult" --> AggregateResult finalmap = AccList.get((Id)OppAr.get('AccId'));

public class VTPv9 {
    Public List<ResultWrapper> resultList {get; set;}
    public class ResultWrapper{
        public Id       AccId           {get; set;}
        public String   AccName         {get; set;}
        public Decimal  expectedRevenue {get; set;}
        public Decimal  amount          {get; set;}
        public Decimal  sales           {get; set;}
    public VTPv9() {    
    public void getcreatemaps() {
        resultList = new List<ResultWrapper>();
        // MAP Account
        //create Map with Account who related to selected owner
        Map<Id, Account> AccList = new Map<Id, Account>([SELECT Id FROM Account WHERE OwnerId = '005b0000001UAm4AAG']);
        Set<Id> AccIdSet = new Set<Id>();              
        for(Account Acc:AccList.values()){
        system.debug('size AccList ' +AccList.size()); 
        system.debug('size AccIdSet ' +AccIdSet.size()); 
        //MAP Opportunity
        Map<Id, AggregateResult> OppMap = new Map<Id, AggregateResult>();
        for(AggregateResult OppAr :[SELECT Account.Id AccId, Sum(ExpectedRevenue) Rev, Sum(Amount) Fin FROM Opportunity WHERE Account.Id = :AccIdSet AND OwnerId = '005b0000001UAm4AAG' AND CloseDate = THIS_YEAR AND (IsClosed = false OR IsWon = true) GROUP BY Account.Id]) {
            OppMap.put((Id)OppAr.get('AccId'), OppAr);
            ResultWrapper combinedResult = new ResultWrapper(); 
            //AggregateResult finalmap = AccList.get((Id)OppAr.get('AccId'));
            combinedResult.Accid            = (Id)OppAr.get('AccId');  
            combinedResult.expectedRevenue  = (Decimal)OppAr.get('Rev');
            combinedResult.amount           = (Decimal)OppAr.get('Fin');
            system.debug('Id: ' + (Id)OppAr.get('AccId') + ' | ExpectedRevenue: ' + (Decimal)OppAr.get('rev') + ' | Amount: ' + (Decimal)OppAr.get('fin'));
            system.debug('combinedResult: ' + resultlist);            
        // MAP Sales
        Map<Id, AggregateResult> SalMap = new Map <Id, AggregateResult>();
        for(AggregateResult SalAr :[SELECT Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__r.Id AccId, Sum(BWS_Brutto__c) BWS FROM Umsatz__c WHERE Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__r.Id = :AccIdSet AND Berater__r.Id = '005b0000001UAm4AAG' AND Bewertungsmonat__c = THIS_YEAR GROUP BY Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__r.Id]) {            
            SalMap.put((Id)SalAr.get('AccId'), SalAr);
            ResultWrapper combinedResult = new ResultWrapper(); 
            combinedResult.Accid            = (Id)SalAr.get('AccId');  
            combinedResult.sales            = (Decimal)SalAr.get('BWS');
            system.debug('Id: ' + (Id)SalAr.get('AccId') + ' | Sales: ' + (Decimal)SalAr.get('BWS'));
            system.debug('combinedResult: ' + resultlist);             

1 Answer 1


AggregateResult finalmap = AccList.get((Id)OppAr.get('AccId'));
When you get an element from AccList, you get an Account because the map has been defined as Map<Id, Account> and of course you cannot assign an Account to a variabile of type AggregateResult. That's why you got that error.

Moreover you were adding multiple wrapper with the same accountId in the resultList.

You don't need three maps, but only one: a map that let you relate an account Id with a wrapper.
Then you will fill it in a for loop on Account, while in the others you'll get the wrapper from the map in order to set revenue, amount and sales. Doing that, the list yor're looking for is just map.values().

public void getcreatemaps() {
    // Create a map of wrapper
    Map<Id, ResultWrapper> mapAccountIdToWrapper = new Map<Id, ResultWrapper>();
    // Create a set of Account Id
    Set<Id> accountIds = new Set<Id>();
    // Fill the wrapper map and the set of Id
    for (Account acc : [SELECT Id, Name FROM Account WHERE OwnerId = '005b0000001UAm4AAG']) {
        ResultWrapper wrap = new ResultWrapper();
        wrap.AccId = acc.Id;
        wrap.AccName = acc.Name;
        mapAccountIdToWrapper.put(wrap.AccId, wrap);

    // for each value in the map, set revenue and amount
    for(AggregateResult oppAr :[SELECT AccountId AccId, Sum(ExpectedRevenue) Rev, Sum(Amount) Fin FROM Opportunity WHERE AccountId IN :accountIds AND OwnerId = '005b0000001UAm4AAG' AND CloseDate = THIS_YEAR AND (IsClosed = false OR IsWon = true) GROUP BY AccountId]) {
        Id accountId = (Id) oppAr.get('AccId');
        ResultWrapper combinedResult = mapAccountIdToWrapper.get(accountId);
        combinedResult.expectedRevenue  = (Decimal)oppAr.get('Rev');
        combinedResult.amount           = (Decimal)oppAr.get('Fin');
    // for each value in the map, set sales
    for(AggregateResult salAr :[SELECT Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__c AccId, Sum(BWS_Brutto__c) BWS FROM Umsatz__c WHERE Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__c IN :accountIds AND Berater__c = '005b0000001UAm4AAG' AND Bewertungsmonat__c = THIS_YEAR GROUP BY Einzelrisiko__r.Gruppenvertrag__r.Unternehmen__c]) {            
        Id accountId = (Id) salAr.get('AccId');
        ResultWrapper combinedResult = mapAccountIdToWrapper.get(accountId);
        combinedResult.sales = (Decimal) salAr.get('BWS');         
    resultList = mapAccountIdToWrapper.values();

By the way, I strongly advise against hardcoding id. If you want to retrieve only the record owned by a specific user, you could store a unique identifier (username or something else) in a custom metadata, then use it in your code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .