+ Start a Discussion
Ken KamKen Kam 

addError is not adding error message under the field

We are trying to use the function addError() of sObject instead of ApexPages.AddMessage() in the controller to display error message on the visualforce page right beneath to the inputfield. However when the error is displayed, it shows up on the general section of the tag <apex:pagemessages>, but it does not show up underneath the inputfield. 

Here is a screenshot
User-added image

Here is the test visualforce page with just one field to display for testing. 
<apex:page standardcontroller="Apttus__APTS_Agreement__c" extensions="RequestorALL_VF_Controller" id="page">
    <apex:sectionheader title="test"/>
    <apex:form id="formId">
            <apex:pageblockbuttons id="Buttons">
                <apex:commandbutton value="Save Request" action="{!Save}" />
                <apex:commandbutton value="Cancel" action="{!Cancel}"/>            
            <apex:pageblocksection id="MainInfoSection">
                <apex:inputField value="{!Apttus__APTS_Agreement__c.Name}" required="false"/>

        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party__r.International_Status__c}" rendered="false" />    
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party__r.BillingCountry}" rendered="false" />
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party__r.Type}" rendered="false" />
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party__r.HCP__c}" rendered="false" />
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party_2__r.International_Status__c}" rendered="false" />
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party_2__r.BillingCountry}" rendered="false" />
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party_2__r.Type}" rendered="false" />
        <apex:outputLabel value="{!Apttus__APTS_Agreement__c.Contracting_Party_2__r.HCP__c}" rendered="false" />

and the snippet of the controller code ...

private boolean showError(SObject sObj, string sName, string errMsg) {
        boolean hasError = false;
        if (sName == null || sName == '') {
            if (errMsg == null) sObj.addError('the value is required');
            else sObj.addError(errMsg);
            hasError = true;
        return hasError;
if (showError(obj, obj.Name, 'Agreement Name is required')) hasError = true;

Any help is appreciated.
Hi Ken,

You would need to add the error to the field, like this:
I don't believe you can use a generic SObject like you've shown, because then you'd need to do something like this sObj.get('Name').addError(errMsg) and that doesn't work.

Hope that helps,