You need to sign in to do that
Don't have an account?
Bug with String.join()
Summary
A class implementing an override for public String toString() will not have this function honored by String.join(). Instead, it returns the name of the class. Using the normal concatenation operator on the list yields output that is expected.
Repro
<apex:page controller="repro"> <table> <tbody> <tr> <td>new sub1().tostring()</td> <td>{!Sub1ToString}</td> </tr> <tr> <td>''+(new sub1[] { new sub1() })</td> <td>{!Sub1AsList}</td> </tr> <tr> <td>string.join(new sub1[] { new sub1() }, '')</td> <td>{!Sub1AsJoin}</td> </tr> <tr> <td>new sub2().tostring()</td> <td>{!Sub2ToString}</td> </tr> <tr> <td>''+(new sub2[] { new sub2() })</td> <td>{!Sub2AsList}</td> </tr> <tr> <td>string.join(new sub2[] { new sub2() }, '')</td> <td>{!Sub2AsJoin}</td> </tr> </tbody> </table> </apex:page>
public with sharing class repro { class sub1 { } class sub2 { public override string tostring() { return 'Hello. I am sub2.'; } } public string getSub1ToString() { return new sub1().tostring(); } public string getSub1AsJoin() { return string.join(new sub1[] { new sub1() }, ''); } public string getSub1AsList() { return ''+(new sub1[] { new sub1() }); } public string getSub2ToString() { return new sub2().tostring(); } public string getSub2AsJoin() { return string.join(new sub2[] { new sub2() }, ''); } public string getSub2AsList() { return ''+(new sub2[] { new sub2() }); } }
Output from above:
new sub1().tostring() | sub1:[] |
''+(new sub1[] { new sub1() }) | (sub1:[]) |
string.join(new sub1[] { new sub1() }, '') | repro$sub1 |
new sub2().tostring() | Hello. I am sub2. |
''+(new sub2[] { new sub2() }) | (Hello. I am sub2.) |
string.join(new sub2[] { new sub2() }, '') |
repro$sub2 |
As one can see, It's not even trying to join them together, it's just calling some sort of odd "List<Object>.getType()" function that we can't even access.
I've logged a case with support, but since I'm half-expecting them to just close it without even trying to log a bug, I'm also posting this here hoping that some PM or internal salesforce.com developer will see this and have it fixed at some point.
Hi,
Thank you for bringing this to our notice. From the summary i understand that the toString() override is not working as expected. I will try to reproduce this issue on my end and if required will forward it to the right team within Salesforce.
Regards,
Satish Kumar
Salesforce.com