Skip to content
forked from ChuckJonas/wtfapex

A list of funny and tricky Apex examples

License

Notifications You must be signed in to change notification settings

hiepllp/wtfapex

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

What the f*ck Apex?

A list of funny and tricky Apex examples

Inspired by wtfjs

When a boolean is not a boolean

Boolean b;
if(b!=true) system.debug(‘b is not true’);
if(b!=false) system.debug(‘b is not false’);

See Advanced Apex Programming in Salesforce for explination.

String compare is Case-insenstive (except when it's not)

String x = 'Abc';
String y = 'abc';
System.assert(x == y); // passes
System.assertEquals(x, y); // fails

Explanation

Shadowing System (global) classes

Nothing prevents you from recreating a class with the same name of one that exists in the System (default) namespace.

public class Database {
    public static List<sObject> query(String qry){
        System.debug(qry);
        return null;
    }
}

Running Database.query('foo') will call our new class (essentially override the Database methods!?)

source

System can have ambiguous returns types

Database.query is one of many cases where the salesforce "System" namespace doesn't play by it's own rules. It can either return a List<SObject> or a single SObject. No Casting required.

Try writing your own method to dot this and you'll get a error:

Method already defined: query SObject Database.query(String) from the type Database (7:27)

Fun with Hashcodes

Enums in batch

Objects in hascodes

JSON Serialization

  1. There way to control automatic serialization of object properties (like [JsonProperty(PropertyName = "FooBar")] in C#)
  2. There are reserved keywords that you can't use as property names.

Meaning the following cannot be parsed or generated using JSON.deserialize or JSON.serialize:

{
   "msg": "hello dingus",
   "from": "Dr. Dingus"
}

Work Around

Generics (parametrized interfaces) exist but you can't use them

Apperently once upon a time, generics were part of apex. However, they have since been removed (with the exception of system classes (List<>, Batchable<>, etc).

Why would you want generics when you're OS has perfectly good Copy & Paste functionality built right into it?

Vote for Generics

Initializing Abstract Classes

public abstract class ImAbstract {
    public String foo;
}

ImAbstract orAmI = (ImAbstract) JSON.deserialize('{"foo":"bar"}', ImAbstract.class);
System.debug(orAmI.foo);

source

Polymorphic Primatives

Object x = 42;
System.debug(x instanceOf Integer); // true
System.debug(x instanceOf Long);  // true
System.debug(x instanceOf Double);  // true
System.debug(x instanceOf Decimal); // true

Source Dainel Barrileger

Since Fixed

Thankfully these WTF's have since been fixed by Salesforce. We'll keep them documented for historical purposes (and entertainment).

Mutating Dates

https://twitter.com/FishOfPrey/status/869381316105588736

More hashcode fun

https://twitter.com/FishOfPrey/status/1016821563675459585 https://salesforce.stackexchange.com/questions/224490/bug-in-list-contains-for-id-data-type

About

A list of funny and tricky Apex examples

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published