This question is related to more or less that I have asked here

My question is, how to get the field value from sObject to little demo you here.

Here is my vfp:

<apex:page controller="dynamicApexController" >
    <apex:form id="theFrm">
        <apex:pageMessages ></apex:pageMessages>

        <apex:pageBlock title="Dynamic" id="pb1"   > 
            <apex:pageBlockSection title="some title" columns="1"> 
                <apex:inputField value="{!mySobject['field_1__c']}"/>
                <apex:inputField value="{!mySobject['field_2__c']}"/>

            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save"  action="{!saveMe}" /> 


public class dynamicApexController 
    public SObject mySobject {get; set;}
    public Id sObjectId {get;set;}
    public String objectName {get;set;} 

    public dynamicApexController()
        sObjectId = ApexPages.currentPage().getParameters().get('id');  

        //building object and fields:

        objectName = sObjectId.getSObjectType().getDescribe().getName(); 
        Set<String> setFields = new Set<String>();        
        DescribeSObjectResult objSchema = sObjectId.getSObjectType().getDescribe();
        Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getMap();
        for (String fieldName : fieldMap.keySet())
            SObjectField sfield = fieldMap.get(fieldName);
            schema.describefieldresult dfield = sfield.getDescribe();
            String field = dfield.getName(); 
        String soqlQuery ='SELECT ';
        for(String sReqField : setFields) {            
            soqlQuery +=  sReqField + ','; 
        soqlQuery = soqlQuery.removeEnd(',');
        soqlQuery += '  FROM ' + objectName + ' WHERE Id =  \'' + String.escapeSingleQuotes(sObjectId) + '\'';

        mySobject = Database.query(soqlQuery);


    public PageReference saveMe()
       string f1 = mySobject.get('field_1__c');
       string f2 = mySobject.get('field_2__c');

       //once you get the value of f1, f2 then upsert to that sobject.

       return null;
  • Why do you need the value at all? Why not just update mySobject? – Adrian Larson May 4 '17 at 13:04
  • the reason is that those two fields are optional so i want to check if there is any value in it, before I update the object – Nick Kahn May 4 '17 at 13:05
  • But why? What are you hoping to achieve? – Adrian Larson May 4 '17 at 13:06
  • save whatever the user entered in those fields, if the user left blank then will get prompt to enter – Nick Kahn May 4 '17 at 13:06
  • 1
    Just make the <apex:inputField> tags required then! – Adrian Larson May 4 '17 at 13:08

If you are trying to enforce a value in these fields before you save, just set the attribute required="true" on your <apex:inputField> tags.

<apex:inputField required="true" value="{!record['Field1__c']}" />
<apex:inputField required="true" value="{!record['Field2__c']}" />

If for some reason that doesn't work for you, you should be able to check if the values are null in your controller as follows:

if (null == record.get('Field1__c'))
    ApexPages.addMessage(new ApexPages.Message(
        ApexPages.Severity.ERROR, 'Field 1 Must be populated'
    return null;
if (null == record.get('Field2__c'))
    ApexPages.addMessage(new ApexPages.Message(
        ApexPages.Severity.ERROR, 'Field 2 Must be populated'
    return null;
| improve this answer | |
  • Yes I could easily add the required attribute but in my case I have some custom business logic that I need to implement for that reason I need to check the value before I update the object. – Nick Kahn May 4 '17 at 13:51
  • @NickKahn See my update. Note that you didn't give your <apex:pageMessages> tag an id attribute, so you cannot rerender it. – Adrian Larson May 4 '17 at 13:55

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.