Skip to content

Instantly share code, notes, and snippets.

@patmcclellan
Created October 27, 2022 23:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmcclellan/3a38dfe07ffab7011ab67009d8ffc5b9 to your computer and use it in GitHub Desktop.
Save patmcclellan/3a38dfe07ffab7011ab67009d8ffc5b9 to your computer and use it in GitHub Desktop.
RAD2 JSON demo (static vs. non-static methods)
//get a Contact
Contact c = [
SELECT Id, Name, AccountId, Account.Name, Email, Phone
FROM Contact
LIMIT 1
];
// METHOD SIGNATURE
// public static String serializePretty(Object objectToSerialize, Boolean suppressApexObjectNulls)
// NOTE: the signature says it's a static method, so that means you use it by
// calling the name of the class (JSON) and providing the variable name as the method argument
String jsonString = JSON.serializePretty(c,true);
System.debug('🔹 JSON: ' + jsonString);
// In contrast, the List method size() is a NON-STATIC method with
// signature, so you invoke it by using the name of the instance variable
// public Integer size()
List<String> fruits = new List<String>{'apple','grape','cherry'};
Integer howManyFruits = fruits.size();
System.debug('🍒 size: ' + howManyFruits);
//Next line throws an error. Error: Method does not exist or incorrect signature: void size(List<String>) from the type List
// System.debug('alt size: ' + List.size(fruits));
List<String> pets = new List<String>{'dog','cat','bird','fish'};
Integer howManyPets = pets.size();
System.debug('🐶 size: ' + howManyPets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment