0

I have a utility class for a trigger on a custom object (Territory) that is meant to query all the Territory records for an Account when a Territory record is updated. It then either sets a custom checkbox field on the Account to TRUE if all the Territory records contain one of the keywords listed in a Custom setting, or it sets the field to FALSE if any of the Territory records do not contain a keyword. I had the trigger working originally, but it was not bulkified so I am now trying to bulkify this utility class. I am running into a number of errors and I'm unsure how to address. Can anyone advise? I'm sure I'm missing a step or 2 or maybe have incorrect syntax.

Errors:

Line 25 - Method does not exist or incorrect signature: void accountMap(Id) from the type Utility1
Line 33 - Variable does not exist: acct
Line 35 - Variable does not exist: acct
Line 37 - DML requires SObject or SObject list type: Map<Id,Account>

Utility Class:

public class Utility1{
    
    public static void updateAccountImpacted(List<Territory__c> newTerritoryList){
        List<KeyWords__c> listKeywords = [SELECT Name FROM KeyWords__c];

        Set<Id> accountIds = new Set<Id>();
        Set<Id> TerrCount = new Set<Id>();
        Set<String> keyWords = new Set<String>();
        
        for(KeyWords__c listKW : listKeywords){
            keyWords.add(listKW.Name);
        }

        for(Territory__c terr : newTerritoryList){
            if(terrr.Account__c != null){
                accountIds.add(terr.Account__c);
            }
        }
        
        Map<Id,Account> accountMap = new Map<Id,Account>([SELECT Id,Impacted__c FROM Account WHERE Id IN: accountIds]);
            
            integer impact = 0;
            
            for(Territory__c terr2 :newTerritoryList){
                Account acct = accountMap(terr2.Account__c);
                        if((terr2.Notes__c != null && (keyWords.contains(terr2.Notes__c) || keyWords.contains(terr2.Notes__c))) || (terr2.Reason__c != null && (keyWords.contains(terr2.Reason__c) || keyWords.contains(terr2.Reason__c)))){
                            impact += 1;
                        }
                terrCount.add(terr2.Account__c);
            }

            if(impact < terrCount.size()){
                acct.Impacted__c = FALSE;
            }else{
                acct.Impacted__c = TRUE;
            }
            update accountMap;
    }
}

1 Answer 1

1

This is line 25: Account acct = accountMap(terr2.Account__c);

To get a value from a map (given the key), you need to use .get(). Line 25 should be Account acct = accountMap.get(terr2.Account__c);

You also have scoping issues. acct is defined inside of a for loop. The variable's scope is therefore tied to that loop. Once you exit that loop, the variable is out of scope and can no longer be used. That accounts for your second and third errors.

The fourth error is simply that you cannot perform DML on a map. Just like the error says, you either need a single SObject instance, or a List of SObjects. The .values() method returns a list (containing all of the values stored in the map), which can have DML performed on it, so simply writing update accountMap.values(); will resolve that error.

There are other logical issues with your code, but this'll help you get past those errors.

1
  • Thanks so much for that. All works now as expected!
    – J. Neilan
    Commented Nov 26, 2020 at 1:28

You must log in to answer this question.

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