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
Venkat PenukondaVenkat Penukonda 

How to prevent a web-to-case creation using a before insert trigger?

I am trying to find solution to this scenario:  

User submits a web-to-case from a website.
A before insert trigger on Case object tries to do some validation on Trigger.New and in certain conditions  has to prevent this case to be created.  
I tried using addError() in trigger, but it works only when a CRM user is trying to create Case from within salesforce.  It does not stop web-to-case scenario.

I must use a trigger, not simple validation rules as the validation involves many other objects.

I highly appreciate if anyone suggests a solution to this problem.

- regards,
- Venkat Penukonda.
Satish_SFDCSatish_SFDC
Try throwing an exception manually and then catch it in the before insert trigger to give out a user friendly error message.

regards,
Satish Kumar
Venkat PenukondaVenkat Penukonda
Unfortunately this solution will not work in the web-to-case scenario.
In my scenario, user is submitting the case from a form on an external web site. Even with manually thrown exception, user will not see any thing wrong and case eventually gets created.   My flow in before-insert case trigger is this:

Check for existing contact with matching user details (from web form).
1)  If a matching contact exists, then  link the contact to the case and proceed with creating case.

2) If contact does not exist, create a lead instead and do not create case.

So, I don't want to show any error to user anyway.  All I need is a way to stop creating case in the second condition above.
Venkat PenukondaVenkat Penukonda
I did a work around by converting the trigger to after insert trigger. And in my validation which results in not needing the case (which fired the trigger), I call an @future method to delete the case. It worked.