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
S MS M 

How to get a map from a set of ids and a list of elements

public static void updateGeoTaxEntity(List<OpportunityTeamMember> newOpportunityTeamMembers){
		List<OpportunityTeamMember> otmsToUpdate = new List<OpportunityTeamMember>();
        Set<Id> opportunityIds = new Set<Id>();
        for(OpportunityTeamMember currentOTM : newOpportunityTeamMembers){
            opportunityIds.add(currentOTM.opportunityId); 
        }
               
        Map<Id,Opportunity> relatedOpps = new Map<Id,Opportunity>([SELECT Id, Terr_Geo__c, Tax_Entity__c FROM Opportunity WHERE Id IN:opportunityIds]);
        for(OpportunityTeamMember currentOTM : newOpportunityTeamMembers){
            if(relatedOpps.containsKey(currentOTM.OpportunityId)){
                Opportunity opp = relatedOpps.get(currentOTM.OpportunityId);
                currentOTM.Geo__c = opp.Terr_Geo__c;
                currentOTM.Tax_Entity__c = opp.Tax_Entity__c;
                otmsToUpdate.add(currentOTM);
            }
        } 
    }

This is my OpportunityTeamMemberHelper class that was called by my OpportunityTeamMemberTrigger.

I have a method updateGeoTaxEntity that gets a list of OpportunityTeamMember records that are being inserted. My intention is to update all OpportunityTeamMember records with the same value for Territory and Geo as is existing within the Opportunity.

The above code works.

But I need to remove the select query and replace it with a Selector Layer Query. All it means is that I cannot use that Select Query in my code. I use a Selector Utility code that gives me a list of Opportunity records for the ids found in opportunityIds.

I need to now get a map of Map<Id,Opportunity>

I can get the id from newOpportunityTeamMember.opportunityId but how do i link it to the opportunity record that I just queried?

Please can you help?


 
Best Answer chosen by S M
Deepali KulshresthaDeepali Kulshrestha
Hi SM, 

You want to create a map of id and opportunity records you have to create a Map<Id, Opportunity> for this please try the given code.
Map<Id,Opportunity> OppMap = new Map<Id, Opportunity>();
        for(Opportunity oppObj : OpportunityList){
            OppMap.put(oppObj.Id , oppObj);
        }
here OpportunityList is the list of Opportunity records you just query.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha