• Rahim Uddin 19
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi All,
I have a batch job which is updating the records, init i have one future method.Now i am running the batch from execute anonymous. But in the debug log future method is running in API user context. its showing sumitted by that user.

Could anyone please explain what would be the reason for this.

Thanks,
Rahim
Apex code should run in system context (except when running executeAnonymous, or using "with sharing", or testing with "runAs").

Specifically, @future calls should run in system context (or, at least, in the same context as their caller).

Instead, they are running in the user context.

Here's a quick example of an api method that queries for a private contact belonging to 'user1':

Code:
 1  Webservice static void now_and_later() {
 2    queryPrivateContact();
 3    queryLater();
 4   }
 5  public static void queryPrivateContact() {
 6    Contact c = [select Name from Contact where Id = '0037000000cWgTXAA0'];
 7    System.debug(c);
 8    }
 9  @future static void queryLater() {
10    queryPrivateContact();
11    }

 If I call this api method as 'user1', everything works fine (here's the debug output, from the non-future call at line 2):

Code:
$ perl api.pl -u user1
--------------------------------------------------------------------------------
Calling 'i.aa_api.now_and_later' as user1
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DEBUG LOG:
20081118002023.813:Class.i.aa_api.queryPrivateContact: line 7, column 9:
        Contact:{Name=Carrie Sloan, Id=0037000000cWgTXAA0}
--------------------------------------------------------------------------------

 
If I call it as 'user2', who is a standard user & thus isn't normally able to see the private contact, the line 2 call to queryPrivateContact works fine (because the code is running in system context).  As a result, the debug output is what you would expect:

Code:
$ perl api.pl -u user2
--------------------------------------------------------------------------------
Calling 'i.aa_api.now_and_later' as user2
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
DEBUG LOG:
20081118002029.727:Class.i.aa_api.queryPrivateContact: line 7, column 9:
        Contact:{Name=Carrie Sloan, Id=0037000000cWgTXAA0}
--------------------------------------------------------------------------------

 However, an exception is thrown when the @future method queryLater runs, and the developer org gets an an error email:

Code:
Apex script unhandled exception by user/organization: 00570000001IdET/00D70000000Jqpv

Failed to invoke future method 'static void queryLater()'

Debug Log:
System.QueryException: List has no rows for assignment to SObject

Class.i.aa_api.queryPrivateContact: line 6, column 21
Class.i.aa_api.queryLater: line 10, column 9

 
This shows that the @future method is running in the user context, rather than the system context.  As a result, the SOQL in "queryPrivateContact" returns empty, and thus we get the "List has no rows for assignment" error.



Message Edited by jhart on 11-17-2008 04:32 PM

Message Edited by jhart on 11-17-2008 04:32 PM
  • November 18, 2008
  • Like
  • 0