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
Eager-2-LearnEager-2-Learn 

Old data vs Validation Rules vs Apex Code

I wanted get some feedback NORMS from the user community on this:

 

1. If you have code that was written last year lets say (old code). 

2. Then today you write a validation rule (VR) that will pop up if a user attempts to save a record that has historical data that break that VR.

 

Now when a process is executed to work on that same record via the apex code mention in 1 above the update action in the apex code does not work because of the VR rule.  Now this code doesn't seem to display an error on the screen either.

 

VR's seem to be causing all kinds of issues in relation with Apex code.

 

 

I have an ideas but not sure if there is a better way:

 

1. Update the historical data to adhire to the new VR (Not always easy)

 


How are you dealing with this type of issue?

 

 

 



Starz26Starz26

This is always an issues....

 

If you are ok with the old records breaking validation, add a filter on CreatedDate in the validation to ignore old records.

gotherthanthougotherthanthou
It's not displaying an error on the screen, but it's almost certainly throwing an exception. Try enclosing your update statement in a try catch block. If your code is in a page controller you can manually add an error message to the page. If it's executing in some other class you can take some other appropriate action.
Eager-2-LearnEager-2-Learn

This is great feedback and could be used as options but still seems awkward to do.

 

Meaning, would you agree that SFDC needs an apex command that peforms some kind of not commuiting save or something that validates all VR's on the object and reports any errors back in a List object.  Then if anything is found in the list you could message that to the users before a process even is allowed to start.

 

This what I see as an issue.  Suppose you have this process that when a user changes the opportunity owner, the account owner is updated to reflect the opp owner.  Your code updates the opp owner ok but fails on the account owner update because of a validation rule.

 

With the suggestion I have I know that I am going to update an account and opportunity record; therefore, I issue this wishful apex command that validates opps and if no errors are found then I validate the account and if no errors are found, start the process that actually updates the opportunity owner and the account owner.  I just think there is a major disconnect between VRs and apex code.  But then again I am still kind of new to this stuff!

 

Am I the only one who is having these types of issues?

 

 

Starz26Starz26

I would agree that there is a disconnect between apex and VR but I also see them as two seperate and distinct items.

 

Also, using the eample you posted, I would either use the apex code to check the account prior to actually updating the Opp owner and provide a page error or something if criteria are not met.

 

I currently have an object 4 relationships away from the account that has a validation rule that checks the account address to make sure it is not NULL. Since you cannot make the address on the account mandatory (ugh) and we had a lot of initial records where it was NULL it became a problem. The custom object required the address so I put the validation there to throw a nasty error that the account address needed entered before they could save the record.

 

The side effect, they had to abandon all changes (or open in a new tab if they knew how to) and update the account record. No pretty, but it worked. I added the same validatin on the account so new records would not cause the issue.

 

Validation rule IMHO are there for quick, easy to maintain checks that do not required codeing skills. It allows SFDC admins with no apex experience to manage the application to a point. APEX allows more complex business case solutions and sometime the two are at odds.

 

I may be way off base, but that is how I currently see it....