8

Update | APEX DML Standalone Statements

Apex Update DML Statement 

To update an existing record, you need to use the update statement.

Example 1:

public static void main() {
    Account a = [SELECT Name from Account WHERE Name like‘ Sh % ’LIMIT 1]
    a.name = ‘Cyntexa’;
    update a;
}

Example 2:

 List < Contact > conList = [SELECT firstname, lastname FROM Contact WHERE
    createdDate = Today
];
Integer i = 1;
for (Contact cc: conList) {
    cc.firstname = cc.firstname + i);
i++;
}
update conList;

Note:

We cannot modify a list or collection in a for-each loop where its even elements are iterated. (infinite loop)

salesforce-developer
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

ServiceNow Stripe