Updating Records¶
Learn how to update existing Salesforce records.
Using update()¶
Update an existing record with new data:
const account = await Account.find('001xx000003DGbQAAW');
if (account) {
await account.update({
Industry: 'Finance',
AnnualRevenue: 7500000
});
console.log('Account updated');
}
Using save()¶
Modify properties and save:
const account = await Account.find('001xx000003DGbQAAW');
if (account) {
account.Industry = 'Healthcare';
account.AnnualRevenue = 8000000;
await account.save();
}
Partial Updates¶
Only update specific fields:
const account = await Account.find('001xxx');
if (account) {
// Only update Industry, leave other fields unchanged
await account.update({
Industry: 'Technology'
});
}
Bulk Update¶
Update multiple records:
const accounts = await Account
.select(x => ({ Id: x.Id, Name: x.Name }))
.where(x => x.Industry === 'Technology')
.get();
await Promise.all(
accounts.map(account => {
account.Industry = 'Tech';
return account.save();
})
);
console.log(`Updated ${accounts.length} accounts`);
update() vs save()¶
Both methods update records, but with different usage patterns:
update()¶
Accepts an object of changes:
save()¶
Saves current state:
Upsert Behavior¶
save() performs an upsert:
- If the instance has no Id, it creates a new record
- If the instance has an Id, it updates the existing record
// Create new record
const newAccount = new Account();
newAccount.Name = 'Brand New Account';
await newAccount.save(); // Creates
console.log(newAccount.Id); // Now has an ID
// Update existing record
newAccount.Industry = 'Finance';
await newAccount.save(); // Updates
With Observers¶
Observers can intercept update operations:
class AccountObserver implements Observer<Account> {
async beforeUpdate(instance: Account, changes: any): Promise<void> {
// Validate changes
console.log('Updating fields:', Object.keys(changes));
if (changes.AnnualRevenue && changes.AnnualRevenue < 0) {
throw new Error('Revenue cannot be negative');
}
}
async afterUpdate(instance: Account, changes: any): Promise<void> {
// React to successful update
console.log(`Account ${instance.Id} updated`);
}
}
Account.observe(new AccountObserver());
// Triggers hooks
await account.update({ Industry: 'Tech' });
Handling Errors¶
try {
await account.update({
Name: '', // Invalid: empty name
Industry: 'Technology'
});
} catch (error) {
console.error('Update failed:', error.message);
// Handle Salesforce validation errors
}
Read-Only Fields¶
System fields cannot be updated:
const account = await Account.find('001xxx');
if (account) {
// ❌ These will be ignored or cause errors
await account.update({
Id: 'new-id', // System field
CreatedDate: new Date(), // Read-only
LastModifiedDate: new Date() // Read-only
});
// ✅ Only update user-editable fields
await account.update({
Name: 'Updated Name',
Industry: 'Technology'
});
}
Conditional Updates¶
Update based on current state:
const account = await Account.find('001xxx');
if (account) {
// Only update if conditions are met
if (account.AnnualRevenue && account.AnnualRevenue > 1000000) {
account.Industry = 'Enterprise';
await account.save();
}
}
Best Practices¶
- Fetch before update - Always retrieve the record first
- Update only changed fields - Don't send unnecessary data
- Validate before update - Use observers for validation
- Handle errors - Wrap in try/catch for error handling
- Use transactions when possible - For related record updates
Next Steps¶
- Creating Records - Insert new records
- Deleting Records - Remove records
- Observers - Lifecycle hooks