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
GoForceGoGoForceGo 

Does a trigger know whether it is being called from a webservice call or through salesforce UI.

Is there anyway for a trigger to know if is being executed in the context of a user interface or a web service?

What i want to do is this:

I am extracting pdf data and putting it in salesforce objects through a client application using webservice. I don't want to give errors -
I want to just insert the data and send an e-mail to the user and letting them know if there are errors. When they go into salesforce, they will see the data.
When they edit the data, saving it would require fixing the errors - I would use .addError when used in the UI context.





BoolsEyeBoolsEye

You may use the trigger context variable 'isExecuting'.

From the Apex Developers Guide:

"isExecuting

Returns true if the current context for the Apex script is a trigger, not a Visualforce page, a
Web service, or an executeAnonymous call."

Maybe this helps. I never used it, though.

//BoolsEye
GoForceGoGoForceGo
Thanks!

I will try playing around with it.

I am still wondering what does Trigger.isExecuting means.

Obviously the trigger is executing when you look at the variable within the context of a trigger...

A general Apex script (a webservice method or any other method in an Apex class) has no access to the trigger object, so Trigger.isExecuting is meaningless in that context.




GoForceGoGoForceGo
Doesn't seem like this would work.

An apex script can look at whether it is being called from within a trigger.

As an example, an Apex class might have a method. Within the method one can check whether this method is being called from a trigger

public class foo {

public static void MemberMethod() {
    boolean IsExecuting = system.trigger.isExecuting.
}

What I really need is for a trigger to know whether it is being called from salesforce UI or a webservice method.

One hack would be to have a field on every object called "isSalesForceUI". When called from Web service, I would set it as false.

I would output my addError messages only when isSalesForceUI is true.

The only probably with above is that a user would have to explicitly select isSalesForceUI as true (in the salesforce UI) to see the error messages....don't sound good.


GoForceGoGoForceGo
Would the following work? The idea is that a static variable is used to communicate that this is a webservice call or UI call?

Code:
global class invokeWebservices {

  boolean static iswebservice = false;

  Webservice static void InsertFooRecord() {
         isWebService = true;
         Foo__c foo = new Foo__c;
         insert foo;
 
} }

The trigger for Foo would check if iswebService is true:

Code:
Code:

trigger validatefoo on Foo__c (before insert) {
    if (!invokeWebServices.isWebService) {
       all validations with AddError message
    }
}

 
    


 



 


BoolsEyeBoolsEye

Yes, this would work,  but you should change your function to make sure that the variable is set back to false in any case of a failure.

Code:
  Webservice static void InsertFooRecord() {
         Foo__c foo = new Foo__c;
 try {
         isWebService = true;
         insert foo; 
 } finally {
         isWebService = false;

 }
}


 

BoolsEyeBoolsEye
What about this ? This is how this variable should be used I think.
 
Code:
trigger myTrigger on myObject (before insert)  {
 if (Trigger.isBefore) {
  if (Trigger.isInsert) {
   if (Trigger.isExecuting) {
    //Not called from a webservice
    foo.MemberMethod(true);
   } else {
    //Called from a webservice
    foo.MemberMethod(false);
   }
  }
 }
}

public class foo {

public static void MemberMethod(Boolean isSalesForceUI) { 
}

 
GoForceGoGoForceGo
Trigger.isExecuting doesn't really tell you whether you are being called from a webservice - it just tells you whether you are right
now in the middle of executing a trigger.

In other words, if a web service inserts a record in the database, the insert trigger can't tell whether web service caused the trigger to execute or was it
done from UI.


GoForceGoGoForceGo

In the code below, Since isWebservice is a shared static variable, is it shared
by all threads within an Org? Conceptually, couldn't another thread/user also set "isWebServices" as false before the record actually gets inserted - see below.



Code:
  Webservice static void InsertFooRecord() {
         Foo__c foo = new Foo__c;
 try {
         isWebService = true;
//could another thread set it as false here before the actual insertion happens?
 insert foo; } finally { isWebService = false; } }