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

Help with inserting records from Map
This should be simple, but I can complete this class that inserts records from a Map
I've filed up a map that contains Contact ID's, Dates and a Unique Number called MRN__c. Now I am trying to insert records that are filled with in the map into the Referral_Entry__c object at the end of this class.
Any suggestions? I highlighted where I am having issues below:
//map to store key month. only including one date for now that corresponds with the one records
//list contact from query above
list<Contact> cons = (list<Contact>)scope;
set<Id> conids = new set<Id>();
for (Contact con : cons) conids.add(con.Id);
list<Contact> fullcontacts = [select Id, LastName,
(select Service_Date__c, MRN__c from Referral_Entries_del__r)
from Contact where Id in :conids];
map<Id, map<Date, set<Decimal>>> conid2datemapmap = new map<Id, map<Date, set<Decimal>>>();
for (Id conid : conids)
{
conid2datemapmap.put(conid, new map<Date, set<Decimal>>());
}
for (Contact con : fullcontacts)
{
//get con map to set of MRNs
//run refe loop
//- set of MRN's for individual Physician for that month
map<Date, set<Decimal>> mrns = conid2datemapmap.get(con.Id);
list<Referral_Entry__c> refs = con.Referral_Entries_del__r;
/*
[Select Id, Service_Date__c, Referring_Physician_del__c, MRN__c
FROM Referral_Entry__c
Where Referring_Physician_del__c = :cons[0].Id])
*/
for (Referral_Entry__c refe : refs)
{
totalprocessed ++;
//Find first of month
Date StartOfMonth = refe.Service_Date__c.toStartOfMonth();
//get MRNs already entered for this month
set<Decimal> theseMRNs = mrns.get(StartOfMonth);
//If there are no MRNs already entered for this month
if (theseMRNs == null)
theseMRNs = new set<Decimal>();
//Add this MRN to set of MRNs for this month
if (refe.MRN__c != null)
theseMRNs.add(refe.MRN__c);
//put set in map
mrns.put(StartOfMonth, theseMRNs);
}
//put map back into con2mrnmap
conid2datemapmap.put(con.Id, mrns);
//set up debug log
debuglog += '<br/>new contact ' + con.Id;
for (Date d : mrns.keyset())
{
debuglog += '<br/>' + d + ' ' + mrns.get(d);
}
} //end for Contact con : fullcontacts
//Delete existing Summary records, which are records without Null values in the Count of MRN field
List<Referral_Entry__c> sumrectodelete = [Select Id, Service_Date__c, Referring_Physician_del__c, MRN__c, Count_of_MRN__c
FROM Referral_Entry__c
Where Count_of_MRN__c != Null];
delete sumrectodelete;
//Insert New Summary records
List<Referral_Entry__c> newsumrecords = new List<Referral_Entry__c>();
for (Contact con : fullcontacts)
{
for (Date d : mrns.keyset())
{
newsumrecords.Referring_Physician_del__c = con.Id;
}
}
Thanks Mike, that is the help I was looking for. I ended up figuring it out, but it was along the same lines as you suggested, below is my code that pulled the values that were put into the maps:
List<Referral_Entry__c> newsumrecords = new List<Referral_Entry__c>();
for (Contact con : fullcontacts)
{
map<Date, set<Decimal>> mrns = conid2datemapmap.get(con.Id);
for (Date d : mrns.keyset())
{
Referral_Entry__c refent = new Referral_Entry__c(
Referring_Physician_del__c = con.Id, FSC__c = 'None', Service_Date__c = d, Count_of_MRN__c = mrns.get(d).size());
newsumrecords.add(refent);
debuglog += '<br/>' + (newsumrecords);
}
}
insert newsumrecords;
All Answers
I'm not sure exactly what you are looking for, but I think you need help iteratoring over the items in the map? You can iterate over all the entries in the map like this.
Thanks Mike, that is the help I was looking for. I ended up figuring it out, but it was along the same lines as you suggested, below is my code that pulled the values that were put into the maps:
List<Referral_Entry__c> newsumrecords = new List<Referral_Entry__c>();
for (Contact con : fullcontacts)
{
map<Date, set<Decimal>> mrns = conid2datemapmap.get(con.Id);
for (Date d : mrns.keyset())
{
Referral_Entry__c refent = new Referral_Entry__c(
Referring_Physician_del__c = con.Id, FSC__c = 'None', Service_Date__c = d, Count_of_MRN__c = mrns.get(d).size());
newsumrecords.add(refent);
debuglog += '<br/>' + (newsumrecords);
}
}
insert newsumrecords;