You need to sign in to do that
Don't have an account?

How to query Map instance
I have a map instance of two strings. Each record is a zip code and a text literal (for example <90210, 'IP'> or <90210, 'RP'> ). The combination of both values makes each record unique.
In my bulk load process, I have a need where I need to query against this Map object. I haven't seen where anyone has done this and I'm wondering what kind of syntax I should use.
My select statement looks like this - I need help on the where clause
What I really want to do is something like:
In my bulk load process, I have a need where I need to query against this Map object. I haven't seen where anyone has done this and I'm wondering what kind of syntax I should use.
My select statement looks like this - I need help on the where clause
Code:
Map<String, String> zips = new Map<String, String>();
// Simplified example
zips.put('90210','IP');
Map<String, String> zips = new Map<String, String>();
// Simplified example
zips.put('90210','IP');
for(Zip_Code__c zip_code:[select Name, Sales_Rep__c from Zip_Code__c where Name in :zips])
{
// Some action code here
}
What I really want to do is something like:
select Name, Sales_Rep__c from Zip_Code__c where Name = zips.key AND IP_or_RP__c = zips.value
How do I do that?
If you "put" a record in that looked like <90210,'RP'> and then "put" one in that was <90210,'IP'> the second would overwrite the first in the map. So, you will need to address that differently.
Second, the way a Map works, you "put" the elements in, like you describe "zips.put('90210','IP')"
To retreive the value, you reference the index key in a get.
"zips.get('90210')" would return a string 'IP'.
The way you have zips referenced in your SOQL query, would work for a set or a list, to do this with a map, you would use the keyset() method to return an array of the keys.
Before you will be able to get to your last SOQL style, you will need to determine how to overcome your duplicate index issue. I thought in your previous question, your zips map had a key that was the zipcode and a value that was the reps name. Is there a possibility that more than one rep has the same zipcode? Is that situation you are trying to overcome?
What would be a way to handle the case where I have unique rows based on two pieces of data? Should I somehow merge them together?
Is there a different way perhaps to have parallel Lists or multidimensional arrays or something of the sort?
Any ideas?
So, in the typical map declaration, you might create something like:
map<ID, Integer> myMap = new map<Id, Integer>();
BUT, you could create a map like:
map<ID, List<Opportunity>> myMultiDimensionalMap = new map<ID, List<Opportunity>>();
Then, when you use a map object's static GET() method, an array of your objects will be returned (Opportunities in the example above).
list<Opportunity> myOppList = myMap.Get(myID);
Message Edited by kevinbnpower on 12-11-2008 11:09 AM
If I understand your issue, you need something like this:
Zip , IP, Rep (90210,'IP',005000000071yG5AAI)
Zip, RP, Different Rep (90210,'RP',005000000071xB2AII)
You will need to query the first map using the zip (90210 in our example), to get a second map that has the 2 division codes and appropriate userID listed.
Message Edited by JimRae on 12-11-2008 02:40 PM
This was interesting, and I was curious how to make it work myself.
So, this is what I came up with.
My Zip_Code object (Zip_Code__c) contains 3 fields of interest:
Name (the Zip Code itself)
Division (The RP/IP Value)
Sales_Rep (the user id of the Sales Rep with the territory)
I updated your original trigger, from the other issue, with a nested map to get the zip code and the division. What I don't know is how you get the division from the lead object to know which to take, that you will have to adjust yourself.
Here is a working trigger sample:
It's sort of a paynow/paylater issue. Perhaps as a natural procrastinator I lean towards 'pay later' ;)
To answer Jim's question, you'll want to assign the reference of the inner value to map that you can use as a 'holder'.
So:
tempMap = myMap.Get(ID);
value = tempMap.Get(whatever);
Because values are automatically passed by reference in APEX (modify a copy of a contact and you modify the original...), this assignment works as the tempMap comes to represent the data in the inner map.