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
AichaSFAichaSF 

visualforce alert message

Hello everyone, 

I have commandButton in my visualforce page that retrieves the id in a search box, searches for an object, and modifies the object founded.

I need to display an error message if the id does not exist or the object is already updated?
Any Help please.
Newbie__SalesforceNewbie__Salesforce
Hello Aicha

When you are assigning value of the id, modify the block with null checks and include Apex Message if found null
AichaSFAichaSF
Thank you for your response, 
Cas you give me exemple please.
Niraj Kr SinghNiraj Kr Singh
Hi AichaSF,

You can try this like:

COntroller:
public with sharing class add
{
    public String res{get;set;}

    public PageReference doAdd() {
        <Object Name> obj = [Select Id, Name From <Object Name> Where Id =: res];
        if(obj == null)
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Record not found.'));
        
        return null;
    }
}
/*****************/
Page:
<apex:page controller="AddController" id="thePage">
    <apex:form id="theForm" >
        <apex:pageMessages id="someMessages" />
        <apex:pageBlock id="theBlock" >
            <apex:inputText id="test" value=""/>
            <input type="button" value="Click" onclick="doAd()"/> <!-- Your button here or YOu can try below line-->
           
<!-- <apex:commandButton value="Click" onClick="doAd()" />-->
        </apex:pageBlock>

        <apex:outputPanel id="out">
            <apex:outputText value="{!res}"/>
        </apex:outputPanel>
        <apex:actionFunction name="doAdd" action="{!doAdd}" reRender="out" >
            <apex:param name="firstParam" assignTo="{!res}" value="" />
        </apex:actionFunction>
    </apex:form>
    
    <script type="text/javascript">
      
        function doAd(){
        var recId = document.getElementById('thePage:theForm:theBlock:test').value ;
        if(recId) {
            doAdd(recId);
        }
        else {
            alert('Plz! provide value for this field');
        }
    }
    </script>
</apex:page>
/****************************************/
Might be it will help you !!
Thanks
Niraj