+ Start a Discussion
DbjensenDbjensen 

Retrieving data in a Map from 1 of 2 records retrieved from a SOQL query

Hello - I have an object called Agent. Every sales rep has 2 agent records. One record represents an Outbound record and the other is Inbound. The field that identifies inbound/outbound is Rep_Type__c. For outbound it's SLO. For inbound it's SLI.

Below is the query I'm using to get both records.  

List<Agent__c> salesAgentList = [SELECT Id, SOB_Code__c, Name, Rep_Type__c, Agent_Performance__c, User__c FROM Agent__c 
                                         WHERE SOB_Code__c IN :mapNewSalesAgntSOB.keySet() OR User__c IN :mapOfNewLeadOwner.keySet()]; 

The problem is, my map is grabbing the inbound record (SLI) and I need to grab the outbound record (SLO). 

        Map<Id, String> mapOfSalesRepType = new Map<Id, String>();
            
        for(Agent__c agt : salesAgentList) {
            mapOfSalesRepType.put(agt.User__c, agt.Rep_Type__c);
        }
        
        System.debug('Agent Rep type ' + mapOfSalesRepType);
      
How can I do this? 
Best Answer chosen by Dbjensen
Steven NsubugaSteven Nsubuga
Use 2 maps,  1 for SLO and 1 for SLI
List<Agent__c> salesAgentList = [SELECT Id, SOB_Code__c, Name, Rep_Type__c, Agent_Performance__c, User__c FROM Agent__c 
                                         WHERE SOB_Code__c IN :mapNewSalesAgntSOB.keySet() OR User__c IN :mapOfNewLeadOwner.keySet()];

Map<Id, String> mapOfSalesRepTypeSLO = new Map<Id, String>();
Map<Id, String> mapOfSalesRepTypeSLI = new Map<Id, String>();
            
        for(Agent__c agt : salesAgentList) {
            if (agt.Rep_Type__c == 'SLO') {
                mapOfSalesRepTypeSLO.put(agt.User__c, agt.Rep_Type__c);
            }
            else if (agt.Rep_Type__c == 'SLI'){
                mapOfSalesRepTypeSLI.put(agt.User__c, agt.Rep_Type__c);
            }
        }


 

All Answers

Steven NsubugaSteven Nsubuga
Why not look for the SLO in an if statement?
List<Agent__c> salesAgentList = [SELECT Id, SOB_Code__c, Name, Rep_Type__c, Agent_Performance__c, User__c FROM Agent__c 
                                         WHERE SOB_Code__c IN :mapNewSalesAgntSOB.keySet() OR User__c IN :mapOfNewLeadOwner.keySet()];

Map<Id, String> mapOfSalesRepType = new Map<Id, String>();
            
        for(Agent__c agt : salesAgentList) {
            if (agt.Rep_Type__c == 'SLO') {
                mapOfSalesRepType.put(agt.User__c, agt.Rep_Type__c);
            }
        }


 

 
DbjensenDbjensen
Hi Steven - that would work. Later on in my code, I'll need to get data from the SLI record. Would it be better to create a different maps and 'for' statement for the SLI records I have to retrieve? 
Raj VakatiRaj Vakati
What is the way to find the inbound record (SLI) and outbound record (SLO) whihc fields it is .. you need to do it like below 

try this
 
List<Agent__c> salesAgentList = [SELECT Id, SOB_Code__c, Name, Rep_Type__c, Agent_Performance__c, User__c FROM Agent__c 
                                         WHERE SOB_Code__c IN :mapNewSalesAgntSOB.keySet() OR User__c IN :mapOfNewLeadOwner.keySet()]; 

The problem is, my map is grabbing the inbound record (SLI) and I need to grab the outbound record (SLO). 

        Map<Id, String> mapOfSalesRepType = new Map<Id, String>();
            
        for(Agent__c agt : salesAgentList) {
			If(agt.Rep_Type__c=='outbound'){
            mapOfSalesRepType.put(agt.User__c, agt.Rep_Type__c);
			}
        }
        
        System.debug('Agent Rep type ' + mapOfSalesRepType);

 
Steven NsubugaSteven Nsubuga
Use 2 maps,  1 for SLO and 1 for SLI
List<Agent__c> salesAgentList = [SELECT Id, SOB_Code__c, Name, Rep_Type__c, Agent_Performance__c, User__c FROM Agent__c 
                                         WHERE SOB_Code__c IN :mapNewSalesAgntSOB.keySet() OR User__c IN :mapOfNewLeadOwner.keySet()];

Map<Id, String> mapOfSalesRepTypeSLO = new Map<Id, String>();
Map<Id, String> mapOfSalesRepTypeSLI = new Map<Id, String>();
            
        for(Agent__c agt : salesAgentList) {
            if (agt.Rep_Type__c == 'SLO') {
                mapOfSalesRepTypeSLO.put(agt.User__c, agt.Rep_Type__c);
            }
            else if (agt.Rep_Type__c == 'SLI'){
                mapOfSalesRepTypeSLI.put(agt.User__c, agt.Rep_Type__c);
            }
        }


 
This was selected as the best answer
DbjensenDbjensen
ahh...got it. Thanks so much, Steven. I really appreciate this.