+ Start a Discussion
SFDCExplorerSFDCExplorer 

How to compare old values with new values in apex code?

I have Picklist called Fuel_Type__c with five values. Once the record saves with value 'Petrol' , This value should never get changed in further updates of the record(Remaining fields can be updated).I tried with validation rule and it is fine.But Could anybody let me know how to do this in Controller? 
Best Answer chosen by SFDCExplorer
Richard Jimenez 9Richard Jimenez 9
Hi,

When you referred to your 'controller' can you provide some more details on what you mean by this? Do you have a custom VisualForce page & controller, or are you using standard page layouts for this object?

If you are using standard page layouts, to disable the field on the page for different values you'll need to use different page layouts with record types. At this point you will need to consider if this is massively over-complicating things.

If you using a VisualForce page to display the record - consider this suggestion only if you are already have a VisualForce page.
 
<apex:outputField value="{!obj.Fuel_Type__c}" rendered="{!!CanEditFuelType}" />
<apex:inputField value="{!obj.Fuel_Type__c}" rendered="{!CanEditFuelType}" />

In your controller, create a property called CanEditFuelType which returns boolean of false if the fuel type is Petrol.

If you are unfamiliar with VF and Apex, check out the salesforce trailheads first.
 
Thanks,
Richard. 

All Answers

Richard Jimenez 9Richard Jimenez 9
Hi,

A validation rule is the best way to handle this because it will block all changes via UI or the API (dataloader etc).

If you have a custom VF page and want to stop changes you can render the field as an outputfield if petrol (or set as disabled) so that it cannot be changed on the vf page.

Thanks,
Richard.
SFDCExplorerSFDCExplorer
Hi Richard,
Thank you so much for your promt response. I am new to vf coding. Hence It would be great if you let me know how to write that outputfield tag to disbale for particular value
.
Richard Jimenez 9Richard Jimenez 9
Hi,

When you referred to your 'controller' can you provide some more details on what you mean by this? Do you have a custom VisualForce page & controller, or are you using standard page layouts for this object?

If you are using standard page layouts, to disable the field on the page for different values you'll need to use different page layouts with record types. At this point you will need to consider if this is massively over-complicating things.

If you using a VisualForce page to display the record - consider this suggestion only if you are already have a VisualForce page.
 
<apex:outputField value="{!obj.Fuel_Type__c}" rendered="{!!CanEditFuelType}" />
<apex:inputField value="{!obj.Fuel_Type__c}" rendered="{!CanEditFuelType}" />

In your controller, create a property called CanEditFuelType which returns boolean of false if the fuel type is Petrol.

If you are unfamiliar with VF and Apex, check out the salesforce trailheads first.
 
Thanks,
Richard. 
This was selected as the best answer