unexpected behavior with string generated with server-side A

Posted by gwf on 19-Dec-2014 14:30

I want to take the field allValuesField, a string of the format "a, b, a, c, d, d, b, e", and use a formula field to create a different string with the short list of the included items but with only one occurrence each, i.e., "a, b, c, d, e".

This code works when defining allValuesString with the string itself:

var allValuesString = "a, b, a, c, d, d, b, e";
var allValuesArray = allValuesString.split(", ");

var shortList = [];
shortList[0] = allValuesArray[0];

var inList;

for (var x = 0; x <allValuesArray.length; x++) {
    inList = false;
    for (var y = 0; y < shortList.length; y++) {
        if (allValuesArray[x] === shortList[y]) {
            inList = true;
        }
    }
    if (inList === false) {
        shortList.push(allValuesArray[x]);
    }
}

shortListString = shortList.toString();

return shortListString;

* this returns "a, b, c, d, e" *

But when I replace the first line above and use the server-side API to grab the string from the field allValuesField like this:

var allValuesString = rbv_api.getFieldValue(objName, objId, "allValuesField");

the formula then returns the complete and unchanged list from allValuesField, i.e., "a, b, a, c, d, d, b, e".


What's happening here?

Thank you,
Greg

Posted by Godfrey Sorita on 22-Dec-2014 14:46

Hi Greg,

Not sure why the strict operators (===) are returning false when comparing the same array elements but I was able to make your code work by removing 1 equal sign (=) from the line below:

if (allValuesArray[x] === shortList[y]) {

While this solution will work on your given example, I'm afraid it will get a different result when there are varying number of spaces in the array elements with the same value(e.g. "a, a , a,    a"). Furthermore, it will return an error if the allValuesField is empty. 

Below is a modified version of your code to catch this issues:

var allValuesString = "{!allValuesField}".replace(/ /g, '');
var allValuesArray = allValuesString.split(",");
var shortList = [];
shortList[0] = allValuesArray[0];

var inList;

for (var x = 0; x <allValuesArray.length; x++) {
    inList = false;
    for (var y = 0; y < shortList.length; y++) {
        if (allValuesArray[x] == shortList[y]) {
            inList = true;
        }
    }
    if (inList === false) {
        shortList.push(allValuesArray[x]);
    }
}
shortListString = shortList.toString();

return shortListString;

Regards,

Godfrey

All Replies

Posted by pvorobie on 19-Dec-2014 15:29

It seems like your data processing does not use Rollbase API. So could you formulate your question in terms of Rollbase API? Where do you update field's value (I don't see this in your posting)?

Posted by gwf on 19-Dec-2014 15:34

Thanks, but I'm not sure of your question - this code is in a formula field. Isn't the rbv_api.getFieldValue using Rollbase API?

A stripped down version of the first operations does work, but something breaks later in the code. For example, the following works as expected:

var allValuesString = rbv_api.getFieldValue(objName, objId, "allValuesField");

var allValuesArray = allValuesString.split(", ");

return allValuesArray[0];

* returns "a" as expected *

Posted by Godfrey Sorita on 22-Dec-2014 14:46

Hi Greg,

Not sure why the strict operators (===) are returning false when comparing the same array elements but I was able to make your code work by removing 1 equal sign (=) from the line below:

if (allValuesArray[x] === shortList[y]) {

While this solution will work on your given example, I'm afraid it will get a different result when there are varying number of spaces in the array elements with the same value(e.g. "a, a , a,    a"). Furthermore, it will return an error if the allValuesField is empty. 

Below is a modified version of your code to catch this issues:

var allValuesString = "{!allValuesField}".replace(/ /g, '');
var allValuesArray = allValuesString.split(",");
var shortList = [];
shortList[0] = allValuesArray[0];

var inList;

for (var x = 0; x <allValuesArray.length; x++) {
    inList = false;
    for (var y = 0; y < shortList.length; y++) {
        if (allValuesArray[x] == shortList[y]) {
            inList = true;
        }
    }
    if (inList === false) {
        shortList.push(allValuesArray[x]);
    }
}
shortListString = shortList.toString();

return shortListString;

Regards,

Godfrey

Posted by gwf on 22-Dec-2014 15:39

Hi Godfrey,

Perfect, that does exactly what I need. Thank you!

Greg

This thread is closed