So I have a scenario where i need to fire validation in UI but the same validation should not run in any where in the code(n number classes).

The reason for doing this is that my system is stabil and I do not want to hinder DML operation on Account by introducing the new validations. There are many places in code where Account get update and by introducing this validation it will stop those code.

What I need is that new validation should fire when a record is saved from UI but do not fire then there is DML from code behind.

Looking forward to it!!


Can this work! - Detect by using apexpage reference if a context originated from VF page and if yes then make a check box true on the record and make my validation check that?

  • Are you just looking to make a field required in the UI and not elsewhere?
    – Adrian Larson
    Nov 7, 2016 at 13:14
  • @AdrianLarson Not only required but also want it to validate like postcode length, city to be mandatory if some other field is not blank etc.
    – user614946
    Nov 8, 2016 at 3:17
  • Just a random thought: can't you combine a validation rule and making a field required on a page layout? For example: only fire the validation rule if something is filled in. Required will make sure that this field is required in UI and then the validation rule will check if the value is valid. This way, if you save a record in the code without this "required" field, the validation rule will not fire
    – Novarg
    Nov 8, 2016 at 10:41
  • @Novarg Thanks for it, this can be done but i don't want my user to fill extra field if its not in there business unit they might not fill that in.
    – user614946
    Nov 8, 2016 at 12:45
  • @AdrianLarson, this link donot answer my question as how to detect if trigger is fired from UI.
    – user614946
    Nov 10, 2016 at 8:54

1 Answer 1


This is a tough one because you cannot detect Ui vs Code Entry points without changing something and working toward a best case solution that even then would potentially introduce issues or edge cases.

A not so foolproof way would be:

  1. In all your apex class entry points update a global static property to indicate that the entry point should skip the validation
  2. Check for that flag in your validation

But that does not cover external data loads and other use cases.

The only way to do it in that case would be to use a single API user or a flag on the user record to bypass the specific validation when records are updated by that user.

Basically You cannot detect if the change was made via the UI without overriding the Edit pages with custom VF pages. You are left with getting creative to find the best workaround

Secondary Question If it is important enough to impose the validation on a user why would it not be important enough to enforce the same validation with other forms of input. Seems to me the priorities are a bit askew. Is the validation really needed then?

  • Right wish there were some global variables similar to $User.uiThemeDisplayed() etc .. to determine the entry point .. it would really help us in detecting the entry points, now we are left with monkey patching the logic ..
    – Sumuga
    Nov 8, 2016 at 4:08
  • @Eric Thanks for the suggestion the thing is that in live we have 100K accounts and if we start that validation for all the old users then it will through error in all the places, I don't want to stop live!, this has to be seamless for the user without changing there perception of the current working, so if they were able to save a account from code then its fine, but same thing cannot be done with UI :)
    – user614946
    Nov 8, 2016 at 5:01

Not the answer you're looking for? Browse other questions tagged .