1

How to display the error message from a trigger to a custom visualforce page?

 public PageReference save()
    { 

        Student__c stud=new Student__c();
        stud.Firstname__c=student.Firstname__c;
        stud.Lastname__c=student.Lastname__c;
        stud.Address__c=student.Address__c;
        stud.Collegename__c=student.Collegename__c;
        stud.Branchname__c=student.Branchname__c;
        stud.Contact_Number__c=student.Contact_Number__c;
        stud.Date_Of_Birth__c=student.Date_Of_Birth__c;
        stud.Gender__c=student.Gender__c;
        studentlist.add(stud);

        try
       {
         if(studentlist!=null)
          {
            insert studentlist;

          }
       }
         catch(Exception e)
          {
          Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,e.getMessage()));

          }


     return null;
    }

i tried using this code but it does not display the message.

3

You need to add <apex:pageMessages/> to display error messages on visualforce page.

Refer apex:pageMessages

This component displays all messages that were generated for all components on the current page, presented using the Salesforce styling.

8
  • i tried that too .do i need to write anything in side the <apex:pageMessages/>? – Naveen Singh rajput May 16 '18 at 17:54
  • no need to write inside that – Santanu Boral May 16 '18 at 17:54
  • sir is my controller class correct? Its still not showing the error message – Naveen Singh rajput May 16 '18 at 18:01
  • 1
    Make sure that an error is being thrown (easy way is to insert new Student__c();, usually fails since theres required fields or expected values), and that your save method rerenders your page messages. – battery.cord May 16 '18 at 18:08
  • why do we need to throw the error? we are already passing the message in addMessage() – Naveen Singh rajput May 16 '18 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.