1

Like we have System class method currentPageReference() to get the reference of current Visualforce Page. Do we have something similar to get the details of running Apex Class?

My requirement is to check status of previous instance of my Batch class when it ran last time. Depending on that I need to set certain attributes of current instance.

Currently I am storing Batch Class Id in custom setting and referring that while querying AsyncApexJob for status. Just wondering if there any other way.

5
  • the solution to this is to throw an exception and parse the stacktrace
    – cropredy
    Jul 29, 2016 at 16:24
  • @cropredy Eh you can query for it by name, which you should know since you're writing the dang thing.
    – Adrian Larson
    Jul 29, 2016 at 16:26
  • @AdrianLarson - sorry; knee jerk response from some prior OP where they wanted to know what trigger was running
    – cropredy
    Jul 29, 2016 at 16:29
  • In short there is no direct way (system method)? Jul 29, 2016 at 16:30
  • @karankhanna No. There is no OOB method to do what you want.
    – Adrian Larson
    Jul 29, 2016 at 16:56

1 Answer 1

2

Notice that the Database.Batchable interface methods all accept a Database.BatchableContext instance (documentation) as a parameter. This object has a method called getJobId().

public void finish(Database.BatchableContext context)
{
    system.debug([SELECT ApexClassId FROM AsyncApexJob WHERE Id = :context.getJobId()]);
}

Otherwise, you can query for it. You can write a system wide cache or query for the specific record.

static Map<String, ApexClass> cache
{
    get
    { // Lazy Load Pattern
        if (cache == null)
        {
            cache = new Map<String, ApexClass>();
            for (ApexClass classInstance : [SELECT Name FROM ApexClass])
                classes.put(classInstance.Name, classInstance);
        }
        return cache;
    }
    private set;
}
static Id currentClassId
{
    get
    { // Lazy Load Pattern
        if (currentClassId == null)
        {
            currentClassId = [
                SELECT Id FROM ApexClass
                WHERE Name = '<current_class>'
                LIMIT 1
            ].Id;
        }
        return currentClassId;
    }
    private set;
}
6
  • Thanks Adrian but I need to do this check in constructor of Batch class hence no context. Jul 29, 2016 at 16:08
  • @karan Please explain why it has to be in the constructor.
    – Adrian Larson
    Jul 29, 2016 at 16:09
  • I need to build my SOQL on the basis of that so it has to be before Start method. Jul 29, 2016 at 16:11
  • You're still being excessively vague. What are you trying to do? Please edit your post to include a query, or something specific enough to give a concrete recommendation.
    – Adrian Larson
    Jul 29, 2016 at 16:13
  • sorry, I was just avoiding to make my question complicated. I will add more details meanwhile let's assume it is usual Apex class (not implementing any interface) and within its method (custom or constructor) can I get the Id of that Apex class? Jul 29, 2016 at 16:17

You must log in to answer this question.

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