+ Start a Discussion
BrianWKBrianWK 

Dynamic Apex -- Comparison Values within Conditional statement

I'm going to break down what i'm doing into a very simple example. Basically, I'm getting a value and testing to see if the value equivalent to another value.

 

Account MySobject = [Select a.id, a.AM_Last_Connect_Amplifi__c, a.name from Account a where a.id = '001T000000ELK88']; Sobject s= MySobject; Sobject delegate = s; Schema.SobjectField f; Schema.DescribeSObjectResult objDescribe = delegate.getSobjectType().getDescribe(); f = objDescribe.fields.getmap().get(MyDate__c); system.debug('Current Value: ' + delegate.get(f)); Object Test = delegate.get(f); system.debug('Value in Test: ' + Test); date dTest = date.valueof(Test); system.debug('Value of dTest: ' + dTest); system.debug('Conditional: ' + dtest == dtest);

 

 

So basically here's what I want to do. Eventually I want to compare the value set in f to a different value. I attempted a completely different date field -- but that didn't work. I figured the simpliest conditional would be to see if dtest is the equivalent to itself (I would think it would be!).

 

What happens: commenting out the conditional debug I see:

 

Current value = 2009-12-07 00:00:00

Value in Test = 2009-12-07 00:00:00

Value of dtest = 2009-12-07 00:00:00

 

so here I've confirmed that value is the same in all 3 items -- including the item where I convert the Object value into a date (dtest).

 

Adding the conditional back I get the following error:

"Comparison arguments must be compatible types: String, Date"

 

Huh? My arguments are the same item! I would expect to get true. How is dTest being treated as a string and as a date?

 

I've also created a different variablel: "date vTest = date.today();" and attempted to compare dTest with vTest. both with are date variables -- and I'll get the same error.

 

Thoughts? What am I doing wrong?

 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

So this one is a mind bender.

I am not sure if it's a bug or expected functionality but the issue is with the debug method and not the comparison.

The debug method is trying to 'toString' everything in the statement, but it can't 'toString' the second variable because it's after a comparison operator. But it gets confused and is trying to compare the local variable to the instance variable, or something like that.

 

This works if you want to debug your code:

 

System.debug('**** test ' + String.valueOf(dTest).equals(String.valueOf(dTest)));

 

In your code you can use a System.assertEquals() to check the comparison if you want and that will work for you.

Hope this helps.

 

 

All Answers

mikefmikef

So this one is a mind bender.

I am not sure if it's a bug or expected functionality but the issue is with the debug method and not the comparison.

The debug method is trying to 'toString' everything in the statement, but it can't 'toString' the second variable because it's after a comparison operator. But it gets confused and is trying to compare the local variable to the instance variable, or something like that.

 

This works if you want to debug your code:

 

System.debug('**** test ' + String.valueOf(dTest).equals(String.valueOf(dTest)));

 

In your code you can use a System.assertEquals() to check the comparison if you want and that will work for you.

Hope this helps.

 

 

This was selected as the best answer
BrianWKBrianWK

Mike,

 

Fabulous! You have no idea how excited I am this works! I've been struggling to write this code where I have to retrieve and update multiple fields on the same object where the fields have a similiar naming path. This will allow me to put the difference of the field names in a string then loop through a list to update based on a conditional return.

 

Simply fabulous!