function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sanjay Vinayak TSanjay Vinayak T 

How to show Custom error message in Visualforce page?

Hi All,

I wanted to show a custom error message on the Visualforce page for the below code.

Kindly help me in fixing the code and show the error message.
 
Trigger: 
trigger StudentRecordTrigger on Student_Details__c (after insert) {
    if(Trigger.isinsert || Trigger.isafter ){
        StudentRecordTriggerHandler.checkStudentStatus(Trigger.new);
    }
}

-----------------------------------------------------------------------------------------------

Trigger HandlerClass:

public class StudentRecordTriggerHandler {
    List<Student_Master__c> studentToUpdate {get; set;}
    public static void checkStudentStatus(List<Student_Master__c> newStudent){
        try{
            Set<Id> StuId = new Set<id>();
            
            if(newStudent!=null){
                for(Student_Master__c stu : newStudent){
                    StuId.add(stu.Id);
                }
            }
            
            List<Student_Master__c> studentToUpdate = new List<Student_Master__c>();
            studentToUpdate = [Select id, Background_Check_Status__c, PAN_Card_Number__c, Phone_Number__c from Student_Master__c where id in : StuId];
            
            if(StuId!=null){
                List<Black_Listed_Candidate__c> blackListedStu = [Select id from Black_Listed_Candidate__c where PAN__c = :studentToUpdate[0].PAN_Card_Number__c limit 1];
                if(blackListedStu!=null){
                    blackListedStu[0].Phone__c = studentToUpdate[0].Phone_Number__c;
                    update blackListedStu;
                    studentToUpdate[0].Background_Check_Status__c = 'Candidate is blacklisted! We cannot Hire.';
                    
                    update studentToUpdate;
                }
              if (studentToUpdate[0].Background_Check_Status__c !=null){
                  ApexPages.addmessage(new apexPages.Message(ApexPages.severity.WARNING,'<h1 style="color : red;"> Student is blacklisted.'));
              }  
            }
        }
        catch(Exception e){
            system.debug('failed to update');
        }
        
    }
}

--------------------------------------------------------------

Visualforce page:
<apex:page Controller="StudentRecordTriggerHandler">
  <apex:form >
   <apex:pageBlock >
      <apex:pageMessages id="Message" escape="False"></apex:pageMessages>
    </apex:pageBlock>
     </apex:form>
</apex:page>

Thank you.
Shah ZamanShah Zaman
This was a much needed post for me for my website (https://pescoonlinebill.pk/). Thank you for sharing.
Shah ZamanShah Zaman
You can generate free money and diamonds using free fire diamonds and coins hack 2023 (https://apkfleet.com/free-fire-generator/). Believe me its fun.