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
Amita TatarAmita Tatar 

Save Account survey questions in a child object of Account?

Hi all,

I have a VF page survey form for account which has certain questions. Each account can have multiple survey forms as per services availed.
I want to save these survey forms in a child object of Account.
How i should achieve this part?
Here is my code.
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 ID rid;
public string msg {get;set;}
private ApexPages.StandardController standardController;
    
public SurveyClass(ApexPages.StandardController controller) {
    this.standardController = standardController;
    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'));
   }
 }  
}


VF 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="{!save}"/>
<apex:commandButton value="Edit" action="{!edit}"/>
</apex:pageBlockButtons>

</apex:pageBlock>
</apex:form> 
</apex:page>
Thanks,
Amita
 
DeepVDeepV
You need add the save method to your apex class:

PageReference save(){
customobject__c  C = new customobject__c ();
C.FieldName__c = FieldName; // map all the object fields  with the visualforce input values
insert C; // this will add a new record to customobject in salesforce
return null;
}
Amita TatarAmita Tatar
Hi DeepV,

Please tell me what i should modify in below code
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'));
   }
 }  
 
/* 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>();
       // Response_Details__c rdetail = new Response_Details__c();
        for(String s : fieldset){
             Response_Details__c rdetail = new Response_Details__c();
             rdetail.Response__c = responseId;
             rdetail.Question__c = fieldset[0];
            // rdetail.Answer__c = ;
             responseDetailsList.add(rdetail);
        }
        insert responseDetailsList;
        PageReference pg = new PageReference('/'+response.id);     
        return pg;
    }
    catch(dmlexception e){
        apexpages.addmessages(e);
        return null;
}
}
I want to insert question and answers in as multiple records in responsedetail object. How do i achieve it?

Regards,
Amita Tatar