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
Aslam Iqbal 25Aslam Iqbal 25 

Create alert for an User

I have my client asking below
if an apportunity creation fails, due to any error (validation, trigger, workflow etc), then that should send some alert message or record the information somewhere.


I know flow errors can be sent, or trigger errors can be handled, but in general someone (could be any user, community user, integration user etc) tries to create a record, this fails , how do I capture and send information to specific user or an admin.

I know this can be debugged , that shows there was an error to create record, but is it possible to capture such information and record it elsewhere

thanks
aslam
Best Answer chosen by Aslam Iqbal 25
Andrew GAndrew G
My first response would be to do more testing before deployment, but we will let that go for the minute.

Could we somehow report on any / all errors?
In theory, we would be able to depending on quite a few things.
Basically, you would need to build an Exception extension class that creates the "error record".  The error object would need some fields such as Long Text to hold the Field and Field Value information, an Error field, User field (which may be same as Owner/Created by), Timestamp (which may be CreatedDate).
You would then need to code all your DML events in a Try..Catch.. where the Catch invokes the class to create the "error record".
You would need to override all the Save Action buttons. 
Depending on how your integration works, you could simply put the Exception Extension in that code.  
The creation of the Error Record would need to use the Schema parameters to record field Label and Contents when the Error occurs, plus the error itself.
Look into the getPopulatedFieldsAsMap() under the sObject class.  You could either loop to create a String to paste to a field, or you could loop and build an XML structure if that may be easier to read.
Check out the exceptions class to review how to grab the error message. 

Other may have better ideas, but the issue I see using the above would be that you need to override every option for saving a record.   Unless it is to be only for the integration piece, then incorporate it with your integration code, if it's not in a managed package.

Regards
Andrew

All Answers

Andrew GAndrew G
I would question what is the value in doing this?   If i'm doing an Oppty and it has validation that says "You didn't enter an Amount", and I then update the Amount and Save the record.  What is the business value in knowing that I missed the Amount on the first run, but the validation caught the error and stopped me from saving an incomplete record?  

I would dig deeper to find out what they are really trying to find out.  

Because if a VR catches an error, there is no way to handle an alert other than the existing UI behaviours.  If you want to record that an error occured in the UI because I missed filling out the Amount, then you will need to do something like go to Apex and create a Trigger Handler that does all your validation and then either creates some sort of "error capture" record which will be stored in SF and consume storage space.  Or you do an email which will spam the recipients who will put in a mail rule to ignore/archive/delete all the emails that come in with validation errors.

Additionally, what was a simple Validation Rule to implement, which you could do sneakily in production, will now become a full Apex coding adventure with Sandboxes, Test code, UAT and Deployments - all that associated overhead.

As for Process Builders, when they fail they will email your Admin group.  


At times, as the technical "experts" which the business rely upon, it is up to us to tell the business that what they are chasing may not have the value they think it will.

So, go back to them, ask them "Why?", "What do you hope to achieve with this information/process?" "What is the value / gain to the business?" And then explain the development overhead and decrease in being "agile"/"responsive to business needs".

my 2 cents
Regards
Andrew

 
Aslam Iqbal 25Aslam Iqbal 25
Hi Andrew,

thanks for the quick reply.

let me explain what really happened.
client asked to put a formula field of type text

After adding this , it turnedout to be when length of the formula calculation  exceeded 255 then this started giving an error.
this was found when external user was trying to create an opportunity, client lost an opportunity.
Exact reason was found out this by enabling debugging on user

client realizes these thing could happen as part of the development, hence want to ensure when data is coming in from external resources (android app , community portal etc), and if that record does not get created for any such salesforce error then following will be required.

1. data/information that was sent in some format shoudl be stored in readable format
2. send an error email to designed admi,  if possible including error message and data information as much as possible in the error email.


so it it possible to handle such situations , either on object level or user level (example integration user)



 
Andrew GAndrew G
My first response would be to do more testing before deployment, but we will let that go for the minute.

Could we somehow report on any / all errors?
In theory, we would be able to depending on quite a few things.
Basically, you would need to build an Exception extension class that creates the "error record".  The error object would need some fields such as Long Text to hold the Field and Field Value information, an Error field, User field (which may be same as Owner/Created by), Timestamp (which may be CreatedDate).
You would then need to code all your DML events in a Try..Catch.. where the Catch invokes the class to create the "error record".
You would need to override all the Save Action buttons. 
Depending on how your integration works, you could simply put the Exception Extension in that code.  
The creation of the Error Record would need to use the Schema parameters to record field Label and Contents when the Error occurs, plus the error itself.
Look into the getPopulatedFieldsAsMap() under the sObject class.  You could either loop to create a String to paste to a field, or you could loop and build an XML structure if that may be easier to read.
Check out the exceptions class to review how to grab the error message. 

Other may have better ideas, but the issue I see using the above would be that you need to override every option for saving a record.   Unless it is to be only for the integration piece, then incorporate it with your integration code, if it's not in a managed package.

Regards
Andrew
This was selected as the best answer