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.

1 Answer 1

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/>? May 16, 2018 at 17:54
  • no need to write inside that May 16, 2018 at 17:54
  • sir is my controller class correct? Its still not showing the error message May 16, 2018 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. May 16, 2018 at 18:08
  • why do we need to throw the error? we are already passing the message in addMessage() May 16, 2018 at 18:24

You must log in to answer this question.

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