Skip to main content
edited tags
Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431
added 246 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

trigger trgRollUpToAccount on Investment__c (after insert, after update) { //List to store Account IDs List AccountIDs = new List(); //Get list of target accounts from investment__c for(Investment__c i: Trigger.new) { AccountIDs.add(i.Target_Company__c); } //query to get all investment records whose account IDs are in the list AggregateResult[] groupedResults = [Select count(id)DealCount, Target_Company__c from Investment__c where Target_Company__c in :AccountIDs Group by Target_Company__c];

// get group of account objects to update Map<id,Account> accountMap = new Map<id,Account>([Select id, DealCount__c from Account where id in :AccountIds]);

//iterate over grouped results and set the account map's data to match

**for(AggregateResult ar: groupedResults) {

trigger trgRollUpToAccount on Investment__c (after insert, after update) 
{
    //List to store Account IDs
    List<Id> AccountIDs = new List<Id>(); 
    //Get list of target accounts from investment__c
    for(Investment__c i: Trigger.new) 
    {
        AccountIDs.add(i.Target_Company__c);
    } 
    //query to get all investment records whose account IDs are in the list
    AggregateResult[] groupedResults = [Select count(id)DealCount, Target_Company__c from Investment__c 
                                        where  Target_Company__c in :AccountIDs Group by Target_Company__c];
    
    // get group of account objects to update
    Map<id,Account> accountMap = new Map<id,Account>([Select id, DealCount__c from Account where id in :AccountIds]);
    
    //iterate over grouped results and set the account map's data to match
    
    **for(AggregateResult ar: groupedResults) 
    { 
        
        accountMap.get(ar.get('Target_Company__c' )).DealCount__c = ar.get('DealCount');  
    }** 
        
        //Update account objects:
        try {
            update accountMap.values();
        }
    catch(DmlException e) {
        System.debug(e.getMessage());
    } 
    
}

}**

//Update account objects: try { update accountMap.values(); } catch(DmlException e) { System.debug(e.getMessage()); }

}

trigger trgRollUpToAccount on Investment__c (after insert, after update) { //List to store Account IDs List AccountIDs = new List(); //Get list of target accounts from investment__c for(Investment__c i: Trigger.new) { AccountIDs.add(i.Target_Company__c); } //query to get all investment records whose account IDs are in the list AggregateResult[] groupedResults = [Select count(id)DealCount, Target_Company__c from Investment__c where Target_Company__c in :AccountIDs Group by Target_Company__c];

// get group of account objects to update Map<id,Account> accountMap = new Map<id,Account>([Select id, DealCount__c from Account where id in :AccountIds]);

//iterate over grouped results and set the account map's data to match

**for(AggregateResult ar: groupedResults) {

      accountMap.get(ar.get('Target_Company__c' )).DealCount__c = ar.get('DealCount');  

}**

//Update account objects: try { update accountMap.values(); } catch(DmlException e) { System.debug(e.getMessage()); }

}

trigger trgRollUpToAccount on Investment__c (after insert, after update) 
{
    //List to store Account IDs
    List<Id> AccountIDs = new List<Id>(); 
    //Get list of target accounts from investment__c
    for(Investment__c i: Trigger.new) 
    {
        AccountIDs.add(i.Target_Company__c);
    } 
    //query to get all investment records whose account IDs are in the list
    AggregateResult[] groupedResults = [Select count(id)DealCount, Target_Company__c from Investment__c 
                                        where  Target_Company__c in :AccountIDs Group by Target_Company__c];
    
    // get group of account objects to update
    Map<id,Account> accountMap = new Map<id,Account>([Select id, DealCount__c from Account where id in :AccountIds]);
    
    //iterate over grouped results and set the account map's data to match
    
    **for(AggregateResult ar: groupedResults) 
    { 
        
        accountMap.get(ar.get('Target_Company__c' )).DealCount__c = ar.get('DealCount');  
    }** 
        
        //Update account objects:
        try {
            update accountMap.values();
        }
    catch(DmlException e) {
        System.debug(e.getMessage());
    } 
    
}
Source Link

Incompatible key type object for Map<id,account>

I am trying to write apex code (and I'm learning how) to roll up data from the on the Account from an object with a lookup relationship to the object.

Fields from Account: ID, DealCount__c

Fields from related object - Investment__c: Target_Company__c (ID from Account), count of records.

when I try saving it, I'm getting the error: Incompatible key type Object for Map<Id,Account> at line 22 column 12. (between ... in code below)

Code:

trigger trgRollUpToAccount on Investment__c (after insert, after update) { //List to store Account IDs List AccountIDs = new List(); //Get list of target accounts from investment__c for(Investment__c i: Trigger.new) { AccountIDs.add(i.Target_Company__c); } //query to get all investment records whose account IDs are in the list AggregateResult[] groupedResults = [Select count(id)DealCount, Target_Company__c from Investment__c where Target_Company__c in :AccountIDs Group by Target_Company__c];

// get group of account objects to update Map<id,Account> accountMap = new Map<id,Account>([Select id, DealCount__c from Account where id in :AccountIds]);

//iterate over grouped results and set the account map's data to match

**for(AggregateResult ar: groupedResults) {

      accountMap.get(ar.get('Target_Company__c' )).DealCount__c = ar.get('DealCount');  

}**

//Update account objects: try { update accountMap.values(); } catch(DmlException e) { System.debug(e.getMessage()); }

}