I want to execute different logic in an update Trigger, depending on whether the Trigger was invoked by an operation through:

  • a Visualforce page

vs. others like

  • standard Page Layout
  • external application using the API
  • future calls or scheduled Apex.

Is there any way to check whether we initially entered the current Execution Context by an operation through a particular Visualforce page.

The Visualforce page is part of a third party Managed Package, so I can't access the code.

2 Answers 2

  • System.currentPageReference() - What is the current page? It will most likely be null for the external API.
  • System.isBatch() - Is the code currently executing inside a batch Apex job?
  • System.isFuture() - Is the code currently executing inside a method annotated with future (or being called by it)?
  • System.isScheduled() - In the code currently being executed inside a scheduled Apex job?
  • 1
    I tried System.currentPageReference(), but it is null when the Trigger is invoked by a record update through the VF page unfortunately....
    – Intigris
    Feb 18, 2014 at 11:29
  • Did you try ApexPages.currentPage() as well?
    – beamso
    Feb 19, 2014 at 12:30
  • 1
    unfortunately 'null' as well
    – Intigris
    Feb 20, 2014 at 20:11

How about hidden check boxes/picklist that will be set depend on what source caused it? then you can find information about source in for example trigger.new (before action) list.

  • I guess you refer to a hidden checkbox/picklist on the Visualforce page? I can't change anything on the VF page, it is contained in a third party Managed Package.
    – Intigris
    Feb 15, 2014 at 13:43
  • No, on object. I think thst's the only way to pass this information Feb 15, 2014 at 13:47
  • How do I then get this field on the Object populated with the source that invoked the trigger?
    – Intigris
    Feb 15, 2014 at 14:04
  • My idea was to set varible in code (future method - apex code, controller - apex code, external application - apex code, standard layout - no value), but as you said - no access to page/controller.... In such case I have no idea. Can't you replace this page? Feb 15, 2014 at 14:21
  • 1
    Well, you could consider the negative case -- create a static Boolean variable isManagedPkgExecuting , default to TRUE, accessible by the trigger, all VF controllers under your control, all REST services under your control, all future methods under your control and so on. The classes under your control set the static variable to FALSE. Your trigger could interrogate the static variable and if not FALSE, assume that managed pkg is executing -- just before your code finishes executing, set the flag back to TRUE. Not a great solution as requires positive action by all your code
    – cropredy
    Feb 15, 2014 at 19:08

You must log in to answer this question.

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