Preface – This post is part of the Core Apex Tools series.
Nothing you code in Apex is actually saved to Salesforce until you use a DML statement! A DML statement is like the “Save” button you see on Salesforce.com – in fact, that button is powered by a DML statement in the background!
If you know how to use one DML statement, you know how to use them all! And even though there are six different DML statements, you really only use two of them.
Lead myLead = new Lead();myLead.FirstName = 'David'; myLead.LastName = 'Liu'; myLead.Company = 'Unemployed';insert myLead;
There are two important things going on in the code above. First, notice how we use the “new Lead()” syntax to create an sObject from scratch. You’ll use a similar syntax no matter what you’re trying to create.
Next, note that the Lead is not created in Salesforce until the “insert” DML statement on the last line of code above! So without that line of code, poor David Liu would only exist digitally in the Apex software world… sweet!
myLead.Status__c = 'Broke'; myLead.Budget__c = 0;update myLead;
Most of the time you’ll be using an Update DML. It’s pretty straightforward as you can see!
This David Liu lead is broke and useless! Time to have another talk with marketing about the quality of their leads…
Next post: SOQL and Apex – a match made in Heaven!