Questions tagged [reflection]

Reflection allows a developer to essentially modify the behavior of the code at runtime. Currently there is little support for reflection in Salesforce.

Filter by
Sorted by
Tagged with
1 vote
4 answers
3k views

Can you check the instanceOf an object dynamically?

Updated after several answers and comments, to clarify the requirements I have an Open Source library and am looking to implement a method that will check if an object is an instance of a given type. ...
Rob Baillie's user avatar
1 vote
0 answers
666 views

Should it still be possible to access public classes belonging to managed packages?

There is a critical update, Restrict Reflective Access to Non-Global Constructors in Packages When this critical update is enabled, regardless of API version, you can initiate only Apex classes that ...
Jeff's user avatar
  • 909
0 votes
2 answers
586 views

How can I get dynamic API name in apex?

I have an question,I created a FieldSet named Detailed_Content in several sObjects,now I want get one of sObjects's Detailed_Content FieldSet,like the following code: public List<Schema....
karuizawa's user avatar
1 vote
1 answer
2k views

How can I create a Map<Id, Account> from a List<sObject> parameter when I know all the records are Accounts?

I'm doing some work with the ESAPI Library. In particular, when updating records it is used to enforce FLS with the following method: global SFDCAccessControlResults.UpdateResults updateAsUser( ...
Daniel Ballinger's user avatar
1 vote
1 answer
7k views

Get Attributes from an Apex Object Dynamically

Is it possible to get the name of the attributes of an apex class? I have a class below, is it possible to get the name of its fields at runtime? public class MyObjectApex { public String name {...
user3582583's user avatar
1 vote
2 answers
130 views

Send object name into a function

I was trying to pass an object name to a function as an String but I was 99% sure that it won't work and it didn't work. So I am wondering how is it possible to send an object name to function. void ...
ReshaD's user avatar
  • 171
3 votes
1 answer
485 views

Managed package cannot see a class from another managed package

We have a following setup: a managed package pkg_a which provides a global virtual class Cls_A to be extended a managed package pkg_b which contains a global class Cls_B that extends pkg_a.Cls_A a ...
Ivan Vrtarić's user avatar
7 votes
2 answers
2k views

Dynamically calling methods in Apex

Assume this simple class in a managed package namespaced PKG: global class Semaphore { private static Boolean m_isLocked = false; global static Boolean lock() { m_isLocked = true; } ...
Mossi's user avatar
  • 3,906
7 votes
1 answer
3k views

Type.forName(namespace, classname) returns null in managed package

I'm trying to pass a class name from a subscriber org up into a managed package, to make the package extensible. The package is going to use Type.forName(namespace, classname) to create an instance of ...
Aidan's user avatar
  • 13.5k
0 votes
2 answers
863 views

Using getSobject to fetch the parent Account of an Account

I would like to dynamically fetch the parent Account of an Account in a similar fashion to the way it's done in the selected answer to this question. However, this doesn't seem to work in this case. ...
ceiroa's user avatar
  • 1,029
5 votes
3 answers
314 views

How to differentiate LIST apart from HIERARCHY setting in Apex?

My Apex code needs to dynamically reflect on user-defined Custom Settings. But the user could use a List or Hierarchy depending on their needs, so I need to be a bit careful manipulating each type. ...
Matt and Neil's user avatar
2 votes
1 answer
260 views

What are all the public methods for Messaging.SingleEmailMessage

I am wrote a wrapper class that builds Messaging.Email instances but need to test that the Messaging.Email object is built properly. The published API at https://www.salesforce.com/us/developer/docs/...
acdupont's user avatar
0 votes
2 answers
1k views

How can I mock a query of ApexClass for testing?

While recently working on a large project where unit test executions would run upwards of 2 hours, we realized we could get a performance gain by avoiding touching the database (e.g. SOQL and DML) ...
Brian Kessler's user avatar
49 votes
7 answers
59k views

How to get the Runtime-Type of an Object dynamically (for Primitive Data Types and SObjects)

Is it possible to get the Type of an Object (it's not an SObject)? String s = 'test'; doSomething(s); public static void doSomething(o Object) { // need to find out if o is a string or an ...
Uwe Heim's user avatar
  • 28.3k
39 votes
3 answers
32k views

Return Name of class from instance of that class

I need a quick approach to something like: MyClass mc = new MyClass(); String theName = ClassNameProvider.getName(mc); // returns 'MyClass'
max's user avatar
  • 2,419
8 votes
2 answers
5k views

How to get the attribute class list

Does exist on apex something like Class.getDeclaredFields? I need to loop on class attributes dynamically : public class MyClass { public type1 att1; public type2 att2; ... public void function() { ...
Martin Borthiry's user avatar
2 votes
2 answers
2k views

How to get fields in deeper object tree levels with reflection

I'm constructing a dynamic query to get some fields from the database. I use a root object for that purpose and branch out from it. My_Custom_Object__c -> Account__r.Id So if I want to get an ...
ipavlic's user avatar
  • 3,684
10 votes
2 answers
8k views

How to invoke a method of an apex class at run time

We have a usecase where we need to invoke an instance of a class and invoke a method at run time using string. I know with reflection i can instantiate a class. But how do we invoke a method of a ...
Buyan Thyagarajan's user avatar