+ Start a Discussion
Amita TatarAmita Tatar 

dsiplayed fieldset values to save in an object as multiple records

Hi all,

I have a requirement where i want to save the fieldset displayed fields and their values in an object on save functionality.
But this can be like multiple records. I will post my code here. Please help me for save part.
Apex class:

public with sharing class SurveyClass{

Public Account acc{get;set;}
Public List<Proposal_Form__c>pfc;
Public Proposal_Form__c pfc1 {get;set;}
public ProposalFields__c pf {get;set;}
public List<String> fieldSet {get;set;}
public List<string> fieldnames{get;set;} 
Public ID rid;
public string msg {get;set;}
public Responses__c r {get;set;}
public Response_Details__c rd{get;set;}
public Id responseId;
private ApexPages.StandardController standardController;
    
public SurveyClass(ApexPages.StandardController controller) {
    this.standardController = standardController;
    rid = ApexPages.currentPage().getParameters().get('id');
    system.debug('*****rId****'+rid);
    acc = new Account();
    pfc1 = new Proposal_Form__c();
  }

public void Selected(){
    system.debug(pfc1.Service_Family__c +'------------'+pfc1.Sub_Service_Category__c);
    
   /* try{
        pfc = [SELECT Fields_Associated__c FROM Proposal_Form__c where Service_Family__c =: pfc1.Service_Family__c and Sub_Service_Category__c =: pfc1.Sub_Service_Category__c LIMIT 1];
        
    } catch(NullPointerException ex){
        
          Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL,'No such combination exists'+ex));
      
    } */
  
    pfc = new List<Proposal_Form__c>();
    pfc = [SELECT Fields_Associated__c FROM Proposal_Form__c where Service_Family__c =: pfc1.Service_Family__c and Sub_Service_Category__c =: pfc1.Sub_Service_Category__c LIMIT 1];

    if(!pfc.isEmpty())
    {
    fieldSet = new List<String>();
    List<String> fields = new List<String>();
     
    if(pfc[0].Fields_Associated__c != null && pfc[0].Fields_Associated__c != ''){
        fields = String.valueof(pfc[0].Fields_Associated__c).split(',');
        system.debug('*********Fields******'+fields);
    }
    else{
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'NO QUESTION SET FOUND'));
    }
   
    Map<String,String> labelMap = new Map<String,String>();
    labelMap = getLabel.retLabelMap('ProposalFields__c');
    system.debug(labelMap);
    system.debug(labelMap);
    for(String s : fields){
        fieldSet.add(labelMap.get(s));
        system.debug('********fieldset*******'+fieldSet);
    }    
  }
  else {
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'NO QUESTION SET FOUND'));
   }
 }  
 
  /*public List<Schema.FieldSetMember> getFields() {
        return SObjectType.ProposalFields__c.FieldSets.Red_Items.getFields();
    }*/
 
 
/* This method will store response of survey*/
public PageReference submitForm(){
    system.debug('Inside submitForm');
    acc = [SELECT Id, Name FROM Account where id =:rid];
    system.debug('*******acc****'+acc);
    Responses__c response = new Responses__c();
    response.Account__c = acc.Id;
    response.Service_Family__c = pfc1.Service_Family__c;
    response.Sub_Service_Category__c = pfc1.Sub_Service_Category__c;
    try{
        insert response;
        
        List<Response_Details__c> responseDetailsList = new List<Response_Details__c>();
        for(String s : fieldset){
             Response_Details__c rdetail = new Response_Details__c();
             //rdetail.Response__c = responseId;
             //fieldnames = new List<String>();
             //fieldnames.add(s.getFieldPath());  
             //rdetail.Question__c = fieldnames;
             responseDetailsList.add(rdetail);
        }
        insert responseDetailsList;
        PageReference pg = new PageReference('/'+response.id);     
        return pg;
    }
    catch(dmlexception e){
        apexpages.addmessages(e);
        return null;
}
}

/*public void getResponseDetails(Id responseId){

List<Response_Details__c> rDetail = new List<Response_Details__c>();
Response_Details__c responseDetail = new Response_Details__c();
responseDetail.Response__c = responseId;
for (Integer f=0; f< fieldSet.size();f++){
    responseDetail.Question__c = fieldSet[f];
    system.debug( responseDetail.Question__c);
}
rDetail.add(responseDetail);
insert rDetail;
} */ 

}


PagE:



<apex:page standardController="Account" extensions="SurveyClass" sidebar="false" showHeader="false" tabStyle="Account">
<apex:form >
<apex:pageMessages rendered="true" id="errMsg" showDetail="false"/>
<apex:pageBlock title="Customer Survey Form"> 

<apex:pageBlockSection title="Service Requirements" >
<apex:inputField value="{!pfc1.Service_Family__c}"/>
<apex:pageblocksectionItem >
<apex:outputLabel value="Sub Service Category"/>
<apex:outputPanel >
        
<apex:inputfield value="{!pfc1.Sub_Service_Category__c}">
<apex:actionSupport event="onchange" action="{!Selected}" rerender="fieldst,errMsg" />
</apex:inputField>
       
</apex:outputPanel>
</apex:pageblocksectionItem>
</apex:pageBlockSection> 

<apex:pageblockSection title="Question Set" id="fieldst">
<apex:repeat value="{!fieldSet}" var="f">
<apex:inputField value="{!pf[f]}"/> 
</apex:repeat>
</apex:pageblockSection>

<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Submit" action="{!submitForm}"/>
<apex:commandButton value="Edit" action="{!edit}"/>
</apex:pageBlockButtons>

</apex:pageBlock>
</apex:form> 
</apex:page>

I want to save the particular fieldset fields and user entered values from vf page to response detail object in a question and answer field.
Please help.

Amita Tatar