I was writing some unit tests just now, and was lazily creating more test methods when I ran across an error on trying to save my unit test.

> Compiling testOLIUpdateManualRollups.cls
> Compile Failed
> testOLIUpdateManualRollups.cls: Invalid identifier: testRollupNew_MRR_Total__c (Line: 364)

To make it very clear, I was attempting to create a new method called testRollupNew_MRR_Total__c

There's more to the test class that I'm writing, but the relevant Apex is simply

static testMethod void testRollupNew_MRR_Total__c(){
    // test as of yet to be written

Now, I know that the keywords/reserved words can't be used alone as identifiers for classes, methods, or variables ('test_Integer_result' and the like are valid identifiers), but why isn't an identifier ending in __c valid for methods?

I can't find anything on this restriction in the documentation for method declaration, and a cursory googling doesn't turn up anything either.

Upon further testing, it appears that __c cannot appear in any portion of the method name. Underscores at the start of an identifier also appear to be verboten, though this is legal in Java, and I can't find any documentation on that restriction either.

This strikes me as a either an oversight in the formal grammar for the Apex language or a bug in Salesforce's compiler (perhaps transpiler might be more accurate?). I can't see any situation where the compiler should expect a custom field identifier anywhere in a method declaration, so I'm at a bit of a loss here.

I'll be working around this by removing the __c from the method name. Just wondering if anybody else can confirm this or can offer an explanation.

  • It's not that surprising. I believe any identifier with __ is illegal. I can't find a reference for it either, however.
    – Adrian Larson
    Feb 9 '16 at 20:52
  • Oh, I was under the impression that a double underscore could be part of a valid identifier. Just tested that, and it appears to be invalid.
    – Derek F
    Feb 9 '16 at 20:56
  • 1
    @AdrianLarson That would indicate a bug in Salesforce's parser or syntax analyzer, or perhaps even the grammar for the Apex lexer. I can see why __c may be problematic at the end of a method identifier. If the lexer adheres to the maximal munch principle though, there's no excuse for making __ illegal (well, at least not in the lexer).
    – Derek F
    Feb 9 '16 at 21:12
  • 1
    i'll bet if you go through the trouble of reporting this as a bug, it will come back as WAD - working as designed
    – cropredy
    Feb 10 '16 at 0:03
  • 1
    @DerekF I see that it is documented for the class name but not for the method. developer.salesforce.com/docs/atlas.en-us.api_meta.meta/…
    – manjit5190
    Feb 10 '16 at 18:22

Two consecutive underscore characters in Apex are used to separate namespace of a managed package from the inner name inside of a managed package.

This is the reason why they are forbidden in Apex Class names, methods, variables and properties.

Your Answer

