I have create a filed set on a custom object and trying to display the filed set to a VF page from apex code. This much is working fine but when i try to loop through the map in which the value is entered i am getting an error Invalid conversion from runtime type String to SOBJECT:. My code :
public Candidates__c ca_Insert {get; set;} // For inserting Candidates records
public blob Attach {get;set;} // For attaching Resume to the specific record
public String fileName {get; set;}
public Attachment a {get; set;}
public Id jobID {get; set;}// To get the ID from URL
public Boolean isSaved{get; set;}// To know is saved successfully
public List<Schema.FieldSetMember> candidateFields{get; set;}
public map<string, Candidates__c> fieldMap {get; set;}
public map<string, string> fieldMap_api {get; set;}
public Candidates__c field_disp {get;set;}
public ctrl_Candidate_Site(){
ca_Insert = new Candidates__c();
//ca_Insert = (Candidates__c)stdController.getRecord(); // getting records from VF page
jobID = ApexPages.currentPage().getParameters().get('jobId'); // Getting the jobId from URL
candidateFields = new List<Schema.FieldSetMember> ();
a = new Attachment();
candidateFields = Schema.SObjectType.Candidates__c.fieldSets.getMap().get('CMSR__Candidate').getFields() ;
system.debug('Fieldset values ' + candidateFields );
fieldMap = new map<string, Candidates__c>();
fieldMap_api = new map<string, string>();
field_disp = new Candidates__c ();
for(integer i =0; i < candidateFields.size(); i++ ){
system.debug('get field field ' + candidateFields[i] );
system.debug('get field Label ' + candidateFields[i].getLabel() );
system.debug('get field Required ' + candidateFields[i].getRequired());
fieldMap.put( candidateFields[i].getLabel(), field_disp );
fieldMap_api.put( candidateFields[i].getLabel(), candidateFields[i].getFieldPath() );
}
system.debug('Map fields' + fieldMap);
}
public PageReference SaveResult(){
system.debug('Value entered in map from Vf page ' + fieldMap);
for(string f : fieldMap.keyset()){
system.debug('map keyset Value ' + f);
system.debug('map entered Value ' + string.valueof(fieldMap.get(f)));
}
return null;
}
VF page:
<apex:page id="Cnd_page1" showHeader="false" controller="ctrl_Candidate_Site" >
<apex:pageMessages ></apex:pageMessages>
<style>
h1{
font-size:16px;
padding-left:50px;
font-family:'trebuchet ms', helvetica, sans-serif;
color:#3366FF;
line-height:20px;
}
.tableStyle{
padding-left:50px;
font-size:14px;
font-family:'trebuchet ms', helvetica, sans-serif;
color:#343434;
}
.cm_formCss{
height:100%;
margin:auto;
width:960px;
}
.tdStyleFirst{
height:25px;
margin-bottom:13px;
margin-left:2px;
width:50%;
}
.tdStyleSecond{
height:25px;
}
</style>
<apex:composition template="{!$Site.Template}" >
<apex:define name="body" >
<apex:form id="Cnd_form" >
<h1>Candidate</h1>
<table class="tableStyle">
<apex:repeat value="{!fieldMap}" var="f">
<tr>
<td class="tdStyleFirst">
<apex:outputText value="{!f}" /> </td>
<td> <apex:inputText value="{!fieldMap[f]}" /> </td>
</tr>
</apex:repeat>
<tr>
<td>Attach Resumes</td>
<td><apex:inputFile value="{!Attach}" accept="doc, docx, txt, pdf" filesize="1000" filename="{!fileName}"></apex:inputFile> </td>
</tr>
<tr>
<td></td>
<td> <apex:commandButton action="{!SaveResult}" value="Submit"/>
<apex:commandButton action="{!reset}" value="Reset" /> </td>
</tr>
</table>
</apex:form>
</apex:define>
can anybody guide me how to get it solved.
Best Answer
This line on visualforce page is reason:
fieldMap
is aMap
which returnssObject
type when you pass a key of typeString
and you are referencing it with<apex:input>
which acceptsString
Instead of directly assigning sObject
Candidate__c
assign its field value ex: