You need to sign in to do that
Don't have an account?
jls_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>
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>
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
Total Map Point Records
Sorry I don't give solutions without being tested.
All Answers
I don't think you need a wrapper class so I simplified the controller
I hope this help
I have made some changes in your code.
here is your apex controller.
try above code. I believe it will help you.
don't forget to mark it as best answer.
Thank you
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
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
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
Total Map Point Records
Sorry I don't give solutions without being tested.