+ Start a Discussion
Anupama@28Anupama@28 

Apex Controller - Compare old and new values

Hi,

I  have a method in a controller, which takes to the edit page of a contact where user can edit the contact and save.
How do i compare the old and new values of that contact ?

apex Controller
==============

public pageReference gotoContactEditPage()
{
             PageReference conEditPage = new PageReference('/' + contactId+ '/e');         
             conEditPage.setRedirect(true);
            // Here i want to compare the old and new values of contact (before save and after save)
             return conEditPage
           


}

 
Shashikant SharmaShashikant Sharma
You could query the values from database and keep them in a separate contact instance and when you save you could compare the old and new values.
Ankit Maini.Ankit Maini.
Contact con  = [select Name from contact where id = :contactId];

Here you will get saved contact, Now compare with edited one.
Ankit Maini.Ankit Maini.

NOTE: Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Anupama@28Anupama@28
Hi I have already tried with the SOQL query but it won't work.
With SOQl query in the method i can get old values not new .

public pageReference gotoContactEditPage()
{
             PageReference conEditPage = new PageReference('/' + contactId+ '/e');         
             conEditPage.setRedirect(true);
            // As soon as the standard contact edit page opens, the below query fetches the old values not new ones
           Contact con = [Select name from contact where id=:contactId]
             return conEditPage;
           


}
 
Sean McMickleSean McMickle
You already have the new values in your object that you are getting from the page. Compare that against the query (old values).