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
JoyDJoyD 

Way to tell if value is changed in custom controller for VF page?

Hi all - just got back from the DEV 501 course and wrote my first VF page and custom controller from scratch!  Now looking to tweak certain things.

 

Is there a function to tell if the user has made a change to a value on a VF page?  In the UI, we have the function isChanged, and in Apex, we have Trigger.old and Trigger. new - but what about in a VF page?

 

If the context matters, here's the project:

Page containing a form that lists multiple sObjects by rows.  Two of the columns contain inputFields so the user can change multiple values all from one page, then hit Submit.  I use a SOQL query to put the sObjects into a List in the controller - then when the user hits Submit, I call update on that List.  Perhaps there's a better way to architect this?

 

Anyway - my concern is that all the sObjects are updated, whether or not they were changed (therefore the Last Modified Date and By are updated).  Thought I could build a 'changed' List out of ONLY those that were changed and just update that, but can't figure out how to tell what has changed.

 

Thanks for any help you can give...

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

 

List<SObject> firstList = SOQL query;

List<SObject> listUsedToCheck = firstList.deepClone();

//Third list will be used for update operation.

List<SObject> toBeUpdatedList = new List<Sobject>();

 

Use firstList in Visualforce page

 

When user clicks on submit, do the following

for(SObject s1: firstList)

{

   for(SObject s2: listUsedToCheck)

   {

    //Check which field is used in UI for updation. As youmentioned that there are two fields that can be updated.

     if(s1.id == s2.id && (s1.<<updateField1>> != s2.<<updateField1>>  || s1.<<updateField2>> != s2.<<updateField2>>))

     {

          toBeUpdatedList.add(s1);

          break;

     }

   }

}

update toBeUpdatedList;

//Note these things, once update is done do the following in the submit action

toBeUpdatedList.clear();

listUsedToCheck = firstList.deepClone();

 

 

Let me know if you have any questions. 

 

The test methods will also be simple.

Try that if you dont get it let me know.

All Answers

Imran MohammedImran Mohammed

why not have a copy of the list and check at the time submitted.

Have two lists, one will be used in Visualforce page and other will be used for check.

 

When the USer submits the records, check list records used in the page with the temporary list.

If the same value exists for a record in the temp list, dont update it.

 

In this way you can do that.

 

Later update the temp list with the List used in page.

JoyDJoyD

thanks for the suggestion.  perhaps you can help with how to loop through that?

 

i have created 3 Lists now - original, list that i'm working with in the page, and then one to do the update with.

 

the original is set to the SOQL query.  then i just make the working list equal to that (or should i do .copy or .deepcopy?).

 

in my updating function, i want to loop through the working list, checking against the original (how do i do that?) - and if it's different, add the item to the update list.

 

in the end i'll call update on the update list.

 

and of course now i'm confused - how do i write a test class that 'passes' updated values to the controller as if it were the page?

 

sorry - lots of questions - may be too complex :-(

Imran MohammedImran Mohammed

 

List<SObject> firstList = SOQL query;

List<SObject> listUsedToCheck = firstList.deepClone();

//Third list will be used for update operation.

List<SObject> toBeUpdatedList = new List<Sobject>();

 

Use firstList in Visualforce page

 

When user clicks on submit, do the following

for(SObject s1: firstList)

{

   for(SObject s2: listUsedToCheck)

   {

    //Check which field is used in UI for updation. As youmentioned that there are two fields that can be updated.

     if(s1.id == s2.id && (s1.<<updateField1>> != s2.<<updateField1>>  || s1.<<updateField2>> != s2.<<updateField2>>))

     {

          toBeUpdatedList.add(s1);

          break;

     }

   }

}

update toBeUpdatedList;

//Note these things, once update is done do the following in the submit action

toBeUpdatedList.clear();

listUsedToCheck = firstList.deepClone();

 

 

Let me know if you have any questions. 

 

The test methods will also be simple.

Try that if you dont get it let me know.

This was selected as the best answer
JoyDJoyD

great thanks!  i didn't think of the second for loop - i was trying to get the id from the first for loop and find it in the original list which was causing me heartburn.  got this working and seems like all is well!

2323

hey thanks JoyD..

I am also had this doubt..

but now am getting cleared and now it works..

thanks a lot for ur question and for ur friend who gives the answer for this question