DEV Community

Cover image for Data Dance Party in Salesforce: Mastering DML & SOQL with Code
Priyank Sevak
Priyank Sevak

Posted on

Data Dance Party in Salesforce: Mastering DML & SOQL with Code

Ever felt like a superhero, manipulating data with the flick of a wrist? Buckle up, Salesforce devs, because this post is your guide to mastering DML (Data Manipulation Language) and SOQL (Salesforce Object Query Language) – the dynamic duo of data manipulation!

DML: The Data Creation Crew

Imagine you're at a bustling party. New faces arrive – that's DML's INSERT operation, creating fresh records like VIP entries. Need to update someone's outfit (industry change)? UPDATE has your back, ensuring your data stays on-trend. Did someone crash the party (unwanted record)? DELETE sends them packing.

But wait, there's more! DML offers bulk operations: Bulk DML operations allow you to insert, update, or delete multiple records efficiently in a single operation. new records entering the party with insert accounts; or a synchronized update for the entire guest list!

Basic DML Operations:

Account newAccount = new Account(Name='Acme Corp');
insert newAccount;

Account accountToUpdate = [SELECT Id, Name FROM Account WHERE Name = 'Acme Corp'];
accountToUpdate.Industry = 'Technology';
update accountToUpdate;

Account accountToDelete = [SELECT Id FROM Account WHERE Name = 'Acme Corp'];
delete accountToDelete;
Enter fullscreen mode Exit fullscreen mode

Upsert: The Social Butterfly

Don't know if your guest is new or a returning party animal? Upsert is your social butterfly, creating a record if it doesn't exist or gracefully updating it if it does. Talk about efficiency!

Account account = new Account(ExternalId__c = 'XYZ123', Name = 'New Account');
upsert account;
Enter fullscreen mode Exit fullscreen mode

Catching the DML Glitch (Exception Handling)

Of course, parties can get wild. DML exceptions are like unexpected glitches – power outages, costume malfunctions! Learn to catch these glitches with try...catch blocks, ensuring your data party stays smooth.

try {
  insert newAccount;
} catch (DmlException e) {
  // Handle exception
  System.debug('A DML exception has occurred: ' +
                e.getMessage());
}
Enter fullscreen mode Exit fullscreen mode

Database Methods: The Power Players

For more control and flexibility, Database methods are the party planners. They offer fine-tuned options like Database.insert(accounts, false);, letting you handle partial success scenarios. This means you can gracefully handle situations where some records are created successfully, while others encounter issues.

List<Account> accounts = new List<Account>();
// Populate accounts list
Database.insert(accounts, false); // Partial success option
Enter fullscreen mode Exit fullscreen mode

DML vs. Database Methods: When to Choose

Think of DML as the DJ, keeping the party flowing. It's perfect for simple, transactional operations. Database methods are the venue managers, ideal for bulk operations and intricate coordination.

DML & Related Records: The Party Posse

Building a guest list with contacts and accounts? DML lets you create child records on the fly, like adding your best friend to your party group!

Account account = new Account(Name='Acme Corp');
Contact contact = new Contact(LastName='Doe', AccountId = account.Id);
account.Contacts.add(contact);
insert account;
Enter fullscreen mode Exit fullscreen mode

SOQL: The Data Search Squad

Now, let's talk about finding the perfect party favor (data)! SOQL is your search squad, crafting queries to retrieve specific records. Need a list of all the "Acme Corp" guests? SOQL has your back!

Beyond the Basics: Querying the Party Scene

SOQL isn't just about guest lists. You can query related records, like finding out which contacts belong to a specific account – the ultimate party posse identification!

Basic SOQL Queries:

SELECT Id, Name FROM Account
Enter fullscreen mode Exit fullscreen mode

Filtering the Party Guest List: SOQL WHERE Clause
Want to find only the VIPs (accounts with annual revenue over a million)? Use the WHERE clause to filter your party guests.

SELECT Id, Name FROM Account WHERE AnnualRevenue > 1000000
Enter fullscreen mode Exit fullscreen mode

Ordering the Guest List: SOQL ORDER BY Clause
Need to create a guest list sorted by last name? The ORDER BY clause is your party planner.

SELECT Id, Name FROM Contact ORDER BY LastName DESC
Enter fullscreen mode Exit fullscreen mode

Limiting the Guest List: SOQL LIMIT Clause
Party venue too small? Use LIMIT to control the guest list size.

SELECT Id, Name FROM Account LIMIT 10
Enter fullscreen mode Exit fullscreen mode

Accessing Variables in the Party: SOQL and Apex
Need a dynamic guest list based on a specific industry? While you can't directly pass variables to SOQL, Apex comes to the rescue with bind variables.

String industry = 'Technology';
List<Account> techAccounts = [SELECT Id, Name FROM Account WHERE Industry = :industry]
Enter fullscreen mode Exit fullscreen mode

Querying Related Records:

SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account
Enter fullscreen mode Exit fullscreen mode

Remember: Optimize your SOQL queries – they're like the party music! Keep it clean and efficient for a smooth data flow.

So, there you have it! By mastering DML and SOQL, you'll be the ultimate Salesforce data DJ, throwing epic data manipulation parties that are both fun and functional. Now get out there and code like nobody's watching (but they probably are, because your code will be awesome)!

Top comments (0)