+ Start a Discussion
Alex Wong 4Alex Wong 4 

Is it possible to show standard and custom error message at the same time?

I want to know that is there a method to show the standard error message and custom error message at the same time? Also, in my script, there are several custom error message. Is it possilbe to show them at the same time? 
Here is my code:
public class extattachfile {
    Public attachment objAttachment{get;set;}
    Public attachment objAttachment2{get; set;}
    Public attachment objAttachment3{get; set;}
    Public attachment objAttachmentt{get; set;}
    Public attachment objAttachments{get; set;}
    Public Artist__c artist{get; set;}
    Public extattachfile(apexpages.standardcontroller stdCon) {
        objAttachment = new Attachment();
        objAttachment2 = new Attachment();
        objAttachment3 = new Attachment();
        objAttachmentt = new Attachment();
        objAttachments = new Attachment();
        artist= new Artist__c ();
    }
    public PageReference save() {
        Boolean checkAttachment = false;
        Boolean isValidUrl = true;
        if(artist.Id == null){
            if(artist.Website__c != null){
                Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
                Boolean isMatch = emailPattern.matcher(artist.Website__c).matches();
                if(!isMatch){
                    objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Website URL.'));
                    isValidUrl = false;
                }
            }
            if(artist.Facebook__c != null){
                Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
                Boolean isMatch = emailPattern.matcher(artist.Facebook__c).matches();
                if(!isMatch){
                    objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Facebook URL.'));
                    isValidUrl = false;
                }
            }
            if(artist.SoundCloud__c != null){
                Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
                Boolean isMatch = emailPattern.matcher(artist.SoundCloud__c).matches();
                if(!isMatch){
                    objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Soundcloud URL.'));
                    isValidUrl = false;
                }
            }
            if(artist.YouTube__c != null){
                Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
                Boolean isMatch = emailPattern.matcher(artist.YouTube__c).matches();
                if(!isMatch){
                    objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Youtube URL.'));
                    isValidUrl = false;
                }
            }
            if(isValidUrl){
                insert artist;
            }else{
                return null;
            }
        }
        List<Attachment> attachmentList = new List<Attachment>();
        if(objAttachment.Body != null){
            objAttachment.ParentId = artist.id;
            List<String> splitStrList = objAttachment.Name.Split('\\.');
            String fileExtension = splitStrList[1];
            objAttachment.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Photo1'+'.' + fileExtension;
            attachmentList.add(objAttachment);
            checkAttachment = true;
        }
        if(objAttachment2.Body != null){
            objAttachment2.ParentId = artist.id;
            List<String> splitStrList = objAttachment2.Name.Split('\\.');
            String fileExtension = splitStrList[1];
            objAttachment2.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Photo2'+'.' + fileExtension;
            attachmentList.add(objAttachment2);
            checkAttachment = true;
        }
        if(objAttachment3.Body != null){
            objAttachment3.ParentId = artist.id;
            List<String> splitStrList = objAttachment3.Name.Split('\\.');
            String fileExtension = splitStrList[1];
            objAttachment3.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Photo3'+'.' + fileExtension;
            attachmentList.add(objAttachment3);
            checkAttachment = true;
        }          
        List<Attachment> attachmentListother = new List<Attachment>();
        if(objAttachmentt.Body != null){
            objAttachmentt.ParentId = artist.id;
            List<String> splitStrList = objAttachmentt.Name.Split('\\.');
            String fileExtension = splitStrList[1];
            objAttachmentt.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Technical'+'.' + fileExtension;
            attachmentList.add(objAttachmentt);
        }
        if(objAttachments.Body != null){
            objAttachments.ParentId = artist.id;
            List<String> splitStrList = objAttachments.Name.Split('\\.');
            String fileExtension = splitStrList[1];
            objAttachments.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Stage'+'.' + fileExtension;
            attachmentList.add(objAttachments);
        }
        Insert attachmentListother;
        if(attachmentList.size() > 0 && checkAttachment){
            insert attachmentList;
            attachmentList = null;
            // if successfully inserted new contact, then displays the thank you page.
            return Page.ack;
        }else{
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please attach at least one photo attachment.'));
            return null;
        }             
    }
}

Thank you for your help.
Best Answer chosen by Alex Wong 4
Sujith NairSujith Nair
Salesforce evaluvates the page level validations first and because of that the standard messages is what appears first. So in your case once the page level validations fail it would not even call the controller and no custom messages appear.

All Answers

Sujith NairSujith Nair
Yes. You can display custom/standard error message using apex:messages or apex:pagemessages.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_pageMessages.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_messages.htm
Alex Wong 4Alex Wong 4
I have typed it in already and it can show both custom/standard error message. However, I want to both of them to be showed at the same time. 

Here is the background for my page. It is a form to fill in information, and also attachment. Some of the essential information and attachment are
required to fill in. However, if I input none of the field and attachment, it will only show the standard error message first, but I want to show all. Only when I input all the field with standard error message, and the attachment field is blank, the custom error message finally shows.

Here is my page code for your reference:
<apex:page standardController="Artist__c" extensions="extattachfile" showHeader="false" sidebar="false">
<meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<style type="text/css">
h1 {font-size: 150%; font-weight: bold;}
h2 {font-size: ;}
p { width:100%;
    font-size: 120%;
    text-align: justify;
    text-justify: inter-word;}
.reddot {color: red; font-size:110%;}
.textarea { height:200px; 
            width:300px;
            border-radius: 5px;}
.url {width:300px;}
.title {font-size:175%;}
.standard {width: 310px;}
@media only screen and (min-width: 900px) {
    /* For desktop: */
p { width:890px;
    font-size: 120%;
    text-align: justify;
    text-justify: inter-word;}
}
.color {
    background-color: #F7F6D2;
    -webkit-transition: all 1s ease;
    -moz-transition: all 1s ease;
    -o-transition: all 1s ease;
    transition: all 1s ease;
}
</style>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"/>
</head>


<apex:form styleclass="form">
   <apex:pageBlock >
<apex:pageMessages />
<div><b class="title">Submission Form</b></div><br/>

<br/>
     <h1>Artist Information</h1>
     <br/><br/>

<br/>
      <div class="standard">
     <h2 >Artist Name (English)</h2><br/>
	<apex:outputText styleClass="reddot" value="*"/>
     <apex:inputField value="{!artist.Artist_Group_Name_English__c}" label=""/>
     <br/>      </div>
<br/>      
<br/>
      <div class="standard">
     <h2 >Artist Biography (English)</h2><br/>
	<apex:outputText styleClass="reddot" value="*"/>
     <apex:inputField value="{!artist.Artist_Group_Biography_English__c}" label="" styleClass="textarea" required="true"/>
     <br/><br/>      </div>
<br/>
      <div class="heading">
     <h1>Name and Roles of Group Members (if applicable)</h1>
     <br/>
	<br/>      </div>
<br/>
      <div class="standard">
     <h2>Name and Roles of Group Members</h2><br/>
	<apex:outputText styleClass="reddot" value=""/><br/>
     <apex:inputField value="{!artist.Member_Name_Role__c}" label="" styleClass="textarea"/><br/>
     <br/>      </div>
<br/>
      <div class="standard">
     <h2 >Photo of Artist/ Group  : max 3 attachments</h2><br/>
	<apex:outputText styleClass="reddot" value="*"/>
     <br/>
     <apex:inputFile value="{!objAttachment.body}" fileName="{!objAttachment.name}"/><br/>
     <apex:inputFile value="{!objAttachment2.body}" fileName="{!objAttachment2.name}" required="false" /><br/>
     <apex:inputFile value="{!objAttachment3.body}" fileName="{!objAttachment3.name}" required="false" /><br/><br/>      </div>
<br/>
      <div class="standard">
     <h2>Website</h2><outputtext/><apex:outputText styleClass="reddot" value=""/><br/>
     <apex:inputField value="{!artist.Website__c}" label="" styleClass="url"/><br/>
     <br/>      </div>
<br/>
      <div class="standard">
     <h2 >Facebook</h2><apex:outputText styleClass="reddot" value=""/><br/>
     <apex:inputField value="{!artist.Facebook__c}" label="" styleClass="url"/><br/>
     <br/>      </div>
<br/>
      <div class="standard">
     <h2 >SoundCloud</h2><apex:outputText styleClass="reddot" value=""/><br/>
     <apex:inputField value="{!artist.SoundCloud__c}" label="" styleClass="url"/><br/>
     <br/>      </div>
<br/>
      <div class="standard">
     <h2 >YouTube</h2><apex:outputText styleClass="reddot" value=""/><br/>
     <apex:inputField value="{!artist.YouTube__c}" label="" styleClass="url"/>
     <br/>      </div>
<br/><br/>
      <div class="standard">
     <h2>Technical Rider : max 1 attachment</h2><br/>
	<apex:outputText styleClass="reddot" value=""/><br/>
     <apex:inputFile value="{!objAttachmentt.body}" fileName="{!objAttachmentt.name}" /><br/>      </div>
<br/>
      <div class="standard">
     <h2>Stage Plot : max 1 attachment</h2>
     <br/>
	<apex:outputText styleClass="reddot" value=""/><br/>
    
     <apex:inputFile value="{!objAttachments.body}" fileName="{!objAttachments.name}" required="false" /><br/><br/>      </div>
<br/>
      <div class="heading">
     <h1>Contact Information</h1> <br/><br/>      </div>
<br/>
      <div class="standard">
     <h2>Contact Person</h2><apex:outputText styleClass="reddot" value="*"/>
     <apex:inputField value="{!artist.Contact_Person__c}" label=""/>
     <br/>      </div>
<br/>
      <div class="standard">
     <h2 >Contact No.</h2><apex:outputText styleClass="reddot" value="*"/>
     <apex:inputField value="{!artist.Contact_Number__c}" label=""/>
     <br/>      </div>
<br/>
      <div class="standard">
     <h2>Email </h2><apex:outputText styleClass="reddot" value="*"/>
     <apex:inputField value="{!artist.Email__c}" label=""/>
     <br/>      </div>
<br/>


        <apex:commandButton action="{!save}" value="Save"/>
      </div>
      
<script>
var addclass = 'color';
var $cols = $('.standard').click(function(e) {
    $cols.removeClass(addclass);
    $(this).addClass(addclass);
});


</script>

</apex:pageBlock>       
</apex:form>
<apex:pageMessages />
</apex:page>

 
Sujith NairSujith Nair
Salesforce evaluvates the page level validations first and because of that the standard messages is what appears first. So in your case once the page level validations fail it would not even call the controller and no custom messages appear.
This was selected as the best answer