6 answers
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 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 approachtry {
If(City is not valid)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please check the city name'));
}
else{
update ac;
}
}Thanks,Maharajan.C
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. 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 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