User Story
As a Salesforce administrator, I need to update the titles of contacts in the Finance department to "Financial Analyst" for better reporting and organization. To achieve this, I first created a list of contacts using the Contact object and added them to a list. I then used a single DML call to insert all the contacts into the database. Next, I iterated through the list of contacts and checked if their department was Finance. If it was, I updated their title to "Financial Analyst" and added the updated contact to a list to be bulk updated with a single DML call. Finally, I performed the bulk update only if there were contacts to update. With this process, I was able to efficiently update the titles for all contacts in the Finance department in Salesforce.
Code
// Create a list of contacts
List<Contact> conList = new List<Contact> {
new Contact(FirstName='Joe',LastName='Smith',Department='Finance'),
new Contact(FirstName='Kathy',LastName='Smith',Department='Technology'),
new Contact(FirstName='Caroline',LastName='Roth',Department='Finance'),
new Contact(FirstName='Kim',LastName='Shain',Department='Education')};
// Bulk insert all contacts with one DML call
insert conList;
// List to hold the new contacts to update
List<Contact> listToUpdate = new List<Contact>();
// Iterate through the list and add a title only
// if the department is Finance
for(Contact con : conList) {
if (con.Department == 'Finance') {
con.Title = 'Financial analyst';
// Add updated contact sObject to the list.
listToUpdate.add(con);
}
}
// Bulk update all contacts with one DML call, but only if there are contacts to update
if (!listToUpdate.isEmpty()) {
update listToUpdate;
}
Video
Video does not exists.