function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RichardR1RichardR1 

How to avoid saving other records in a custom VF page

Hello everyone, I created a custom list of Event records using a custom controller and Visualforce page. In the list the user can edit an Event but after saving the change, it also changes the last modified date of the other records on the list, even though there was no change to the data. What can I do so that Salesforce will only update the record that was changed?
Below is my current custom controller:
public class EventsMDHomePage{

    public List<Event> eventsToday {get; set;}

    public EventsMDHomePage() {
        eventsToday = [
            SELECT FIELDS(STANDARD)
            FROM Event
            WHERE OwnerId =: UserInfo.getUserId() AND ActivityDate =: date.today()
            ORDER BY StartDateTime ASC
            LIMIT 1000];
    }
    
    public PageReference saveEvents() {
        try{
            update eventsToday;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
        }
        return null;
    }      
}
VinayVinay (Salesforce Developers) 
Hi Richard,

Hope you query is answered below.

https://salesforce.stackexchange.com/questions/382812/how-to-avoid-saving-other-records-in-a-custom-vf-page

Thanks,
RichardR1RichardR1
Hi Vinay, thanks for notifying me. I think the solution is correct but do you mind explaining what this part means please:
for(Integer i = 0, s = eventsToday.size(); i < s; i++) {
            if(originalEvents[i] != eventsToday[i]) {
                recordsToSave.add(eventsToday[i])
            }
        }