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 

Insert records from wrapper class

I can't figure out why the insert function isn't working. Can anyone help me? Thank you so much.

CONTROLLER:

public class ApartmentMapLister {

    public Map_Lead__c MapLead {get;set;}
    public Map_Point__c MapPoint {get;set;}
    public Map<Integer, Map_Point__c> ApartmentMap {get;set;}
    Public List<Wrapper> Wrapperlist {get;set;}

    public ApartmentMapLister() {
        MapLead = [SELECT ID, NAME, First_Name__c, Last_Name__c FROM Map_Lead__c WHERE ID=:ApexPages.currentPage().getParameters().get('id')];
    }    

    public Map<Integer, Map_Point__c> getonebedadmin() {

       Wrapperlist = new List<Wrapper>();
       
       ApartmentMap = new Map<Integer, Map_Point__c>();
       List<Map_Point__c> ApartmentList = [SELECT Id, Name, IsSelected__c FROM Map_Point__c Order By Name];

        for(Map_Point__c mp: ApartmentMap.values()) {
            Wrapper wrap = new Wrapper();
            wrap.mappoint = mp;
            Wrapperlist.add(wrap);
            }
                    
       for(Integer index = 0 ; index < ApartmentList.size() ; index++) {
           ApartmentMap.put(index, ApartmentList[index]);
       }
       return ApartmentMap;
    }

    public class Wrapper{
    
        public boolean isSelected {get;set;}
        public Map_Point__c mappoint {get;set;}
               
    }
    
    public void InsertApartment(){
        
        for(Wrapper wr : Wrapperlist){
        
            if(wr.mappoint.isSelected__c == true){
                
        Map_List__c ml = new Map_List__c();
            ml.Map_Lead__c=ApexPages.currentPage().getParameters().get('id');
            ml.Map_Point__c=wr.mappoint.ID;
            ml.Unique__c=(MapLead.NAME + wr.mappoint.AIMid__c);
            insert ml;
            }
        }
    }
    

      public PageReference SaveRecords(){
    
        List<Map_Point__c> records = new List<Map_Point__c>();
        for(Map_Point__c df : ApartmentMap.values()) {
            records.add(df);
        }
        // Bulk update with one DML call
        update records;
        return null;
      }
}


VISUALFORCE PAGE:
<apex:page sidebar="false" showHeader="false" cache="true" id="page" docType="HTML" readOnly="false" controller="ApartmentMapLister">
<apex:form >
    {!MapLead.First_Name__c} {!MapLead.Last_Name__c}
    
    <apex:pageBlock >
       <apex:pageBlockButtons location="top">
           <apex:commandButton value="Save" action="{!InsertApartment}"/>
       </apex:pageBlockButtons>
       <apex:pageBlockTable value="{!onebedadmin}" var="Map_Point__c">
            <apex:column >
                <apex:inputField value="{!onebedadmin[Map_Point__c].IsSelected__c}"/>
            </apex:column>
            <apex:column >
                <apex:inputField value="{!onebedadmin[Map_Point__c].Name}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>
Best Answer chosen by jls_74_tx
Abdul KhatriAbdul Khatri
Hi jls_74_tx

I am sorry but that cannot be true. I tried my code and it worked with no issues. You are just adding comlexity in the code with that wrapper. You don't need that. 

For your information, sharing the screen shot. I have three records in my Map Point and I only selected two so it created only 2 Map List against that lead

User-added image

Total Map Point Records
User-added image
Sorry I don't give solutions without being tested.

All Answers

Abdul KhatriAbdul Khatri
Hi jls_74_tx

I don't think you need a wrapper class so I simplified the controller
 
public class ApartmentMapLister {

    public Map_Lead__c MapLead {get;set;}
    public Map_Point__c MapPoint {get;set;}
    public Map<Integer, Map_Point__c> ApartmentMap {get;set;}

    public ApartmentMapLister() {
    
        MapLead = [SELECT ID, NAME, First_Name__c, Last_Name__c FROM Map_Lead__c WHERE ID=:ApexPages.currentPage().getParameters().get('id')];
    }    

    public Map<Integer, Map_Point__c> getonebedadmin() 
    {
       
       ApartmentMap = new Map<Integer, Map_Point__c>();
       List<Map_Point__c> ApartmentList = [SELECT Id, Name, AIMid__c, IsSelected__c FROM Map_Point__c Order By Name];
                   
       for(Integer index = 0 ; index < ApartmentList.size() ; index++) 
       {
           ApartmentMap.put(index, ApartmentList[index]);
       }
       
       return ApartmentMap;
    }
    
    public void InsertApartment()
    {
        for(Map_Point__c wr : ApartmentMap.values())
        {       
            if(wr.isSelected__c == true)
            {
                Map_List__c ml = new Map_List__c();
                ml.Map_Lead__c = ApexPages.currentPage().getParameters().get('id');
                ml.Map_Point__c = wr.ID;
                ml.Unique__c = (MapLead.NAME + wr.AIMid__c);
                insert ml;
            }
        }
    }
    
}

I hope this help
ravi soniravi soni
hi ,
I have made some changes in your code.
here is your apex controller.
public class ApartmentMapLister {

    public Map_Lead__c MapLead {get;set;}
    public Map_Point__c MapPoint {get;set;}
    public Map<Integer, Map_Point__c> ApartmentMap {get;set;}
    Public List<Wrapper> Wrapperlist {get;set;}

    public ApartmentMapLister() {
        MapLead = [SELECT ID, NAME, First_Name__c, Last_Name__c FROM Map_Lead__c WHERE ID=:ApexPages.currentPage().getParameters().get('id')];
    }    

    public Map<Integer, Map_Point__c> getonebedadmin() {

       Wrapperlist = new List<Wrapper>();
       
       ApartmentMap = new Map<Integer, Map_Point__c>();
       List<Map_Point__c> ApartmentList = [SELECT Id, Name, IsSelected__c,AIMid__c FROM Map_Point__c Order By Name];
	   
	   for(Integer index = 0 ; index < ApartmentList.size() ; index++) {
           ApartmentMap.put(index, ApartmentList[index]);
       }

        for(Map_Point__c mp: ApartmentMap.values()) {
            Wrapper wrap = new Wrapper();
            wrap.mappoint = mp;
            Wrapperlist.add(wrap);
            }
                    
       
       return ApartmentMap;
    }

    public class Wrapper{
    
        public boolean isSelected {get;set;}
        public Map_Point__c mappoint {get;set;}
               
    }
    
    public void InsertApartment(){
        
        for(Wrapper wr : Wrapperlist){
        
            if(wr.mappoint.isSelected__c == true){
                
        Map_List__c ml = new Map_List__c();
            ml.Map_Lead__c=ApexPages.currentPage().getParameters().get('id');
            ml.Map_Point__c=wr.mappoint.Id;
            ml.Unique__c=(MapLead.NAME + wr.mappoint.AIMid__c);
            insert ml;
            }
        }
    }
    

      public PageReference SaveRecords(){
    
        List<Map_Point__c> records = new List<Map_Point__c>();
        for(Map_Point__c df : ApartmentMap.values()) {
            records.add(df);
        }
        // Bulk update with one DML call
		if(records.size() > 0){
		update records;
		}
        
        return null;
      }
}

try above code. I believe it will help you.
don't forget to mark it as best answer.
Thank you
 
mukesh guptamukesh gupta
Hi,

Your code is correct but mistake in code flow:
In your code your was trying to add values in  ApartmentMap before SOQL that's why no value added in this Map ,

So please use below code 
 
public class ApartmentMapLister {

    public Map_Lead__c MapLead {get;set;}
    public Map_Point__c MapPoint {get;set;}
    public Map<Integer, Map_Point__c> ApartmentMap {get;set;}
    Public List<Wrapper> Wrapperlist {get;set;}

    public ApartmentMapLister() {
        MapLead = [SELECT ID, NAME, First_Name__c, Last_Name__c FROM Map_Lead__c WHERE ID=:ApexPages.currentPage().getParameters().get('id')];
    }    

    public Map<Integer, Map_Point__c> getonebedadmin() {

       Wrapperlist = new List<Wrapper>();
       
       ApartmentMap = new Map<Integer, Map_Point__c>();
       List<Map_Point__c> ApartmentList = [SELECT Id, Name, IsSelected__c FROM Map_Point__c Order By Name];
	
       for(Integer index = 0 ; index < ApartmentList.size() ; index++) {
           ApartmentMap.put(index, ApartmentList[index]);
       } 
        
        for(Map_Point__c mp: ApartmentMap.values()) {
            Wrapper wrap = new Wrapper();
            wrap.mappoint = mp;
            Wrapperlist.add(wrap);
            }
                    
       
       return ApartmentMap;
    }

    public class Wrapper{
    
        public boolean isSelected {get;set;}
        public Map_Point__c mappoint {get;set;}
               
    }
    
    public void InsertApartment(){
        
        for(Wrapper wr : Wrapperlist){
        
            if(wr.mappoint.isSelected__c == true){
                
        Map_List__c ml = new Map_List__c();
            ml.Map_Lead__c=ApexPages.currentPage().getParameters().get('id');
            ml.Map_Point__c=wr.mappoint.ID;
            ml.Unique__c=(MapLead.NAME + wr.mappoint.AIMid__c);
            insert ml;
            }
        }
    }
    

      public PageReference SaveRecords(){
    
        List<Map_Point__c> records = new List<Map_Point__c>();
        for(Map_Point__c df : ApartmentMap.values()) {
            records.add(df);
        }
        // Bulk update with one DML call
        update records;
        return null;
      }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
jls_74_txjls_74_tx
Thank you to everyone who responded. The first two suggestions didn't work unfortuneately. Mukesh, yours fired but it only inserted one record into Map_List__c. If IsSelected__c = true on multiple records, I want all of them inserted. So the insert statement is working, but it's not reading over multiple records. Can you please help? It's something with the wrapper. Thank you in advance!
Abdul KhatriAbdul Khatri
Hi jls_74_tx

I am sorry but that cannot be true. I tried my code and it worked with no issues. You are just adding comlexity in the code with that wrapper. You don't need that. 

For your information, sharing the screen shot. I have three records in my Map Point and I only selected two so it created only 2 Map List against that lead

User-added image

Total Map Point Records
User-added image
Sorry I don't give solutions without being tested.
This was selected as the best answer
jls_74_txjls_74_tx
Thank you so much for the additional clarification. This was my mistake. I had a trigger that was deleting the Map_List__c after insert so it appeared it wasn't inserting all of my records. I deactivated the trigger and it works as expected. Thank you so much for reducing my code. I wasn't sure if the Wrapper was best practices, but I didn't know any other way to itterate over the records. I really appreciate your help, thank you so much for the assistance.