You need to sign in to do that
Don't have an account?
Comparing sObjects in list
Hi,
In my apex script I make a copy of an original list with sObjects, to see at a later phase (eg when saving the objects) wether there has been a change of any of the fields by the user.
I use deepclone (clone(true,true)) to make the copy of the relevant objects which I add to the list. Immediately after the cloning, the objects are identical (assertequals of clone and original returns true), after showing them on a vf page with only outputfields they remain identical,
but after showing them in a vf page with inputfields they are seen different (assertequals of clone and original returns false), although all fields (including system fields like lastmodifieddate and id) are identical (assertequals of the individual fields returns true).
Anyone who recognizes this and could think of a way to handle this without having to compare each individual field to check for changes?
Have you tried using the == operator? E.g. assert(sobject1==sobject2), rather than the assertEquals?
According to the apex docs, == performs a deep check of equality for all fields. It may be that the assertEquals is using some other equality check, though I can't think what
Hi, indeed I tried a regular IF(xx == yy) and both assert with == as well as assertequals. Allways all individual fields are the same, but the sObject as a whole is not...
I guess the only thing that has happened is that everything has been converted to strings to display on the page and then converted back again when the form has been submitted. I can't think of anything specific, but I'm wondering about date/time fields and timezones.
Have you found any examples where any fields differ? I know its a pain, but its probably worth taking one object and comparing all of the fields to see where the difference actually occurs.
indeed, I have put both records in assert log and compared them in detail on the screen and in an editor, but no differences than the order of the fields listed. but that was also the case when I compared the two objects directly after the clone, when they were still reported as equal.
your idea of fields converted to strings sounds reasonable, but I would still expect that these specific fields would also report unequal in individual field comparisons.
I am thinking that there might be a (hidden) system field which is updated when an sObject is used in a vf screen in input mode. Something with locking or activity logging?
Sorry if this is stating the obvious, but have you checked the schema explorer to ensure you are checking all fields on the object. From your last post it sounds like you have, but I'm clutching at straws here.
Bummer. Shouldn't happen according to the docs - all fields have to be equal for records to be equal.
Sorry, can't think of anything else. All the other ideas I can come up with are based around fields changing, which isn't the case when you check them directly.
Hi, I'm experiencing a very similar issue. Did you ever figure this out?
No, I created a boolean method comparing the two objects field by field.
When you compare new sObjects original/after submit via JSON serialize, you'll notice that the sObject's attributes that do not exist before VF presentation do exist with a null value upon submit of the page.
The same issue does not apply for records that were returned by a SOQL query, since the attributes are present before as well as after the VF submission. Itterating over attributes seems to be the only solid approach.
I don't know of a better way to dynamically check an object. But here is what I did to meet the need for any sObject:
The compare method will return true if the objects are identical, and false otherwise. Since I am not aware of a way to determine which fields were included in a sObject, the JSON serialize and deserialize are a means to that end.
To clone the list of sobjects use
List.deepclone(true,true,true);
To compare those object I am using this method.
String.valueOf(object1).equals(String.valueOf(object1Clone))