21 votes
Accepted

Purpose of an inner class

I think this question is way too broad for one answer to cover. I suggest you look at some libraries out there to see how they use inner classes. There are so many use cases. It would be really ...
Adrian Larson's user avatar
  • 149k
14 votes

Purpose of an inner class

Their primary use in my experience is to logically group classes together and/or reduce the number of top-level classes. This starts to matter when your code base grows into hundreds of top-level ...
Keith C's user avatar
  • 135k
12 votes

What's It Called: Inner Class Without Sharing?

When this pattern is done unintentionally, it's a "privilege escalation bug," which is a major security concern. This design pattern will at least raise red flags with the security review team if this ...
sfdcfox's user avatar
  • 481k
11 votes
Accepted

Confirmation that (inner) classes are still not fully supported in @AuraEnabled methods?

Unfortunately I wasn't able to make it work using the class directly, either with an inner class or even a top level class. Using the second solution with a String attribute in your AuraEnabled method ...
Fabien Taillon's user avatar
9 votes
Accepted

What's It Called: Inner Class Without Sharing?

I believe the term you're looking for is Trampoline because it allows a developer to "Bounce" up into a higher security context. Trampolines should be written very carefully and validate their ...
Mgetz's user avatar
  • 288
9 votes
Accepted

Inner class does not allow maps

The issue is with the spellings of the access modifier for your second map in the inner class. Please change it to public from pubilc and it will resolve your issue. I am successfully able to save the ...
Bilal Malik's user avatar
8 votes
Accepted

Inner Classes/Interfaces

It means that an inner class can't have an inner class in Apex. So a structure like this is legal and common, especially when implementing patterns like wrapper classes or dependency injection: ...
David Reed's user avatar
  • 92.1k
8 votes
Accepted

An inner interface inside an outer interface is not supported in Apex?

As the documentation says in this entry Differences Between Apex Classes and Java Classes - Inner classes and interfaces can only be declared one level deep inside an outer class. So you are ...
mnunezdm's user avatar
  • 890
7 votes
Accepted

Possible to declare static constants in an inner class?

No, you cannot make them static. You can use inner-class constants by simply dropping the static modifier, however. public class Stuff { public class Letters { public final String A = ...
Adrian Larson's user avatar
  • 149k
7 votes
Accepted

For loop within for loop

You should never do DML statements (such as insert) inside loops, this will easily get you off limits. Create a List outside the loop, iterate over your trigger in the first loop, create new records ...
Mauricio Oliveira's user avatar
7 votes
Accepted

Managed package patches - can a public inner class be added?

Yes. We have taken this approach with our managed package when back-porting features to patches that added new top-level classes in our master branch. Obviously there are limits to what can appear in ...
Phil W's user avatar
  • 34.1k
6 votes
Accepted

Nested Inner Classes creation in an apex class

You only have to read the first paragraph of the Apex Developer Guide documentation on Apex Class Definition to answer this question (emphasis mine): Apex Class Definition In Apex, you can ...
Adrian Larson's user avatar
  • 149k
6 votes
Accepted

Code Coverage on Inner Class/Variable Declarations

The line containing strFirstName does not require coverage because you only declare the variable, but do not instantiate it. Notice how strTitle requires coverage, but there you perform assignment. ...
Adrian Larson's user avatar
  • 149k
6 votes
Accepted

Problem using Protected modifier to expose method to inner class

You can use a much simpler MVR here: public class Demo { protected String variable; protected void method() { } public String property { get; protected set; } } This behavior does seem ...
Adrian Larson's user avatar
  • 149k
6 votes
Accepted

Adding a Wrapper Class to an External Class

Somewhere else in the local context, there is a variable declaration that's shadowing the class Models. Example: public class TestQ242685 { public class InnerClass { } public static ...
David Reed's user avatar
  • 92.1k
6 votes
Accepted

How to make a Json object a subclass

Just pull the definitions out of the class: global with sharing class mainclass { class cls_SalesProductSections { public String SectionCode; // public String SectionName; // public ...
sfdcfox's user avatar
  • 481k
5 votes

Adding a Wrapper Class to an External Class

You seem to have a name shadowing here. This code is adding CheckItemClass objects in a List<CheckItemClass> which is most likely declared in your controller as: List<CheckItemClass> ...
Jayant Das's user avatar
  • 30.3k
5 votes
Accepted

Best Practice deserialize a JSON to avoid heap size limit

The heap limit is more of a "suggestion" than a hard rule. Having 13 MB of used heap isn't a big deal if you only do so briefly. However, there is a hard limit for strings; one single string cannot ...
sfdcfox's user avatar
  • 481k
5 votes

An inner interface inside an outer interface is not supported in Apex?

The main reason why this is not supported, is that Apex isn't Java. When salesforce.com sat down and started designing Apex, they created a BNF that they felt could be implemented in a reasonable ...
sfdcfox's user avatar
  • 481k
4 votes
Accepted

Implementing CompareTo In An Inner Class That Needs Outer Class Data

You can pass in a reference to the controller class in e.g. the constructor: public class WEvent implements Comparable { private ActivityListAttemptController controller; public Event e {get; ...
Keith C's user avatar
  • 135k
4 votes
Accepted

Why Don't Visualforce Wrapper Class Getter/Setter Methods Not Exist?

Since source, Year and Field are variable then Just use p.source, p.Year and p.Field String test1 = p.getSource(); Integer test2 = p.getYear(); String test3 = p.getField(); Instead try String test1 ...
Ratan Paul's user avatar
  • 22.5k
4 votes
Accepted

Deserialization using Apex class generated using JSON2Apex gives null for inner values

Your JSON doesn't match your data structure. In Apex, you have public cls_Transaction[] Transactions; in the plural, but your JSON has "messages":{ "Transaction":[ { in the singular.
David Reed's user avatar
  • 92.1k
4 votes
Accepted

LWC inner class property access issue

To use @wire to call an Apex method, you must annotate the Apex method with @AuraEnabled(cacheable=true). Certainly, you have forgotten the cacheable=true. Also in the html file, instead of using ...
dibocor's user avatar
  • 1,307
4 votes
Accepted

How to test an inner class?

Simply create an instance of inner class and populate its properties by test data: OuterClass.ExistingAccountResult result = new OuterClass.ExistingAccountResult(); result.salesforceaccountid = // ...
Oleh Berehovskyi's user avatar
4 votes

How do I call this method inside of inner class

Your outer class is PT_Interface, so you need to call your inner class in this format PT_Interface.PT_Request_Model. And you can't call your constructor without a parameter since you have defined your ...
Hengky Djapar's user avatar
4 votes

Wrapper class showing error 'Inner types are not allowed to have inner types' in apex

You need to move the definition of ValueRanges outside of the fromJSON class (same as you have for values). So your code should be something like: public class GetTheDetails { // Wrapper class ...
user1067017's user avatar
4 votes
Accepted

Apex @RestResource returning result inner class exposes private attributes

The docs do (kind of) call this out: (my emphasis) You can use user-defined types for parameters in your Apex REST methods. Apex REST deserializes request data into public, private, or global class ...
Daniel Ballinger's user avatar
3 votes

Adding a Wrapper Class to an External Class

Name shadowing can take many forms... Here is my personal favorite (as I have made this mistake more than once wrto name shadowing) from the lazy loading pattern... public Contact[] contacts { get ...
cropredy's user avatar
  • 69.9k
3 votes
Accepted

How to use inner class / wrapper class as scope in Apex Batch

You are using Custom Iterator. So you need to implement same as well. InvoiceHeaderBatch implements Database.Batchable<InvoiceHeaderListController.InvoiceData> Here is a complete code sample ...
Tushar Sharma's user avatar
3 votes

Confirmation that (inner) classes are still not fully supported in @AuraEnabled methods?

Spring 19 Update : I just stumbled upon this question and I recently used something similar in Spring 19 so thought might answer this. Inner classes are supported now, you have to use setter and ...
Pranay Jaiswal's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible