I am displaying many (up to hundred) records on a single page and allow users to modify fields of those records using .
If the user clicks the "Save" command button I only want to update those record that really have changed and not all. Why: I don't want the LastModified Date to become meaningless. If I would change it even if nothing changed...
Can this be done?
Attribution to: Robert Sösemann
Possible Suggestion/Solution #1
To expand on option 2 I found it was easiest to use set functionality because my sObject comparisons were not working properly. I used the following and it worked perfectly:
public PageReference saveValues(){
Set<Order_Item__c> Item1 = new Set<Order_Item__c>(ItemList);
Set<Order_Item__c> Item2 = new Set<Order_Item__c>(StaticItemList);
Item1.removeAll(Item2);
UpdateItemList = new List<Order_Item__c>(Item1);
try{
update UpdateItemList;
Attribution to: John Crusan
Possible Suggestion/Solution #2
Yes that is possible. I am not going to write the code here but I can give you an idea or approach.
Option 1:
What I suggest first is to create an inner class that will have 3 variables:
1) The sObject record
2) Integer to keep track of the index in the table
3) Boolean to keep track whether there were changes made to that row
Once you create objects of the class above for all your records, add them to a map or a list which you will later use to display the records on the page.
On each of your input fields, add actionSupport
or javascript onchange
event together with an actionFunction
which will call a method in your class that will iterate through the map or list of your inner class objects and once it finds the changed index, it will mark that object with "changes have been made".
Lastly when the user clicks save, you loop through your map or list of inner class objects, you find the ones with the boolean variable indicating that changes have been made set to true and you add only them in a separate list which you will update later.
Option 2:
Alternatively, put the records in a map and keep a clone of that map. One of the maps would be used to display the records on the page (and will have the changed sObjects) and the other map will keep the original records once loaded from the database. On save, go through the changed map and compare the cloned record's fields in the original map. If one of the fields don't match, you add that record in a separate list used to update at the end.
Notes:
1) Create a clone of the map, do not use only a reference because that will change the record in both maps.
2) You can use the schema.describe methods to compare the fields dynamically.
3) Indexes in the maps must be the same so that you can get the correct corresponding sObject
I believe there are other ways of doing it, but these are the 2 that I could think of straight away and I'd use one of these depending on the complexity of your page and number of fields you're working with.
Attribution to: Boris Bachovski
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31436