2

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;}
        }


    }
2

I believe your issue lays in this section of your code:

              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; 

The problem is that you need to be creating a list of CustomE__c records for each unique CustomC__c record. Once you have that, you then put the values into a Map of CustomC__c.ID to list<Id> of CustomE__c records so you can later relate the CustomD__c records to the appropriate child CustomE__c records. Your parent tempCd.Id; must be the same for all of those records. Your method above doesn't allow you to do that; thus the need for a map to track them.

I'll leave it to you to figure out how to best adjust your code to implement this type of mapping in your code.

EDIT:

Example of how to do what you need:

In your code posted above you have a section where you add the queried CustomC__c records to a wrapper that resulted from the CustomB__c record selection:

        for(Customc__c customcloop : cc1) {

            tempWrapper.add(new wrapper1(false,customcloop));
        }

This is where you also need to create a map of those results with CustomB__c. Currently, you use the method below to return list<CustomC__c>cc1. It's what you need to use to get what you want:

    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;
    }

You can modify the initial for loop above to put create the map that you need to create of CustomBIds to CustomCIds since there can be a many to one relationship between them.

        map<Id,Id>bIdToCId = new map<Id,Id>();
        list<Id>custCIds = new list<id>();
        Integer i = 0;
        Id tempBId = new Id();
        Id tempCId = new Id();

        for(Customc__c customcloop : cc1) {

             tempWrapper.add(new wrapper1(false,customcloop));

             bIdToCId.put(get.customcloop.customB__c,CustCIds);

             If(customcloop.Id != tempCId && customcloop.customB__c != tempBId)
             {
                // clear the list
                custCIds.clear();
                i=0;
             }

             custCIds.Add(customcloop.Id)

             If(i==0){
                 // set temp variables to current iteration values
                 tempBId = customcloop.customB__c;
                 tempCId = customcloop.Id;
             }else if(i!=0 && customcloop.customB__c == tempBId && customcloop.Id != tempCId){
                 tempCId = customcloop.customC__c;                  
             }else if(i!=0 && customcloop.customB__c != tempBId && customcloop.Id != tempCId){
                 tempBId = customcloop.customB__c;
                 tempCId = customcloop.customC__c;
             }
             // iterate value of i
             i++;
        }

The above isn't the cleanest way of creating the map and I'm confident someone else has a really slick way of doing it, but this is the way I know how to do it. Once you have the map, you can use it to test to see if there's more than one value of C for every B and map those for when you're creating your D and E records since they should correlate. For every C record related to B, there will be an equivalent E record related to D. The map will preserve that relationship for you when creating those records.

  • can you show me an example to how to do this? – Gaynerys Nov 22 '16 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.