Apex/VisualForce Tips & Tricks
All those wires under the hood not making sense? Lick your fingers, grab two of them, and hope for the best...
ISSUE: Can't Express Date or DateTime Literals in APEX
-
date MyDate = Date.valueOf('2008-12-30'); //Apex representation of December 30th, 2008 literal, assigned to a date variable called MyDate -
datetime MyDateTime = datetime.newInstance(2008, 3, 24); //Use this if you need to set a datetime value -- this would be March 24 2008, at midnight. (You can also include hour, minute, and second parameters -- see Apex Language reference under DateTime methods.)
ISSUE: Need to Test Apex DML Statement for Success/Failure/etc.
-
Database.SaveResult SR = database.update(MyContact); //Updates the MyContact record, assigning the result of the DML operation to the Database Object SR
-
if(!SR.isSuccess){} //Now we can test the SR object for success or failure, in this code, we're checking for failure by negating the isSuccess method of the object: "If SR is NOT a success, execute the following code..."
