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
Siddhesh ChavanSiddhesh Chavan 

<apex:pagemessages> not working in visualforce page.

<apex:pagemessages> not working in visualforce page.

Error with details directly redirected to new page
I want to show errormesage in standard error messagebox.
Below is the code details.

If the logedin user doesn't have FLS access then error message should get display on same page.
Visualforce Page:
 
<apex:pageMessages id="pmsg" />

<apex:pageBlock title="Cases">
   <apex:pageBlockButtons >
      <apex:commandButton value="Select "  onclick="return Test();" action="{!caseMethod}"  reRender="pmsg" />
   </apex:pageBlockButtons>

Controller Code:
if (!CaseComment.sObjectType.getDescribe().isDeletable()){
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Insufficient access'));
          return null;
        }
 delete ChildComment;

 
Chamil MadusankaChamil Madusanka
What is the use of onclick="return Test();"

Remove that part and try it.
OR
If there is a validation part or somthing in the Test() function, then use the following way.
 
<apex:page controller="testpagemsgcontroller">

<script>
function Test()
{
 alert('sss');
 if(true)
 {
     caseMethod();
 }
}


</script>
  <apex:pageMessages id="pmsg" />
<apex:form >
<apex:actionFunction name="caseMethod" action="{!caseMethod}" reRender="pmsg" />
<apex:pageBlock title="Cases">
   <apex:pageBlockButtons >
      <apex:commandButton value="Select " onclick="return Test();"  reRender="pmsg" />
   </apex:pageBlockButtons>
   
   </apex:pageBlock>
   </apex:form>
</apex:page>

If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.


 
Siddhesh ChavanSiddhesh Chavan
Thank you Chamil for your reply.

I have tried using above code but unfortunately its not working.
My controller method (caseMehtod()) not getting called from javascript by using above code.

When i am putting reRender ="pmsg" in commandbutton its giving null value error in javascript code at (shid.value = store;) line but when i am removing that , page is reRendering but caseMethod() not getting called.

Below is the javascript code.
function Test() {
        var radios = document.getElementsByName("parentId");
        var found = 1;
        for (var i = 0; i < radios.length; i++) {       
            if (radios[i].checked) {
               
                var store = radios[i].value;
                var shid = document.getElementById("j_id0:theForm:j_id5:hiddenId");
               
                shid.value = store; 
            
                found = 0;

                caseMethod();

                break;
            }
        }
        if(found == 1)
        {
            alert("Please Select the Case");
            return false;
        }
           
    }

 
Chamil MadusankaChamil Madusanka
Can you share the VF page and the controller?
Siddhesh ChavanSiddhesh Chavan
Visual Force Page:
 
<apex:page controller="DuplicateCases">
<script>
    function Test() {
        var radios = document.getElementsByName("parentId");
        var found = 1;
        for (var i = 0; i < radios.length; i++) {       
            if (radios[i].checked) {
               
                var store = radios[i].value;
                var shid = document.getElementById("j_id0:theForm:j_id5:hiddenId");
               
                shid.value = store; 
            
                found = 0;
                
                caseMethodAction();
                break;
            }
        }
        if(found == 1)
        {
            alert("Please Select the Case");
            return false;
        }
           
    }
    
</script>

<apex:pageMessages id="pmsg"></apex:pageMessages>
<apex:form id="theForm">

<apex:actionFunction name="caseMethodAction" action="{!caseMethod}" rerender="pmsg"/>

<br/>
<apex:outputLabel value=" Cases Found :" rendered="{!CaseSubject!=null}" style="font-weight:bold;"/><br/><br/>
<br/>

  <apex:pageBlock title="Cases">
 
   <apex:pageBlockButtons >
      <apex:commandButton value="Select Master Case" onclick="return Test();"  rerender="pmsg"/>
   </apex:pageBlockButtons>
   <apex:inputHidden value="{!idRadio}" id="hiddenId"/>
         
   <apex:pageBlockTable value="{!testCases}" var="sc">
        <apex:column headervalue="Select Parent">
            <input type ="radio" name="parentId" value="{!sc.ID}" > {!sc.ID}</input>
         </apex:column>
   </apex:pageBlockTable>
   </apex:pageBlock>
   
</apex:form>
</apex:page>

Controller Method:
 
public PageReference caseMethod() {
    
    List<case> updateCase = new List<case>();
    NewComment = new List<CaseComment>();
    childattachment = new List<Attachment>();
    
    for(Case cs: selectedCases){
        if(cs.id != idRadio){
      
        ids.add(cs.id);
        cs.ParentId = idRadio;
        cs.Status = 'Closed';
        
        List<CaseComment> ChildComment = new List<CaseComment>();
        for(CaseComment comm : cs.CaseComments)
        {
            ChildComment.add(comm) ;
        }
        
        for(CaseComment movecomm : ChildComment)
        {
            CaseComment com = new CaseComment();
            com.ParentId = idRadio;
            com.CommentBody= movecomm.CommentBody ;           
            NewComment.add(com) ;
        }
        
        // CRUD/FLS Enforcement for Delete CaseComment
        if (!CaseComment.sObjectType.getDescribe().isDeletable()){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Insufficient access'));
          //return null;
        } 
      
        delete ChildComment;  //Deleting old case comments
        
        }
    } 
    
    return Page.MergeSuccess; 
    }

 
Krishna SambarajuKrishna Sambaraju
The visualforce page and controller seem to be OK. Can you debug your javascript, by giving alerts at different places in your script and check how far it is executing? or press F12 and check if it is reporting any javascript errors in the console?

The script may be throwing an error in getting or setting the value of an element.

Regards,
Krishna.