+ Start a Discussion
Money CareMoney Care 

How to display error Message

I have created a class for file uploading.its working fine me now.but i want to put restriction like that if attachement then save otherwise it will display error message...How to achieve this
public class FormCurriculumController { 

public Purchase_Order__c curriculum {get; set;}
public Boolean privacy {get; set;}
public Boolean saved {get; set;}
public String styleClass {get; set;}

public Transient Blob resume {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}
    public boolean showC1RecordType {get;set;}
    public boolean showC2RecordType {get;set;} 
    private Id c1RecordTypeId; 
    private Id c2RecordTypeId;

//ublic FormCurriculumController () {
    //curriculum = new Purchase_Order__c();
    //saved = false;

  public FormCurriculumController(ApexPages.StandardController controller) {

        c1RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId();
        c2RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId();
        curriculum= new Purchase_Order__c();
        curriculum= (Purchase_Order__c)controller.getRecord();
        String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues');
        if(isButtonClicked == 'true'){
        showC1RecordType = false;
        showC2RecordType = false;
        if(curriculum.RecordTypeId == c1RecordTypeId){
            showC1RecordType = true;
            showC2RecordType = false;
        else if(curriculum.RecordTypeId == c2RecordTypeId){
            showC1RecordType = false;
            showC2RecordType = true;
     public void setDefaultValues(){
        String quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        Quote__c quote = [Select o.id, o.name, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c,
                    o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE O.id=:quoteId];
        curriculum.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c;
        curriculum.Quote__c = quote.id;
        curriculum.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c;
        if(quote.Record_type_name__c == 'C1'){
            curriculum.RecordTypeId = c1RecordTypeId;
        else {
            curriculum.RecordTypeId = c2RecordTypeId;


public PageReference save() {
    try {
        if (!privacy) {
         ApexPages.Message reqMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please upload recent Purchase Order  before saving the record');
            return null;

        if( ! Test.isRunningTest() )
        if (resume != null) {
            Attachment attach = new Attachment();
            attach.Body = resume;
            attach.Name = fileName;
            attach.ContentType= contentType;
            attach.ParentId = curriculum.id;                
             try {
                return new PageReference('/'+curriculum.id);
            catch (Exception ex) {

                return null;

        update curriculum;

        saved = true;

    catch(Exception ex) {
    return null;   

Vivek DVivek D
I guess you have added the ApexPages.addMessages for the code now if you need to display the error on the page just put the tag 
on the page and the message will display. You have to choose Serverity properly because it will display different kind of alert view
new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg');
User-added image