2

enter image description here

As in the above pic shows, I don't want to display the System error message which i rounded in pic. How can i make disable it? I want to display only the message what we used generally in ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Error Message'));. I can make this possible by using if - else condition (Please see the code which i used made it). but i want get it through using try - catch blocks only. And also please tell me that how we are getting Required fields are missing: [Account Name] message. Is that system validation rule or any other error message?

You can also see the code which i have used.

    public void comittedResult() {
        try{
        aOne=[select id,name,phone,Industry from Account where ID=:accid];
       /* if (String.isBlank(accName)){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter account name'));
        }//!(accPhone.isNumeric())
        else if(!(pattern.matches('[0-9]+',accPhone))){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter correct phone number'));
        }
        else  {*/
        aOne.name = accName;
        aOne.phone = accPhone;
        update aOne;
        displayPopup = false;
        reflectRecords();
   //   }
     }catch(DMLException e){
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter Name'));
     }
     }
3
  • @Kumaar, if you're using an apex:inputField element you can simply add required="true" attribute to it, and it will not allow name to be blank and it will save unnecessary server operation. Aug 8, 2016 at 7:21
  • KS Kumaar.. Can you please closed this question by accepting that answer which you used as your solution.
    – Ratan Paul
    Aug 10, 2016 at 11:35
  • Yeah Ratan, Still i am finding for exact solution where the following solutions giving hints only. Definitely i will close it.
    – KS Kumaar
    Aug 11, 2016 at 6:19

3 Answers 3

2

If you want to have this field not blank you can achieve this using the visualforce required='true' attribute.

So if you have:

<apex:inputText value='{!a.Name}'/>

just change it to:

<apex:inputText value='{!a.Name)' required='true'/>

And it should validate that the name won't be blank, and won't send an unnecessary server-call and won't do an unnecessary DML operation.

1

instead of adding the add error in the catch block. first, check the name is null or not

if(accName == null)
{
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter Name'));
   return;
}

OR another way to show only one error message in page

catch(DMLException e){
    ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, e.getdmlMessage(0) );
    ApexPages.addMessage(msg);
}
7
  • Hi Ratan, The same two messages are coming even by using e.getdmlMessage(0)
    – KS Kumaar
    Aug 8, 2016 at 7:21
  • @KSKumaar with e.getdmlMessage(0) are you using (ApexPages.severity.error,'Please enter Name') this as well?
    – Ratan Paul
    Aug 8, 2016 at 7:23
  • Yeah @Ratan. I am using it.
    – KS Kumaar
    Aug 8, 2016 at 7:27
  • @KSKumaar dont' use both only use only one i.e. e.getdmlMessage(0)
    – Ratan Paul
    Aug 8, 2016 at 7:28
  • I used only e.getdmlMessage(0). But it is giving Required fields are missing: [Account Name]. This is system error message. right?
    – KS Kumaar
    Aug 8, 2016 at 7:34
0
if(accName == null || accName == '') {
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Please enter Name'));
}

OR

catch(DMLException e){
    if(e.getMessage().contains('Required fields are missing')){
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please Enter Account Name');
        ApexPages.addMessage(myMsg);
    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .