10

Scenario: I have a custom button on a standard layout, which will call an apex class to do some validation on a record. If not validated, it needs to throw out some errors on that page. What are the best UX options to achieve this?

I am not too sure how to achieve this. Within a trigger, you can use the addError method to add the custom errors.

Can my Apex class reference the fields of a standard object? The ideal approach would be to do the following:

Contact.Email.addError('Please enter the right format here');

How can I achieve this via an apex class that the custom button will call?

What I have thought: Having a VF Page popup on the screen so that user can see all the errors, but that's not the best approach as UX is a big deal for this application

Thanks.

4

You could exercise your validation logic in apex using either a Visualforce page or webservice called from Javascript. There is a good webservice example here:

Best practice for calling Apex methods from custom button?

Using Visualforce you have methods for error message handling built in. The most popular post here on StackExchange outlines when to use the various Visualforce message options.

Difference between the multiple messaging options in Visualforce?

The short answer is that you need an outputPanel VF component in your page that renders if you have a message to show. Example, with Twitter bootstrap markup/styles:

<apex:outputPanel rendered="{!hasMessages}" >
  <div class="row">
      <div class="span12">
          <div class="alert alert-error failureMsg">
              <h3>Error:</h3>
              <strong><apex:messages styleClass="unstyled" /></strong>
           </div>
       </div>
   </div>
</apex:outputPanel>

And your apex class has something like this:

//error handling properties to be aware of and display messages on error to the user on the page
public Boolean hasMessages { get { return ApexPages.hasMessages(); }}

I think your best bet is to just include the detail of the record page on the VF "error page" so adding the apex:detail component just below your error text would make the page look just like the standard object layout but it would in fact be a VF page with your validation logic and error surfacing.

  • I am not using Visualforce on the standard layout. I am well aware of apex messages! – subodhbahl Oct 16 '13 at 23:55
  • And what does your button call? – greenstork Oct 16 '13 at 23:56
  • an apex webservice or a class. – subodhbahl Oct 16 '13 at 23:59
  • You can't call a class directly without a webservice or Visualforce page. You could go the webservice route, I've seen that done before and then you'd use JS to handle and display your error. – greenstork Oct 17 '13 at 0:03
  • Agreed. So, is JS the only way to go? – subodhbahl Oct 17 '13 at 0:06
2

It's probably not as slick as what you're after, but it seems like the best route might be for the button to open a blank VF page, which does the processing from an Init() method in the controller. If there's an error you could then display a message on that page, maybe along with a <apex:detail> component or similar to allow them to make edits and try again.

If there's no error processing then you could simply redirect them at the end of the method call to the original record again.

Like I say, it's not exactly ideal but right now there is no standard way for you to achieve what you're after. Since presentation is an issue maybe replacing the standard view page with a VF page is the best option for you, thought it will potentially require the most work.

0

If throwing error on button custom button click is your prime requirement i don't think you need a controller. Create custom button to execute javascript on button click. Further you can get access of all the fields of object with following code {!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} var newComments = new Array(); var test= new sforce.SObject("Object Name"); If(test.fieldName == '' ){ alert('Show error message'); }

  • Thanks Jaimal for the input. I don't want to show the alerts to the user, that is one way to go but can get clunky if the errors will be on 30 different fields. – subodhbahl Oct 17 '13 at 18:40
0

Create a custom text field (or two). Your apex class has to populate the field with the error and the field to receive the error.

Then create a trigger that runs before update that checks if that field(s) is filled. If it is, it throws an error using the text on that field.

-1

You can use the addError method to add the error message to be displayed on the standard UI in your trigger. An example code will look like this:

trigger AccountTrigger on Account(before update) {
    for (Account acct:Trigger.new) {
        if(acct.yourfield != yourconditionvalue) {
            acct.addError('Validation failed.');
            return;
        }
    }
}

If you want to perform validations on more than one field and display all validation error messages, keep adding the validations and return just once after your validation is complete by tracking through a variable (flag).

Edit: The above works only in trigger. Couple of options to display error messages on the click of the custom button in standard page are: a. Answer suggested by greenstork b. Redirect to a visualforce page that can display one or more error messages with a link back to the original standard ui (this technique is something that I have used in one of my work).

  • This doesn't really answer the poster's question, who didn't ask about trigger errors but how to handle and show errors on a VF page. – greenstork Oct 16 '13 at 23:47
  • my bad - misunderstood the question. I'll update my answer. – Tech Matrix Oct 16 '13 at 23:49
  • Sorry its not about VF page. The error needs to be displayed on a standard page layout! So, the user clicks the custom button, validation happens and the errors are displayed on the standard layout. I cannot use validation rules because this is a DRO project and validation rules are not supported. – subodhbahl Oct 16 '13 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.