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
Aneesh AlumallaAneesh Alumalla 

How to show success/error message in the vf page if insertion/update is success/failed.

Hi ,
I have created one Apex class and its VF page.
I have 2 fields in VF page, CITY and Phone.
Target: To update the Phone of the Account whose City is given City with the given phone number.

I have created everything and update is working perfectly.
But I wanted to add a message if the update is failed. and also if the update is success.

Here is my code:
Apex Class:
public class D2 {
    public String city{get;set;}
    public String phn{get;set;}
    public boolean show{get;set;}
    public D2(){
       
    }
    public void updatePhoneNum(){
        List<account> ac = new List<account>();
        List<account> ac1 = new List<account>();
        ac =[Select name, phone, Status__c, Gender__c, BillingCity from Account where BillingCity =:city];        
        for(Account a : ac){
               a.phone =phn;
            a.Status__c = 'In Progress';
            a.Gender__c = 'Male';       
        }
        update ac1; 
    }
}

Vf Page:

<apex:page controller="D2" >
    <apex:form>
        <apex:pageBlock title=" To Update the phone number of Accounts whose City is provided">
            <apex:pageMessages />
            <apex:pageBlockSection title="Provide the details" columns="1">
                <div align = "center">
                <apex:inputText  label="City" value ="{!city}" />
                <apex:inputText  label="Phone Number" value ="{!phn}" />
                    </div>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <div align = "center">
                <apex:commandButton  style ="float:centre" value ="Update" action ="{!updatePhoneNum}"/>
                </div>
            </apex:pageBlockSection>
                
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please assist
Best Answer chosen by Aneesh Alumalla
Sahil ShitoleSahil Shitole
Hi Aneesh,
Use below code:

VF PAGE:
<apex:page controller="D2" >
    <apex:form>
        <apex:pageBlock title=" To Update the phone number of Accounts whose City is provided">
            <apex:pageMessages id="msgId"/>
            <apex:pageBlockSection title="Provide the details" columns="1">
                <div align = "center">
                <apex:inputText  label="City" value ="{!city}" />
                <apex:inputText  label="Phone Number" value ="{!phn}" />
                    </div>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <div align = "center">
                <apex:commandButton  style ="float:centre" value ="Update" action ="{!updatePhoneNum}" rerender="msgId"/>
                </div>
            </apex:pageBlockSection>
                
        </apex:pageBlock>
    </apex:form>
</apex:page>

CONTROLLER:
public class D2 {
    public String city{get;set;}
    public String phn{get;set;}
    public boolean show{get;set;}
    public D2(){
       
    }
    public void updatePhoneNum(){
        List<Account> ac = [Select name, phone, BillingCity from Account where BillingCity = :city];
        if(!ac.isEmpty()) {
          for(Account a : ac){
            a.phone = phn;
            a.Status__c = 'In Progress';
            a.Gender__c = 'Male';
           }
            update ac;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, 'Record updated...!!!'));
        }
        else {
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Update Failed..No account present in org for given city'));
        }
    }
}

Do let me know if this helps you.

Thanks

All Answers

Maharajan CMaharajan C
Hi Aneesh,

Please use the below apex class:

public class D2 {
    public String city{get;set;}
    public String phn{get;set;}
    public boolean show{get;set;}
    public D2(){
       
    }
    public void updatePhoneNum(){
        List<account> ac = new List<account>();
        List<account> ac1 = new List<account>();
        ac =[Select Id,name, phone, Status__c, Gender__c, BillingCity from Account where BillingCity =:city];        
        for(Account a : ac){
            a.phone =phn;
            a.Status__c = 'In Progress';
            a.Gender__c = 'Male';       
        }
        
        try
        {
            update ac;
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Record Updated successfully')); 
        }
        catch(DmlException e) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Contact your Administrator')); 
        }
        
    }
}

Thanks,
Maharajan.C
Aneesh AlumallaAneesh Alumalla
Hi Maharajan,
I tried this. But it is not showing any error message when the update is failed.
I tried with Wrong City name such as 123, and it is not showing any error message.
Please assist
Maharajan CMaharajan C
Aneesh, Try any one of the below methodology.

1.   Create the custom validation rule in Account object to validate the city names as per you requirement and throw the Validation message.Thsi is Standard out of box functionaliy 

2.  Otherwise write the custom apex logic before the update statement and throw the error message using below line.
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please check the city name')); Find the below code sample for  this approach
try
{
    If(City is not valid)
    {
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please check the city name')); 
    }

    else{
       update ac;
    }
}




Thanks,
Maharajan.C
 
Sahil ShitoleSahil Shitole
Hi Aneesh,
Use below code:

VF PAGE:
<apex:page controller="D2" >
    <apex:form>
        <apex:pageBlock title=" To Update the phone number of Accounts whose City is provided">
            <apex:pageMessages id="msgId"/>
            <apex:pageBlockSection title="Provide the details" columns="1">
                <div align = "center">
                <apex:inputText  label="City" value ="{!city}" />
                <apex:inputText  label="Phone Number" value ="{!phn}" />
                    </div>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <div align = "center">
                <apex:commandButton  style ="float:centre" value ="Update" action ="{!updatePhoneNum}" rerender="msgId"/>
                </div>
            </apex:pageBlockSection>
                
        </apex:pageBlock>
    </apex:form>
</apex:page>

CONTROLLER:
public class D2 {
    public String city{get;set;}
    public String phn{get;set;}
    public boolean show{get;set;}
    public D2(){
       
    }
    public void updatePhoneNum(){
        List<Account> ac = [Select name, phone, BillingCity from Account where BillingCity = :city];
        if(!ac.isEmpty()) {
          for(Account a : ac){
            a.phone = phn;
            a.Status__c = 'In Progress';
            a.Gender__c = 'Male';
           }
            update ac;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM, 'Record updated...!!!'));
        }
        else {
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Update Failed..No account present in org for given city'));
        }
    }
}

Do let me know if this helps you.

Thanks
This was selected as the best answer
Aneesh AlumallaAneesh Alumalla

Thanks Sohil!.

This Helped.

Aneesh AlumallaAneesh Alumalla
Thanks Maharaj,
Your code also worked successfully.
But unfrtunately I could select only one answer as Best Answer.

Both Maharaj and Sahil answers are correct.

Thanks for your help guys.