Tuesday 23 June 2015

System.SelectOption to JSON Object

I had a particular need to examine the SelectOptions used on a page and I'm ever so much happier using the Developer Console so, after reading this answer on Stack Overflow to this question (How to parse String with Javascript looks like object) I thought I'd have a go myself. Given the following HTML:

<p><strong>Raw SF SelectOption data:</strong></p>
<div id="sfData"></div>
<p><strong>SF data converted to string with an array of objects using Regex:</strong></p>
<div id="sfDataToStr"></div>
<p><strong>SF data converted to proper array of Objects:</strong></p>
<div id="strToObj"></div>

We can use the following JavaScript (using jQuery just to make things easier):

var sfData = 
    '[System.SelectOption[value="-- None --", label="-- None --", disabled="false"],' + 
    ' System.SelectOption[value="Special 1", label="Special 1", disabled="false"],' + 
    ' System.SelectOption[value="Special 2", label="Special 2", disabled="false"],' + 
    ' System.SelectOption[value="Special 3", label="Special 3", disabled="false"]]';
$(function(){
    $("#sfData").
        append(sfData).
        prepend($("<p></p>", {
            "text": "TYPE: " + typeof sfData}));
    var sfDataToStr = sfData.
        replace(/System.SelectOption\[(\w+)=/g, "{\"$1\":").
        replace(/\],\s?/g, "},").
        replace(/\",\s?(\w+)=/g, "\",\"$1\":").
        replace(/\]\]/g, "}]");
    $("#sfDataToStr").
        append(sfDataToStr).
        prepend($("<p></p>", {
            "text": "TYPE: " + typeof sfDataToStr}));
    var strToObj = JSON.parse(sfDataToStr);
    $("#strToObj").
        append(JSON.stringify(strToObj)).
        prepend($("<p></p>", {
            "text": "TYPE: " + typeof strToObj}));
});

To produce this markup:

<p><strong>Raw SF SelectOption data:</strong></p>
<div id="sfData"><p>TYPE: string</p>[System.SelectOption[value="-- None --", label="-- None --", disabled="false"], System.SelectOption[value="Special 1", label="Special 1", disabled="false"], System.SelectOption[value="Special 2", label="Special 2", disabled="false"], System.SelectOption[value="Special 3", label="Special 3", disabled="false"]]</div>
<p><strong>SF data converted to string with an array of objects using Regex:</strong></p>
<div id="sfDataToStr"><p>TYPE: string</p>[{"value":"-- None --","label":"-- None --","disabled":"false"},{"value":"Special 1","label":"Special 1","disabled":"false"},{"value":"Special 2","label":"Special 2","disabled":"false"},{"value":"Special 3","label":"Special 3","disabled":"false"}]</div>
<p><strong>SF data converted to proper array of Objects:</strong></p>
<div id="strToObj"><p>TYPE: object</p>[{"value":"-- None --","label":"-- None --","disabled":"false"},{"value":"Special 1","label":"Special 1","disabled":"false"},{"value":"Special 2","label":"Special 2","disabled":"false"},{"value":"Special 3","label":"Special 3","disabled":"false"}]</div>

Or this rendered markup:

Raw SF SelectOption data:

TYPE: string

[System.SelectOption[value="-- None --", label="-- None --", disabled="false"], System.SelectOption[value="Special 1", label="Special 1", disabled="false"], System.SelectOption[value="Special 2", label="Special 2", disabled="false"], System.SelectOption[value="Special 3", label="Special 3", disabled="false"]]

SF data converted to string with an array of objects using Regex:

TYPE: string

[{"value":"-- None --","label":"-- None --","disabled":"false"},{"value":"Special 1","label":"Special 1","disabled":"false"},{"value":"Special 2","label":"Special 2","disabled":"false"},{"value":"Special 3","label":"Special 3","disabled":"false"}]

SF data converted to proper array of Objects:

TYPE: object

[{"value":"-- None --","label":"-- None --","disabled":"false"},{"value":"Special 1","label":"Special 1","disabled":"false"},{"value":"Special 2","label":"Special 2","disabled":"false"},{"value":"Special 3","label":"Special 3","disabled":"false"}]

This makes a nice array of objects representing the original SF data which I can use later on, I've got a working example on JSFiddle. Hope it helps.

Sunday 7 June 2015

Empty data array returned by ssp.class.php

I've just been reading a fantastic article and this is by far one of the best quotes in it:

"A senior developer is intimately familiar with their own failure."

It is perhaps because of the week I've just had that makes me feel like it's particularly brilliant. Let me explain: I've spent a week trying to re-code a really rather cool application (which my team wrote and which we're rightly proud) for another format… and, as you might've guessed, failed. It is not an insurmountable collection of issues but still issues that will take longer than I have to fix. I know just how I would fix them and the time involved in the fixing and I know the number of heartwarming highs and heartbreaking lows that effort would involve. The highs and lows I can live with but the time isn't something we can bear so it'll have to take a sprint or two or perhaps a re-examination of the whole solution just for that one format.

I'm balding and have dry-skin… I also have to moderate my diet and drink intake… I'm this close to dropping my cigarette intake to 3-a-day (I would give up but I sometimes think it's my vices that make me and 3-a-day sounds like I'm not really smoking at all!). Despite all this, when I sort out an issue in a project, I feel like I have the stature of a god (no, not Bhudda, some handsome Greek god). When I'm confronted with something that just doesn't work I feel each and every one of those extra pounds; each and every-flake of skin that lands on my t-shirt is indicative of my failure as not only a coder but member of the human race and each and every drink is a balm to my tortured soul.

I guess what I'm trying to say is that that there article by Matt Briggs has warmed my heart in that what I constantly feel is indicative that I deserve the title of Senior Developer… I guess. Perhaps as I ease myself into it I'll get myself down to 3-a-day, drop some pounds, shave my head and moisturise more. The drink intake has been successfully moderated as I really, really can't code with a hangover and drunken coding is a recipe for disaster!

Getting back to the title of this post though I noticed that a server-side script written to return rows for a DataTable wasn't. It was returning the number of results but the data array was stubbornly empty. I checked the variable array being sent to the server in the ajax code and everything seemed to be fine. I echoed out the queries that the server-side script was making and clocked that the SQL was limiting the result to 0… this was despite the length variable being set… I took to echoing out individual values from the array and everything seemed to be okay until I got to the length… I'd not long ago changed the table as I was showing too much data which wasn't relevant and wasn't showing some data that the client felt was relevant. But I was still returning that data, the new stuff was simply tacked onto the end of the query so the resulting variable was huge. And that was the answer, I was asking far too questions and because the variable array was so large it was getting truncated and I was losing the pagination data, effectively making the query end like this:

LIMIT 0 OFFSET 0

or

LIMIT 0, 0

No wonder I wasn't getting any data despite the pagination still working. I knocked off the extraneous fields from the table columns array and all was well. Always pays to check the length of the data being sent in a GET request though!

Actually I had a brain-wave while I was looking at what I'd just written and clocked that I use that extra data to populate modal dialogs should the user want to edit the data. But I can still return the data and then act upon it, the issue is in the asking for it. I can add all sorts of data to the row but I need to be careful about what I ask for! There was me getting ready to prune the dialog and the returned data and I don't need to, everything it still gravy: I just need to ask for the absolute minimum of what I need.