Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Creating, updating, and deleting records

October 13, 2013

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.

DML statements need an sObject or list of sObjects to save. The general format goes like this:

DML_statement sObject;

INSERT

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!

UPDATE

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!

DELETE

delete myLead;

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!

12 Comments
Pranay Jhawar
October 29, 2020 @ 7:00 am

Pranay:
objects-
1. Folder(folder name,StartDate,EndDate,EsimatimatedDuration)
2. Project(Project Name ,Folder Name)
3. Board(Board Name,Project Name)
4. Card( Card Name, Board,Activity,MilleStone)

Relationships…..
1. lookup —
i- Folder -> Project
ii- Board -> Card.

2. Master —
i. Project -> Board.

Need to create 5 folder records…..

each folder contain 5 project… each project contain 5 board and each board contain 5 card record.

Reply
Pranay Jhawar
October 29, 2020 @ 6:59 am

Pranay:
objects-
1. Folder(folder name,StartDate,EndDate,EsimatimatedDuration)
2. Project(Project Name ,Folder Name)
3. Board(Board Name,Project Name)
4. Card( Card Name, Board,Activity,MilleStone)

Relationships…..
1. lookup —
i- Folder -> Project
ii- Board -> Card.

2. Master —
i. Project -> Board.

Need to create 5 folder records…..

each folder contain 5 project… each project contain 5 board and each board contain 5 card record.

Reply
Arumoy Datta
January 27, 2020 @ 10:48 pm

Hi David,

I have been going through your course curriculum and i am stuck at week 6: Homework. Can you guide me in your style of simple explanation on how to go about in solving this problem. Don’t know how to write it?
I am a marketing guy of 9 years exp and was recently laid off and hence now i planning to shift domain and have been really inspired by your journey. Kindly help.

Reply
Art
May 15, 2019 @ 6:51 am

Please add upsert, it’s actually really usefull

Reply
Anonymous
March 9, 2015 @ 2:26 am

trigger try12 on BMCServiceDesk__Incident__c (after update)
{
for(BMCServiceDesk__Incident__c inc1:trigger.old)
for(BMCServiceDesk__Incident__c inc2:trigger.new)

for(BMCServiceDesk__Incident__c inc:trigger.new)
{
//retriving current incident id
BMCServiceDesk__Incident__c id1 =[select id from BMCServiceDesk__Incident__c where id in:trigger.new];

//task object and task id related to current incident whose status is not closed
LIST task = new LIST();
task=[select id,name,BMCServiceDesk__FKIncident__c,BMCServiceDesk__FKStatus__c from BMCServiceDesk__Task__c where BMCServiceDesk__FKIncident__c=:id1.id and BMCServiceDesk__FKStatus__c!=:’a3w2800000002wKAAQ’ ];

//problem object and problem id related to current incident whose status is not closed

//geting previous status and new current status

//checking for previous status is not equal to close/reswolved and current status is equal to resolved

if(inc1.BMCServiceDesk__FKStatus__c !=’a3w2800000002wKAAQ’|| inc1.BMCServiceDesk__FKStatus__c !=’a3w2800000002x9AAA’ && inc2.BMCServiceDesk__FKStatus__c ==’a3w2800000002x9AAA’ )
{

//updating staus of task to closed status whose incident status is resolved
for(BMCServiceDesk__Task__c taskupdate : task)
{
taskupdate.BMCServiceDesk__FKStatus__c = ‘a3w2800000002wKAAQ’;
update taskupdate;
}system.debug(‘hellllo u r in if’);

//probelm id
LISTproblem=[Select id,BMCServiceDesk__FKStatus__c from BMCServiceDesk__Problem__c where BMCServiceDesk__FKStatus__c!=:’a3w2800000002wKAAQ’ and (id in(select BMCServiceDesk__FKProblem__c from BMCServiceDesk__Incident_Problem_Link__c where BMCServiceDesk__FKIncident__c=:id1.id))];
LISTproblem1=new LIST();

for(BMCServiceDesk__Problem__c probupdate : problem)
{
probupdate.BMCServiceDesk__FKStatus__c = ‘a3w2800000002wKAAQ’;
problem1.add(probupdate);
//update probupdate;
}update problem1;

//change id
// List change = [Select id,BMCServiceDesk__FKStatus__c from BMCServiceDesk__Change_Request__c where BMCServiceDesk__FKStatus__c!=:’a3w2800000002wKAAQ’ and (id in(select BMCServiceDesk__FKChange__c from BMCServiceDesk__Incident_Change_Link__c where BMCServiceDesk__FKIncident__c=:id1.id))];
List change1 = new List();

for(BMCServiceDesk__Change_Request__c changeupdate1 : [Select id,BMCServiceDesk__FKStatus__c from BMCServiceDesk__Change_Request__c where BMCServiceDesk__FKStatus__c!=:’a3w2800000002wKAAQ’ and (id in(select BMCServiceDesk__FKChange__c from BMCServiceDesk__Incident_Change_Link__c where BMCServiceDesk__FKIncident__c=:id1.id))])
{
changeupdate1.BMCServiceDesk__FKStatus__c = ‘a3w2800000002wKAAQ’;
change1.add(changeupdate1);
//update changeupdate1;
}update change1;
}

}

}

Reply
Michelle Chaplin (@MichelleCRuns)
December 19, 2014 @ 10:56 am

Does the DELETE DML statement move the record to the recycle bin or permanently delete the record?

Reply
    David Liu
    December 19, 2014 @ 8:04 pm

    Recycle bin =)
    http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex6_5.htm

    Reply
Thom
September 28, 2014 @ 10:04 am

DML Statement link is broke!

Reply
    David Liu
    September 29, 2014 @ 9:59 pm

    Updated, thanks for the heads up!

    Reply
Matt Santy
October 15, 2013 @ 7:29 am

Under the insert DML, you say that you’re using “new Contact()” syntax. But in the example you’re creating a new Lead(). Just a heads up.

Reply
    David Liu
    October 15, 2013 @ 12:19 pm

    Thank you Matt – updated!

    David

    Reply
Chris
October 13, 2013 @ 7:16 pm

Thank you!!

Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply