function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jls_74_txjls_74_tx 

Wrapper Class Dynamic Record ID

I can't figure out how to insert related record ID numbers dynamically. In the sample below they are hard coded and it works as needed, but I need the ID's included in the loop to insert records.

Thank you in advance for you help! 


public class DataListingController {

    public String CID;
    public List<Wrapper> Wrapperlist {get;set;}

    public DataListingController(){
    
        CID = ApexPages.currentPage().getParameters().get('id');

        //Fetch Referrals
        List<Data_Feed__c> dfList = new List<Data_Feed__c>();

        Wrapperlist = new List<Wrapper>();

        dfList = [SELECT ID, NAME, Rental__r.NAME, Data_Client__r.ID FROM Data_Feed__c WHERE Data_Client__c=:CID ORDER BY Rental__r.NAME];
        
            if(!dfList.isEmpty()){
                
                for(Data_Feed__c df: dfList) {
                
                    Wrapper wrap = new Wrapper();
                    wrap.isSelected = FALSE;
                    wrap.dfeedObj = df;
                    Wrapperlist.add(wrap);
                }
            }       
        }

    /* Insert Apartment functionality based on selected records
    */
    
    public void InsertApartment(){
        
        for(Wrapper wr : Wrapperlist){
        
            if(wr.isSelected = true){
                
                Data_Listing__c dl = new Data_Listing__c();
                    dl.Data_List__c='a0g55000000S7Cs';
                    dl.Data_Feed__c='a0m55000001Ihno';
                    insert dl;
            }
        }
    }
    
    /* Wrapper class with checkbox and Apartment object. 
    this is also  called as inner class 
    */

    public class Wrapper{
    
        public boolean isSelected {get;set;}
        public Data_Feed__c dfeedObj {get;set;}
        public Data_List__c dlistObj {get;set;}
               
    }
     
}
Best Answer chosen by jls_74_tx
Meghna Vijay 7Meghna Vijay 7
Hi,

First take a deep breath :) second :-

public void InsertApartment(){
        
        for(Wrapper wr : Wrapperlist){
        
            if(wr.isSelected = true){
                
                Data_Listing__c dl = new Data_Listing__c();
                   //dl.Data_List__c='a0g55000000S7Cs';
                    dl.Data_Feed__c= wr.dfeedObj.Id;
                    insert dl;
            }
        }
    }
Half of the problem will be solved, the other half can be only solved if you just tell me how to get the id of data list like you used soql to fetch 
dfList = [SELECT ID, NAME, Rental__r.NAME, Data_Client__r.ID FROM Data_Feed__c WHERE Data_Client__c=:CID ORDER BY Rental__r.NAME]; 
Is there any soql or anything which is mentioned in the requirement from where i can get data list or it's set of id's or anything?

Thanks

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi,
Can you tell me the relationship between data feed and data list? How will you be fetching data list object?
Thanks
jls_74_txjls_74_tx
Data_Listing__c has a lookup relationship with Data_List__c and Data_Feed__c. I'm not sure how to call the Data List object. I'm getting lost in how to relate all the objects together. 
jls_74_txjls_74_tx
I found this example online that brings 3 custom objects together (similar to my requirement), but I can't get it to work. There are too many errors in the sample.

https://developer.salesforce.com/forums/?id=906F0000000Ae2GIAS
Meghna Vijay 7Meghna Vijay 7
Hi,

First take a deep breath :) second :-

public void InsertApartment(){
        
        for(Wrapper wr : Wrapperlist){
        
            if(wr.isSelected = true){
                
                Data_Listing__c dl = new Data_Listing__c();
                   //dl.Data_List__c='a0g55000000S7Cs';
                    dl.Data_Feed__c= wr.dfeedObj.Id;
                    insert dl;
            }
        }
    }
Half of the problem will be solved, the other half can be only solved if you just tell me how to get the id of data list like you used soql to fetch 
dfList = [SELECT ID, NAME, Rental__r.NAME, Data_Client__r.ID FROM Data_Feed__c WHERE Data_Client__c=:CID ORDER BY Rental__r.NAME]; 
Is there any soql or anything which is mentioned in the requirement from where i can get data list or it's set of id's or anything?

Thanks
This was selected as the best answer
jls_74_txjls_74_tx
Thank you so much! That worked perfectly!! I'm pretty good at writing classes, but wrappers always confuse me. Too many abbreviations. LOL

I'm still deciding what I'm going to do about Data_List__c. I'm testing some ideas, but now that you've showed me the path, I think I can figure it out.

Thanks again!!