About this question
I am a Salesforce developer and I am currently tasked with updating records based on specific criteria by using the SOQL. How can I construct a SOQL query to update multiple records efficiently?Â
I am a Salesforce developer and I am currently tasked with updating records based on specific criteria by using the SOQL. How can I construct a SOQL query to update multiple records efficiently?Â
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on Apr 10, 2024
In the context of Salesforce, here is the approach given of how you can construct a SOQL query by using the âUPDATEâ keyword for updating multiple records efficiently:-
Suppose you need to update the âstatusâ field of all accounts owned by a particular sales rep (OwnerID = 005XXXXXXXXXXXX) for âactiveâ by using the SOQL query for performing this update efficiently and accurately:-
Then here is the solution given below:-
UPDATE Account
SET Status__c = âActiveâ
WHERE OwnerId = â005XXXXXXXXXXXXâ
Here is another example given:-
// Define a class to handle the update operation
Public class AccountUpdater {
  // Method to update Account records owned by a specific Sales Rep
  Public static void updateAccountsForSalesRep(String salesRepId) {
    // Query for Accounts owned by the specified Sales Rep
    List accountsToUpdate = [SELECT Id, Status__c FROM Account WHERE OwnerId = alesRepId];
    // Check if there are any records to update
    If (!accountsToUpdate.isEmpty()) {
      // Iterate through each Account record and set the Status__c field to âActiveâ
      For (Account acc : accountsToUpdate) {
        Acc.Status__c = âActiveâ;
      }
      // Perform the update using DML (Data Manipulation Language)
      Try {
        Update accountsToUpdate;
        System.debug(âAccounts updated successfullyâ);
      } catch (Exception e) {
        System.debug(âError updating accounts: â + e.getMessage());
      }
    } else {
      System.debug(âNo accounts found for the specified Sales Repâ);
    }
  }
  // Example usage in a test method or elsewhere
  Public static void main(String[] args) {
    // Provide the Sales Rep Id for whom you want to update accounts
    String salesRepId = â005XXXXXXXXXXXXâ;
    // Call the update method
    updateAccountsForSalesRep(salesRepId);
  }
}Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Prepare for Salesforce Developer Certification: A Practical Guide
A step-by-step guide to Salesforce Developer certification - what to study for PD1, how PD2 fits in, and how to know you're readyâŚ
Salesforce Salesforce Developer Certification Guide: Everything You Need to Know in 2026
Learn about Salesforce Developer Certification, including exam requirements, cost, topics, preparation time, retakes, and careerâŚ
Salesforce How to Prepare for Salesforce Admin Certification: A Practical Study Guide
Get a practical study plan for the Salesforce Admin exam - weighting by section, how to use Trailhead effectively, and how toâŚ
Salesforce Salesforce Developer Salary Guide: How much you can earn
Discover the real Salesforce developer salary in 2026 by experience, location, skills & certifications. See what actually boostsâŚ