function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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:pagemessages/>
        <apex:pageblock>
            <apex:pageblockbuttons id="Buttons">
                <apex:commandbutton value="Save Request" action="{!Save}" />
                <apex:commandbutton value="Cancel" action="{!Cancel}"/>            
            </apex:pageblockbuttons>
                    
            <apex:pageblocksection id="MainInfoSection">
                <apex:inputField value="{!Apttus__APTS_Agreement__c.Name}" required="false"/>
            </apex:pageblocksection>
                
        </apex:pageblock>

        <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" />
    </apex:form>    
</apex:page>

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.
ClintLeeClintLee
Hi Ken,

You would need to add the error to the field, like this:
 
Apttus__APTS_Agreement__c.Name.addError(errMsg);
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,

Clint