I am trying to solve the following problem: There is a text field "Product" on Opportunity object and I need to change the values of that and map it to picklist. There is already data in the system which means about 200 different values of Product field that, in the end, will be mapped to one of 5 picklist values according to mapping that I have already stored in object ProductMap__c.
Mapping example:
ProductMap__c.Former__c ProductMap__c.Actual__c
"Leasing KA" "Leasing"
"Leasing 100k" "Leasing"
I was thinking of creating a List of Opportunities and then somehow use a Map of ProductMap, find a value of Map and store it on the Opportunity.
Can you guys help me? I am quite new to Apex and I don't know how to continue or maybe my thoughts are completely wrong..
list<Opportunity> opps = [select id, name, Product__c from Opportunity where Product__c != ''];
for(Opportunity opp : opps){
...?Map of ProductMap?
Best Answer
Welcome to SF.SE Marting. A map contains a key and a value. What I'm gathering from your example is that you want to update values containing
ProductMap__c.Former__c
toProductMap__c.Actual__c
. I'm also gathering thatProductMap__c.Former__c
values will be unique, butProductMap__c.Actual__c
values will not. In a map, the keys need to be unique while the values do not. That means your map needs to be something likeYou'd use
ProductMap__c.Former__c
as the key andProductMap__c.Actual__c
as the value for each key when assembling the map. The pattern is to use mapname.put(key,value).When updating your Opportunities, I'd recommend your do your query as a Map. That way it looks like:
You can then do
You can also add them to a new list or whatever you wish in order to update the Opps. This is just to give you the concept. In the above, you need to get the key before you can reassign the value.
Edit
First, you need to declare the map properly as
map<string,string> pm
. But in this case, you're querying an object, so you'd need to get the list and then create the map. I'm guessing your map probably would be created by doing something like this:And yes, adding your values to a new list is a perfectly acceptable way to do what you wanted to do, however, using your code, you'd have wanted to do it as follows: