Integers and Commas
I came back from holiday today to discover a bug in my inbox from my co-worker, he was unable to generate a license because the server was coming back saying the quanity was an invalid number. The number that it thought was invalid was "1,500" which is when I realised that something was up. It seems Salesforce was adding commas to the value to seperate the thousands.
The license is generated from a "On Click" Javascript button which takes the Salesforce fields, puts them in Javascript vars and then calls an APEX function. Quantiy had quotes around it and removing them caused an error, I insepected the source code and discovered the same value "1,500" which of course now it was unquoted was causing the syntax error due to the eroneous comma.
Some quick googling found that Salesforce always presents Integer fields like this in Visualforce pages and in this particular Javascript usage, I have never seen it treat integers like this returned from the SOAP API or the Data Loader (I know the Data Loader uses the SOAP API but some people reading this may not know that).
According to some responses on success.salesforce.com there is no way to remove the comma formating and the best resolution is to create a text formula field which copies in the Integer value, if you ask me this sounds like a massive hack.
In the end my solution was pretty brute force, in the Javascript after getting the Quantity value back as a string, I then strip the commas and use the parseInt() function.
quantity = parseInt(quantity.replace(/,/g, ''), 10);
After adding this code and perfoming some tests I discovered that annoyingly if the Integer is less than a thousand, and therefor doesn't have any commas, it comes into Javascript treated as an Integer and not a String. This meant that my ".replace" function failed since it wasn't operting on a String. To solve this issue I cast the Quantiy to a string before as a precaution.
quantity = quantity + '';
quantity = parseInt(quantity.replace(/,/g, ''), 10);
Although I have fixed this issue, it has made me concerned about other Integer assignments I have done for other projects and will be promptly checking all of my Salesforce Javascript.
I have been working on a new website administration framework which should mean making custom website content editable by users much simpler.
I recently discovered a bug in my Salesforce "On Click" Javascript where Integer values were coming back with commas to seperate the thousands.
In this entry I discuss the problems when working with Salesforce APEX triggers with complex SOQL. I then present a possible solution to this by building efficient SOQL queries which work on batch triggers.