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
Ap30Ap30 

ApexMessage is not displayed

Hi All,
In my below code else part is not working though status field is blank. Please guide where i'm going wrong.
===============
<apex:page standardController="Detail__c" extensions="WarningPage" action="{!displayWarning}">

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

==============================================


public with sharing class WarningPage {
 
    public WarningPage(ApexPages.StandardController controller) {
    }
    public void displayWarning(){
   
    list<Detail__c> stud = [select id,name,Status__c  from Detail__c ];
    
        for(Detail__c s : stud)
        {
        
            
                if(s.Status__c == 'More details needed')
                {
                   
                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'More details needed'));
                }
                else if(s.Status__c == '')
                {
                    
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Verified'));
                }
                }
                }
                }
Best Answer chosen by Ap30
Suraj Tripathi 47Suraj Tripathi 47

Actually, I correct your code Only not your logic.

Yoy are getting this error "I'm getting both messages in single page irrespective of status field value." because your detailList has 2 object simple

So as a  page Load ==>at that instant whatever would be in the  List would show

so correct your code according to it.

By the way, what is the requirement?

 

 

All Answers

PriyaPriya (Salesforce Developers) 
Hi ,

In the Apex class, before the For Loop, you should check if the list is empty or not. 
For example, consider the following code:
Account[] records = [SELECT Id, Name, AccountNumber FROM Account];
 if(records!=null && !records.isEmpty()) 
{
 for(Account record: records)
 { 
// Do Something Here
 }
}

Please mark it as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan



 
Ap30Ap30
Hi, It doesn't work. Warning message is "More details needed" even if the status field value is empty.
PriyaPriya (Salesforce Developers) 
Kindly confirm the status__c value by using debug statement. It seems like it has the value. 
Ap30Ap30
I changed the status comparison value to null. Now both messages are displaying in page layout. else if(s.Status__c == null) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Verified')); }
Suraj Tripathi 47Suraj Tripathi 47

Hi AP,

I have run your code in my Org and I found that minor mistake in you code

Please replace this Line    else if(s.Status__c == '') with below code

1.

else if(String.isBlank(s.Status__c) || s.Status__c == '')

Or

2.

You can also simply write

else if(String.isBlank(s.Status__c)){}

in both case, it is working fine.

 Please mark it as the Best Answer so that other people would take reference from it.

Thank You

Ap30Ap30
Thanks for the reply. I'm getting both messages when I open a record/create a record.
Suraj Tripathi 47Suraj Tripathi 47

Ap, Please mark As the Best Answer

Thank You

Ap30Ap30
Sorry, I was not clear in the reply. I'm getting both messages in single page irrespective of status field value.
Suraj Tripathi 47Suraj Tripathi 47

Actually, I correct your code Only not your logic.

Yoy are getting this error "I'm getting both messages in single page irrespective of status field value." because your detailList has 2 object simple

So as a  page Load ==>at that instant whatever would be in the  List would show

so correct your code according to it.

By the way, what is the requirement?

 

 

This was selected as the best answer