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>

        font-family:'trebuchet ms', helvetica, sans-serif;
        font-family:'trebuchet ms', helvetica, sans-serif;




<apex:composition template="{!$Site.Template}" >
 <apex:define name="body" > 
    <apex:form id="Cnd_form" >
          <table class="tableStyle">              
          <apex:repeat value="{!fieldMap}" var="f"> 


                     <td class="tdStyleFirst"> 
                          <apex:outputText value="{!f}"  />  </td> 
                     <td> <apex:inputText value="{!fieldMap[f]}" />  </td>                         

                      <td>Attach Resumes</td>
                      <td><apex:inputFile value="{!Attach}" accept="doc, docx, txt, pdf" filesize="1000" filename="{!fileName}"></apex:inputFile> </td>
                      <td> <apex:commandButton action="{!SaveResult}" value="Submit"/>
                           <apex:commandButton action="{!reset}" value="Reset" /> </td>


can anybody guide me how to get it solved.

  • Can you post visualforce page of which it is controller? – Ashwani Apr 15 '14 at 9:46
  • have edited the questions. – AnuRaj Apr 15 '14 at 10:10
  • Posted an answer. – Ashwani Apr 15 '14 at 10:43

This line on visualforce page is reason:

<apex:inputText value="{!fieldMap[f]}" />

fieldMap is a Map which returns sObject type when you pass a key of type String and you are referencing it with <apex:input> which accepts String

Instead of directly assigning sObject Candidate__c assign its field value ex:

<apex:inputText value="{!fieldMap[f].name}" />
| improve this answer | |
  • but if i use fieldMap[f].name the fields are coming from filed set so it is dynamic then how can i use static field. – AnuRaj Apr 15 '14 at 11:00
  • You can pass field api name directly into custom object instance. Syntax should be {!fieldMap[f][fieldAPIName]} @AnuRaj – Ashwani Apr 15 '14 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.