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
OldDeadBugOldDeadBug 

Displaiy a message based on user error rather than a system error

This should be fairly simple

 

I have a pageTable with one column dedicated to a selectCheckbox for the row item. If the user click the pageBlockButton while no item is selected, I was hiping to either use the ApexPages Message component, or if that's overkill, just display a Javascript alert when the button is pushed.

 

I've tried creating a boolean checking method on the display list

 

 

public boolean checkMethod (list<object> objectList)
{
    boolean check = false;
    for (Object obj :ObjectList)
    {
        if (obj.Selected = true)
            check = true;
    }
     return check
}

 I then call this as part of the method referenced by the button:

 

public Pagereference processItems()
{
   if (!objectList.isEmpty())
   {
       if (!checkSelection(ObjectList)
       {
           ApexPages.Message err = new ApexPages.Message(
              ApexPages.Severity.ERROR, 'No selection has been made. Select an item before continuing');
           ApexPages.addMessage(err);
       }
   }
   else
   ...run Method

 however, this does not display anything in the page when using either <apex:PageMessages></apex:PageMessages>, or using <apex:Messages>.

 

 

The other idea was to put logic into the action attribute of the commandButton. I created a boolean property called checkSelection, I changed the above checkSelection method to 'getCheckSelection', and then wrote the action attribute as

action="{!IF checkSelection, processItems, alert('Please select item before continuing')}"

 

but that didn't work either.

 

Suggestions and solutions are most appreciated

 

ODB

 

jeffdonthemic2jeffdonthemic2

My suspicion is that you need to rerender the the pageMessages component on your Visualforce page. Take a look the demo below. Notice on line #4 of the Visualforce page I've assigned an id (errors) to the pageMessages component. When the actionFunction (searchServer) fires to run the search on the server (line #25), I am rerendering the errors pageMessages so that any new messages appear. Line #46 in the controller is where I am generating an error notification if the query fails. There's a working demo you can run and generate an error to see how it works.

 

Building a Dynamic Search Page in Visualforce

 

HTH

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com

 

Author: The Salesforce Handbook

OldDeadBugOldDeadBug

First of all, thanks so much for the blog reference. There was a lot of pieces there that I had not seen in terms of how to use certain VF components. For example,

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));

I didn't get from the VF Guide that this is how you add your own message. I don't think they have this line anywhere in the guide examples and I guess we're just supposed to figure it out from what's in there. I didn't.

 

However, the issue I have is to produce an error message for something that isn't really an error, as far as the system is concerned. I have an inputCheckbox for each line in my table, and want to display a message if a user forgets to select a line when modifying it.The system just reRenders the outputPanel with no changes, but I would like to display a message to the user reminding him to select a line. I would think maybe an actionSupport on the Create button, or possibly trying to use the onclick="" attribute could work, however I can't seem to use a boolean function to inform the attribute-not sure why.

 

For example:

 

 <apex:commandButton 
action="{!IF(selectedCheck, createQuote, alert('Please select an item'))}"
this doesn't work
value="Create Quote" > </apex:commandButton> //with public boolean checkSelection; public boolean getCheckSelection(list<LineQuote> LQs) { boolean selectCheck = false; for (LineQuote LQ :LQs) { if(LQ.Selected) { selectCheck = true; } } return checkSelection; }

 

 

This returns the following error message: 

                Error: Unknown property 'OpportunityStandardController.checkSelection' 

       not sure why as the property is clearly defined in the controller. This is an Opportunity extension, so maybe that has something to do with it- I don't think so but I don't really know.

 

Anyway, here's how I solved it.

 

 

if (checkSelection)
{
   for (Object O :objectList) 
   {
       ...selected item is found, code runs normally
   }
}
else
{
    for (Object O :objectList)  // I can't create a Validation Rule for an unselected inputBox, I create my own error
    {
        try{                    
            Account error;
            insert error;
        }
        catch (Exception e){
            e.setMessage('Please Select an item');  //change the error Message on the system exception
            ApexPages.addMessages(e); //send the edited message to the page
            break;     // not sure why, but apparently this process needs to
// break a loop
// otherwise the page just refreshes without the error message }

I know there is an Exception class that can be extended but the guide's examples are rather sparse, or else I'm too dense to figure out how to use it from the guide. I tried several times to create a myException object but nothing worked right. So why not create a system error, change the error message and send it to the page? One exception's as good as another for sending a message to the page, as long as I'm creating it on purpose.

 

Its probably a stupid way to do it, but it works.

However, I'd like to see a good example of how to use the Exception class for this kind of purpose. Anything to help understand the Dev Guide.

 

 

jeffdonthemic2jeffdonthemic2
sinsiterrulezsinsiterrulez

HI,

Why don't you just disable the button initially & when ever the user ticks the checkbox, have an action function which will increment the count of a variable in controller by 1 & then refresh the page..If the variable value is equal to no. of records on page enable the button

<apex:actionfunction name="Enable">
<apex:param value="" assignto="{!Checked}">
</apex:actionfunction>

<apex:pageblock>
<apex:pageblockbutton>
<apex:commandbutton action={!Save}" value="Save" disabled="{!Checked}"/>
</apex:pageblockbutton>

<apex:pageBlockTable value="{!account.Contacts}" var="item">
<apex:column value="{!item.name}" onclick="Enable(this.value);"/>
</apex:pageBlockTable>

 Create a controller methods to implement your logic!!!