I have a page involving 5 custom objects:
CustomA__c, CustomB__c, CustomC__c, CustomD__c, and CustomE__c
And here is the relationship among these objects:
M-D : CustomA__c & CustomD__c
M-D : CustomB__c & CustomC__c
M-D : CustomD__c & CustomE__c
The process starts on a search page using CustomA__c
as the standardcontroller. It has a lookup field named CustomB__c
that looks for a certain CustomB__c
record and all its related Customc__c
records.
A new CustomE__c
empty field displays automatically for each selected Customc__c
record.
On save()
method, new CustomA__c
, CustomD__c
, and CustomE__c
records will be created.
All of these are successfully inserting but I noticed that every time there are more than 1 CustomD__c
and CustomE__c
records are created only the last CustomD__c
id is mapped to CustomE__c
records. Specifically, I need to map the ids of the newly-inserted CustomD__c
records to CustomD__c
fields from the CustomE__c
object.
Here's the controller:
public class MainController {
private ApexPages.StandardController controller;
public CustomA__c custARecord {get;set;}
public CustomB__c custBqry {get;set;}
public List<wrapper1> wrapperList1 {get;set;}
public List<wrapper2> wrapperList2 {get;set;}
public List<wrapper3> wrapperList3 {get;set;}
public List<wrapper1> tempWrapper {get;set;}
public List<CustomA__c> ca1 {get;set;}
public List<CustomD__c> cd1 {get;set;}
public List<wrapper3> w3 {get;set;}
public List<Customc__c> cc1 {get;set;}
public CustomA__c NewCustomA {get;set;}
public CustomD__c tempCd {get;set;}
public List<Customc__c> selectedCustomc = new List<Customc__c>();
public MainController(ApexPages.StandardController con) {
controller = con;
wrapperList1 = new List<wrapper1>();
wrapperList3 = new List<wrapper3>();
custARecord = new CustomA__c();
custBqry = new CustomB__c();
ca1 = new List<CustomA__c>();
cd1 = new List<CustomD__c>();
w3 = new List<wrapper3>();
}
public void init() {
List<Customc__c> cc1 = new List<Customc__c>();
List<wrapper1> tempWrapper = new List<wrapper1>();
List<Customc__c> selectedCustomcItems = new List<Customc__c>();
if(custARecord.CustomB__c != null) {
custBqry = [SELECT Id, Name FROM CustomB__c WHERE Id=:custARecord.CustomB__c];
cc1 = getCustomcItems();
}
else {
custARecord.clear();
wrapperList1.clear();
}
for(Customc__c customcloop : cc1) {
tempWrapper.add(new wrapper1(false,customcloop));
}
if(tempWrapper != null) {
wrapperList1 = tempWrapper;
}
}
public List<Customc__c> getCustomcItems() {
List<Customc__c> cc1 = new List<Customc__c>();
cc1 = [SELECT Id, CustomB__c, Field1__c, Field2__c FROM Customc__c WHERE CustomB__c =: custARecord.CustomB__c];
return cc1;
}
public PageReference newMethod() {
getSelected();
GetSelectedCustomcItems()
createNewCustomE();
return null;
}
public PageReference getSelected() {
selectedCustomc.clear();
w3.clear();
for(wrapper1 rwrap : wrapperList1) {
if(rwrap.IsSelected == true) {
selectedCustomc.add(rwrap.customclist);
}
}
return null;
}
public List<Customc__c> GetSelectedCustomcItems() {
return selectedCustomc;
}
public void createNewCustomE() {
if(selectedCustomc.size() > 0) {
for(Customc__c customclist : selectedCustomc) {
List<wrapper3> temp_customclist = new List<wrapper3>();
wrapper3 newRec = new wrapper3();
CustomE__c newCustomE = new CustomE__c();
newRec.ce = newCustomE;
newRec.index2 = temp_customclist.size();
temp_customclist.add(newRec);
for(wrapper3 rwloop : temp_customclist) {
w3.add(rwloop);
}
}
}
}
public PageReference save() {
List<CustomA__c> caList = new List<CustomA__c>();
Map<Id, CustomA__c> caMap = new Map<Id, CustomA__c>();
List<wrapper1> caList2 = new List<wrapper1>();
List<CustomD__c> cdList = new List<CustomD__c>();
List<CustomE__c> ceList = new List<CustomE__c>();
CustomA__c NewCustomA;
CustomD__c tempCd;
Set<Id> newIds = new Set<Id>();
for(wrapper1 rwraploop : wrapperList1) {
if(rwraploop.IsSelected) {
if(rwraploop.customclist.CustomB__c != null) {
NewCustomA = new CustomA__c();
NewCustomA.Date__c = custARecord.Date__c;
NewCustomA.CustomB__c = rwraploop.customclist.CustomB__c;
caMap.put(rwraploop.customclist.CustomB__c, NewCustomA);
}
caList2.add(rwraploop);
}
}
if(!caMap.isEmpty()) {
insert caMap.values();
if(caList2 != null && !caList2.IsEmpty()) {
List<Customc__c> newCcList = new List<Customc__c>();
for(wrapper1 rec1 : caList2) {
newCcList.add(rec1.customclist);
}
for(Customc__c rec1 : newCcList) {
tempCd = new CustomD__c();
tempCd.Field1__c = rec1.Field1__c;
tempCd.Field2__c = rec1.Field2__c;
tempCd.CustomA__c = caMap.values().Id;
tempCd.Customc__c = rec1.Id;
cdList.add(tempCd);
}
if(cdList != null && !cdList.isEmpty()) {
insert cdList;
}
for(wrapper3 rec2 : w3) {
CustomE__c ce2 = new CustomE__c();
ce2.Field1__c = rec2.ce.Field1__c;
ce2.Field2__c = rec2.ce.Field2__c;
ce2.CustomA__c = NewCustomA.Id;
ce2.CustomD__c = tempCd.Id;
ceList.add(ce2);
}
insert ceList;
}
Set<Id> ids = new Set<Id>();
for(CustomA__c idl : caList) {
ids.add(idl.Id);
}
List<CustomA__c> caqry = [SELECT Id, Name, Date__c, CustomB__c FROM CustomA__c WHERE ID IN : ids];
ca1 = caqry;
}
PageReference pg = new PageReference('/' + NewCustomA.Id);
pg.setRedirect(true);
return pg;
}
public class wrapper1 {
public Customc__c customclist {get;set;}
public Boolean IsSelected {get;set;}
public wrapper1(Boolean is, Customc__c cc3) {
IsSelected = is;
customclist = cc3;
}
}
public class wrapper2 {
public CustomD__c r {get;set;}
public Integer index {get;set;}
}
public class wrapper3 {
public CustomE__c ce {get;set;}
public Integer index2 {get;set;}
}
}