You need to sign in to do that
Don't have an account?
d3developer
String assignment to null when using single quotes
Used to have some simple code that went like this:
g.showRule.Value_To_Show__c = ''; for(String s : g.showVals) g.showRule.Value_To_Show__c += s + ',';
Crazy thing was, the value to show would save like this:
nullvalue1,value2,value3
So for some reason a double single quote was being converted into a four character string 'null.' Oddly, this has not happened in other instances where I successfully concatenate strings.
My workaround:
if(g.showVals.size() > 0) { g.showRule.Value_To_Show__c = g.showVals.get(0) + ','; for(Integer i = 1; i < g.showVals.size(); i++) g.showRule.Value_To_Show__c += g.showVals.get(i) + ','; }
I have run into this too. In my experience, it happens more with Multi-select picklists. the work around I have used with success, is simply to initialize a temporary string value, append onto that, and then assign the result string to your original field. Having trouble with text and non-text versions of 'null,' I just do this as a general practice.
It's hacky but it works. Would love to find out the 'clean' way to avoid that null.
I think the 'clean' way is for salesforce to fix what is obviously a bug...
Can you provide an isolated piece of code that reproduces this problem? When I try:
list<string> mylist = new string[] {'one', 'two', 'three'};
string foo = '';
for(String s : mylist)
foo += s + ',';
system.debug(foo);
The result is
one,two,three,
not
nullone,two,three,
I've never had it happen except this one time. Perhaps it has sometime to do with the assignment to an SObject field of type String?
Good guess. This reproduces the problem:
account a = new account();
list<string> mylist = new string[] {'one', 'two', 'three'};
for(String s : mylist)
a.name += s + ',';
system.debug(a.name);
I'll look into it.
Or, rather, this also produces the problem, and actually initializes the name to the empty string:
account a = new account(name='');
list<string> mylist = new string[] {'one', 'two', 'three'};
for(String s : mylist)
a.name += s + ',';
system.debug(a.name);
By the way, this suggests a less hackish workaround. Just use a string variable, and assign it to the sobject field after the for loop.
I agree. I think I will change my code to do that.
Okay, I found out why this is happening. Evidently, it's intended behavior that setting an sobject field value to the empty string is interpreted as setting it to null.
Just one of those unfortunate historical facts. It's always been that way, and changing it now would just create more chaos.
Rich
Okay - It's one thing for an empty string to resolve to a null value when committed to the database the a dml operation, but to conflate an empty string with a null value within a programming language is just daft.
Are you sure that's really the intended behavior within apex? If it is, yes, it should be fixed. or the append logic += should be adjusted to correspond with this deviation from basic programming concepts.
Regardless of the logic of having a empty string equate to a null value, if you append to a null value the string should not become
'nullMyValue'
A string with four characters 'null' != null and if that is intended behavior it is not daft, it is just, well, wrong.
Excellent point. I'll plan this change for the next version. It will work like this:
So the weird nulling behavior (which must exist for backwards compatibility reasons) will be enforced by the DML instead of within the language.
This will work for code version 20.0 (when it comes out).
Rich
Awesomeness!!!
Very cool. That makes my evening!