0

I just want to insert the value for the selected field name of the selected object. I wrote the code but i am still little bit confused to write the code regarding the insert operation. please help me to solve this.

Apex code

public class DFieldInsertController {
public String selectedObject { get; set; }
public String selectedField { get; set; }
public String fieldValue {get; set;}
public String dataToEnter {get; set;}
public SelectOption[] getObjectOptions() {
    SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
    for(SObjectType sType: Schema.getGlobalDescribe().values()) {
        DescribeSObjectResult res = sType.getDescribe();
        results.add(new SelectOption(res.getName(), res.getLabel()));
    }
    return results;
}
public SelectOption[] getFieldOptions() {
    List<SelectOption>  options = new List<SelectOption>();
    if(selectedObject != null) {
    Map <String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap();        
    Map<String, String> preDefinedMap = new Map<String, String>();
    for(Schema.SObjectField sfield : fieldMap.Values()){
        String strType = string.valueOf(fieldMap.get(sfield.getdescribe().getname()).getDescribe().getType());
        if(strType == 'STRING' && sfield.getdescribe().isAccessible() ==  true && 
           sfield.getdescribe().isUpdateable() == true) {
               options.add(new SelectOption(sfield.getdescribe().getname(), sfield.getdescribe().getlabel()));
           }
    }
    }    
    return options;
   /* SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
    if(selectedObject != null) {
        for(SObjectField sField: Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap().values()) {
            DescribeFieldResult res = sField.getDescribe();
            results.add(new SelectOption(res.getName(), res.getLabel()));
        }
    }
    return results;*/
}
public void saveData(){
    //Pagereference pageRef = new PageReference();
    //return pageRef

}

}

Visualforce page

<apex:page controller="DFieldInsertController">
<apex:form id="form">
    <table>
     <tr>
            <th> Objects to select </th>
            <th> Enter Account Name</th>
            <th> Select Field </th>
            <th> Enter Value for field </th>
            <th> Action </th>
        </tr>
        <tr>
            <td>
                <apex:selectList value="{!selectedObject}" size="1">
                    <apex:selectOptions value="{!objectOptions}" />
                    <apex:actionSupport reRender="form" event="onchange" />
                </apex:selectList>
            </td>
            <td>
                <apex:inputText value="{!dataToEnter}"></apex:inputText>
            </td>
            <td>
                <apex:selectList value="{!selectedField}" size="1">
                    <apex:selectOptions value="{!fieldOptions}" />
                </apex:selectList>
            </td>
            <td>
              <apex:inputText value="{!fieldValue}"></apex:inputText>
            </td>
            <td>
              <apex:commandButton action="{!saveData}" value="Save"/>
            </td>
    </tr>   
    </table>
</apex:form>

output

output should displayed as given image

Thank you in advance

  • Use the sobject class ‘put’ method – cropredy Jul 9 '19 at 11:06

Your Answer

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

Browse other questions tagged or ask your own question.