Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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
6 answers
  1. Feb 3, 2020, 9:06 AM
    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
  2. Jan 31, 2020, 5:22 PM
    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

     
  3. Feb 4, 2020, 10:53 AM
    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.
  4. Jan 31, 2020, 8:33 AM
    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
  5. Jan 30, 2020, 6:54 PM
    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
0/9000