function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Robert Wambold 10Robert Wambold 10 

Method does not exist or incorrect signature: void add(Id, List) from the type Map

My APEX trigger was timing out, so I thought I would  to use a MAP, but I cannot get past the above error.

Anyone willing to take a look?

Map<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity Where Almac_Division__c='SI']){
    List<Opportunity> opplst = AccountOppMap.get(opp.accountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.add(opp.accountId,opplst); <- error line
}

Best Answer chosen by Robert Wambold 10
TechingCrewMattTechingCrewMatt
Add is a method on lists and sets. For maps, you cn use put as seen below:
Map<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity Where Almac_Division__c='SI']){
    List<Opportunity> opplst = AccountOppMap.get(opp.accountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.put(opp.accountId,opplst);
}

All Answers

TechingCrewMattTechingCrewMatt
Add is a method on lists and sets. For maps, you cn use put as seen below:
Map<Id,List<Opportunity>> accountOppMap = new Map<Id,List<Opportunity>>();
for(Opportunity opp : [Select id,accountId from opportunity Where Almac_Division__c='SI']){
    List<Opportunity> opplst = AccountOppMap.get(opp.accountId);
    if(opplst == null)
        opplst = new List<Opportunity>();
    opplst.add(opp);
    accountOppMap.put(opp.accountId,opplst);
}
This was selected as the best answer
Robert Wambold 10Robert Wambold 10

Thank you so much for your help. May I ask another question?

The second part of my trigger needs to access the Map for a field, MyOpsPath__c   then Update the Opportunity Object with information from the Account Object.

for(Account acc : trigger.new) {
       if(acc.SI_Site_Reference__c!=null){ 
          
          if((acc.Name != trigger.oldMap.get(acc.Id).Name) ||
             (acc.Site != trigger.oldMap.get(acc.Id).Site) ||
             (acc.SI_Site_Reference__c != trigger.oldMap.get(acc.Id).SI_Site_Reference__c)){      
              accountIds.add(acc.Id);

              if (!accountIds.isEmpty()){

                 if ( accountOppMap.ContainsKey(accountIds) ) {

                      Opportunity.MyOpsPath=acc.Name;

                      Update Opporttunity;

                 }

              }

          }       

       }

 

TechingCrewMattTechingCrewMatt
Most mature orgs are easier to manage if they don't have a lot of logic in the trigger itself. You would typically call a handler or utility class from the trigger to implement the detailed logic. In your case and in a lot of cases it might make sense to create an SObjectUtil class and a method to test if values were changed.

It's a little difficult to tell how these variables are declared and what the exxact problem is, but this should be good start:
 
for(Account acc : trigger.new) {
       if(acc.SI_Site_Reference__c!=null){ 
          
          if((acc.Name != trigger.oldMap.get(acc.Id).Name) ||
             (acc.Site != trigger.oldMap.get(acc.Id).Site) ||
             (acc.SI_Site_Reference__c != trigger.oldMap.get(acc.Id).SI_Site_Reference__c)){      
              accountIds.add(acc.Id);
          }       

       }

} 
 if (!accountIds.isEmpty()){
   List<Opportunity> oppsToUpdate = new List<Opportunity>();
   for(Id accId : accountIds){
       if ( accountOppMap.ContainsKey(accId) ) {
                      for(Opporunity opp : accountOrgMap.get(accId){
                           Opportunity.MyOpsPath=Trigger.newMap.get(accId).Name;
                           oppsToUpdate.add(opp);
                      }
        }

   }
   update oppsToUpdate;
}