• Aditya Raut
  • NEWBIE
  • 40 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 3
    Replies
In the below class, how to put the List(oppList) in the value of Map(oppMap)?

public class class1{
    
    public static Map<Id, list<opportunity>> oppmethod(Set<Id> oppId){
        
    List<Opportunity> oppList = [SELECT Id, AccountId, Name FROM Opportunity where ID IN: oppId];

    Map<Id, list<opportunity>> oppmap = new Map<Id, list<opportunity>>();
    
    for(Opportunity op : oppList){
        oppmap.put(op.AccountId, op);
    }
    
    return oppmap;
    }
}
trigger createfutherdetails on Opportunity (before insert) {
    Set<Id> accSpecificIds = new Set<Id>();
    
    List<OpportunityContactRole> ocrlist = new List<OpportunityContactRole>(); 
    
    Map<Id, List<Contact>> accountSpecificContacts = new Map<Id, List<Contact>>();
    
    for(Opportunity o: Trigger.New) { 
        if(o.AccountSpecificOppId__c != Null){
            accSpecificIds.add(o.AccountSpecificOppId__c);
        }
    }
    for(Contact con: [select Id,AccountSpecificId__c from Contact where AccountSpecificId__c IN: accSpecificIds]) {
            if(!accountSpecificContacts.containsKey(con.AccountSpecificId__c)){
                accountSpecificContacts.put(con.AccountSpecificId__c, new List<Contact>());
                accountSpecificContacts.get(con.AccountSpecificId__c).add(con);
            }
    }
    for(Opportunity opp: Trigger.New) {
                if(accountSpecificContacts.containskey(opp.AccountSpecificOppId__c)&& accountSpecificContacts.get(opp.AccountSpecificOppId__c) != NULL) { 
                    Boolean isFirstContact = true;
                                   
                    for(Contact c: accountSpecificContacts.get(opp.AccountSpecificOppId__c)) {
                        OpportunityContactRole ocr = new OpportunityContactRole(ContactId = c.Id, OpportunityId = opp.Id);
                        if(isFirstContact) {
                            ocr.IsPrimary = true; 
                            isFirstContact = false;
                        }
                        ocrList.add(ocr);
                    }
                }
            
    }
    
        if(ocrlist.size() > 0)
        insert ocrlist;
        
        
    }