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
Karan Khanna 6Karan Khanna 6 

Running class Id in apex

Hi Team,

Like we have System class method currentPageReference() to get the reference of current VFPage, 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.
 
Best Answer chosen by Karan Khanna 6
Ishwar ShindeIshwar Shinde
Hi Karan,

I think, You can use ApexClass object in soql to get class id.

Current class name, you can identify by hardcoding the string variable in class, or try below snippet to provide it dyanamically -

String CurrentClassName =String.valueOf(this).substring(0,String.valueOf(this).indexOf(':'));

'this' will provide the current instance of class.

Hope it is helpful !!☺

 

All Answers

Ishwar ShindeIshwar Shinde
Hi Karan,

I think, You can use ApexClass object in soql to get class id.

Current class name, you can identify by hardcoding the string variable in class, or try below snippet to provide it dyanamically -

String CurrentClassName =String.valueOf(this).substring(0,String.valueOf(this).indexOf(':'));

'this' will provide the current instance of class.

Hope it is helpful !!☺

 
This was selected as the best answer
Karan Khanna 6Karan Khanna 6
Thanks Ishwar for response, but this will give me name of my class not Id, I would have to write SOQL on ApexClass to get Id through this approach.
I am trying to avoid SOQL.
Ishwar ShindeIshwar Shinde
I don't think without soql on apex class you can achieve it. You can try one thing, instead of checking of class id, directly identify the record by Apexclass.Name ( by reference field). Not sure if this helps you..
Karan Khanna 6Karan Khanna 6
unfortunately It won't serve the purpose, seems like SOQL is the only option. Thanks!