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
Big EarsBig Ears 

Quick Question - Displaying error messages to users

I was just hoping to get something confirmed.

 

Is it only possible to display error messages to a user via:

1) Trigger code

2) Apex code for visualforce controllers

 

I just want to confirm if it's possible to display an error message to a user within am APEX method that has been called by a trigger. If it is, I've not worked out how to display the message to the user.

 

Andy

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Calling addError on a field or object involved in the transaction will return that error to the parent module. So, for example, if a page calls a dml that has a trigger, and that trigger calls a function that is passed one of the records from the trigger's "New" or "NewMap" members, and calls addError, AND those records are rendered on the screen (they are bound to inputField elements), the errors will appear.

All Answers

Big EarsBig Ears

Thank you for your response. However, I don't think it answers my question. These are examples that use visualforce controllers or trigger code. I'm looking to find if there's any way to present an error to a user outside of these specific cases.

sfdcfoxsfdcfox

Calling addError on a field or object involved in the transaction will return that error to the parent module. So, for example, if a page calls a dml that has a trigger, and that trigger calls a function that is passed one of the records from the trigger's "New" or "NewMap" members, and calls addError, AND those records are rendered on the screen (they are bound to inputField elements), the errors will appear.

This was selected as the best answer
T-007T-007

Use the loggingLevel enum to specify the logging level for all debug methods.
Valid log levels are (listed from lowest to highest):
ERROR
WARN
INFO
DEBUG
FINE
FINER
FINEST
Log levels are cumulative. For example, if the lowest level, ERROR, is specified, only debug methods with the log level of ERROR are logged. If the next level, WARN, is specified, the debug log contains debug methods specified as either ERROR or WARN.

Big EarsBig Ears

sfdcfox,

 

Thank you for your reply. My method performs some actions on a subset of records from the trigger.new list (e.g. Opportunities that have just been marked "Payment received")

 

I've started passing the records of interest AND the trigger.newMap into the method and am using the "addError" method with the trigger.newMap records in order to ensure that the error is assigned to the trigger context records. However, placing the error against the record doesn't seem to stop the code, so it just falls down at a later point. Is there a way to prevent the code from continuing once the error has been assigned? Or is it that the code needs to be structured in such a way that the ONLY thing that can happen is the error message and nothing else (i.e. wrap the whole code in an if-else statement, one part of which has the errors, the other part having the productive code)?

 

With thanks for your time. I know it's not easy dealing with "theory" questions, rather than specific examples.

 

Andy

Big EarsBig Ears

sfdcfox,

 

You can ignore my last question - your guidance plus my code tinkering has worked it out. I ensured that the code had nowhere to "go" after the "addError" method by using the if-else statement, so that the last thing it did was present the error.

 

If there are lots of failure conditions, does this mean that the apex code can end up very "nested" (i.e. lots of if-else conditionals stacked within each other). Is there a way around that?

 

Thank you for your original reply.

 

Andy