2

I have a trigger that fires for an object before insert/update to validate that a date range entered does not overlap any existing records. That appears to be working OK after a bit of work.

I have now added a Flow to enable the users to input the data for a new record from their lightning homepage. That works well except that when the record create runs it appears to fire the trigger which if the dates overlap and an error is returned the flow returns an unfriendly error:

This error occurred when the flow tried to create records: FIELD_CUSTOM_VALIDATION_EXCEPTION: You can not book these dates as they overlap existing records. For details, see API Exceptions.:

I can't find a way to capture the error in the flow so a user friendly one can be presented instead. The only thing I can think of is to put the trigger code in a class and use the class in the flow, would require changing the code.

Any ideas or better ways?

2

I had the same issue, my message looked a bit different, but you can easily adjust the formula below.

Creating the Formula

You have to create a formula variable in your flow and enter the formula below formula friendlyFaultMessage

MID(
 {!$Flow.FaultMessage},
 FIND('FIELD_CUSTOM_VALIDATION_EXCEPTION: ', {!$Flow.FaultMessage} )+34,
 (
   LEN({!$Flow.FaultMessage} ) - 
   (FIND('FIELD_CUSTOM_VALIDATION_EXCEPTION: ', {!$Flow.FaultMessage} )+34)
 ) - 
 (
   LEN({!$Flow.FaultMessage} ) - 
   FIND('. For details, see API Exceptions', {!$Flow.FaultMessage} )
 )
)

This will give you the message between the default error message and only show the exception message. For ValidationRules you have to use this as the last thing.

FIND('. You can look up ExceptionCode', {!$Flow.FaultMessage} )

Displaying the Error

I check the message before displaying it and if it contains the string

FIELD_CUSTOM_VALIDATION

Then I will display it to the customer with a screen

fault path from DML element

(disabling the previous button on that screen is also a good idea IMO)

0

Flows that perform DML with Record {Create|Update|Delete} or their Fast {Create|Update|Delete} friends can include a second outbound connection from the flow element called a fault path. The fault path can take some other action to handle or display (in a screen-based flow) the error to the user.

Flow example

Within the fault path element sequence, you can access the system variable {!$Flow.FaultMessage} to get the error message from the trigger.

Here is a thorough, complex example, and a documentation reference.

1
  • Thanks for the reply, I already have fault handling in the flow so the error from the trigger doesn't show as an unhandled error, the fault message would appear to be displayed but is not particularly user friendly. I'll review the link you included. Thanks Apr 12 '18 at 13:23
0

It looks to me like it is a validation rule on the object that is throwing the error (thus the FIELD_CUSTOM_VALIDATION_EXCEPTION error).

In this case, you can use a decision element after the screen element to evaluate if the dates overlap, and then you can display an error message in the flow.

I dont think your trigger is what is throwing this error message. If I'm wrong, and it is your trigger that is throwing the exception, then you might be able to build out a formula that could parse an apex exception and present it in a user friendly format.

4
  • Thanks for the reply, that particular error is thrown in the trigger, there are validation rules which I have duplicated in the flow so they don't get thrown on record create. The problem is that I need to validate the recorded being created does not overlap any existing records which was the job of the trigger. If only there was a graceful way to trap errors in the flow thrown by the object or triggers... I'm now looking at ways to use the trigger code in an apex class and call that in the flow so that the record create won't run if the class returns an error. Apr 13 '18 at 11:53
  • I think you could do a fast look up prior to the insert just using the flow and see if there are any matching records that way. I have found that if you evaluate a fast lookup equals true in a decision element, that will be true if records were found. I've never looked for documentation ok this...so ymmv. But i use that a lot.
    – gorav
    Apr 13 '18 at 11:57
  • 1
    Thanks again, unfortunately I can't base my decision simply on the records returned. I have to manually loop and check for half days clashing at the start or end of the range. Ideally I just want the trigger error that works fine when creating the records from a view or parent object to not throw an api exception error when the records are created from Flow instead. My trigger works as it should... I thought about using a class and calling from flow until I realised you can't oass multiple parameters to a class from flow. Apr 16 '18 at 9:19
  • gotcha, trigger makes sense! when using invocable methods in apex you can pass multiple invocable variables.
    – gorav
    Apr 16 '18 at 10:55

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.