I'm confusing myself with lists/maps. I want when criteria is met to put 4 values into a container. Id, Sales Participant Id(a lookup), Sales Participant Name, and an Integer. I have 3 separate Sales Participant lookup fields so if one is changed I want to add the id to the container. Finally, I want to take the container of the 4 values and generate a new record. Looping through each row in the container to create a new record.
How can I collect this data to pass to a handler? With a map or a list? In my current code I used a map which seems to only pull the two ids over but I cannot figure out how to get the lookup id to populate for the field.
Example Code:
public static void createSalesParticipant(Map<Id, Opportunity> newMap, Map<Id, Opportunity> oldMap)
{
List<
Map<Id, Id> salesParticipants = new Map<Id, Id>();
for(Opportunity oppy : newMap.values())
{
if(oppy.Sales_Participant_1__c != null && oldMap.get(oppy.Id).Sales_Participant_1__c != oppy.Sales_Participant_1__c)
{
salesParticipants.put(oppy.Id, oppy.Sales_Participant_1__c);
}
if(oppy.Sales_Participant_2__c != null && oldMap.get(oppy.Id).Sales_Participant_2__c != oppy.Sales_Participant_2__c)
{
salesParticipants.put(oppy.Id, oppy.Sales_Participant_2__c);
}
if(oppy.Sales_Participant_3__c != null && oldMap.get(oppy.Id).Sales_Participant_3__c != oppy.Sales_Participant_3__c)
{
salesParticipants.put(oppy.Id, oppy.Sales_Participant_3__c);
}
}
SalesParticipantHandler.createParticipant(salesParticipants);
}
Handler:
public with sharing class SalesParticipantHandler {
public static void createParticipant(Map<Id, Id> salesParticipants){
List<Sales_Participant__c> salesParticipantsToInsert = new List<Sales_Participant__c>();
for(Sales_Participant__c salesP : salesParticipants) {
salesParticipantsToInsert.add(new Sales_Participant__c(
Name = 'test',
Opportunity__c = salesP.Id,
Sales_Participant__c = salesP.Sales_Participant_1__c
));
}
insert salesParticipantsToInsert;
}
}