Post

1 follower Follow
0
Avatar

Javascript help in calculated field

I have a calculated field that is simply trying to return the OEM field from a string of data.

Podio field name: DataFromDW (a text field)

The value in the field: "Sam Butter Chevrolet|Chevrolet|Live - OK|213|0:Bill Blah Chevrolet|Chevrolet|Live - OK|213|0:Sally Jules Isuzu|Isuzu|Onboarding|213|0-10";

-If I try to use the text field (DataFromDW) in a calculated field with Javascript, it errors out, as shown below:

// BEGIN NON-working CODE:
// USING the text field in Podio called DataFromDW
var sDealers = @DataFromDW;

var aDealers = sDealers.split(":");

var myDict = {};

for (var i = 0; i < aDealers.length; i++) {
var aDealerNameValuePairs = aDealers[i].split("|");
myDict[aDealerNameValuePairs[0]] = {};
myDict[aDealerNameValuePairs[0]]['oem']=aDealerNameValuePairs[1];
myDict[aDealerNameValuePairs[0]]['DealStage']=aDealerNameValuePairs[2];
myDict[aDealerNameValuePairs[0]]['distance']=aDealerNameValuePairs[3];
myDict[aDealerNameValuePairs[0]]['WorkTrucksStock']=aDealerNameValuePairs[4]

};

myDict['Sam Butter Chevrolet']['oem'];

// END NON-working CODE

// broken code results in the error "TypeError: Cannot read property 'oem' of undefined."

However if I run the same script in my calculated column in Podio and provide the value
of the variable "sDealers" directly in the script, it works as expected:

//BEGIN WORKING CODE:

@DataFromDW; //Podio requires this reference.

var sDealers = "Sam Butter Chevrolet|Chevrolet|Live - OK|213|0:Bill Blah Chevrolet|Chevrolet|Live - OK|213|0:Sally Jules Isuzu|Isuzu|Onboarding|213|0-10";

var aDealers = sDealers.split(":");

var myDict = {};

for (var i = 0; i < aDealers.length; i++) {
var aDealerNameValuePairs = aDealers[i].split("|");
myDict[aDealerNameValuePairs[0]] = {};
myDict[aDealerNameValuePairs[0]]['oem']=aDealerNameValuePairs[1];
myDict[aDealerNameValuePairs[0]]['DealStage']=aDealerNameValuePairs[2];
myDict[aDealerNameValuePairs[0]]['distance']=aDealerNameValuePairs[3];
myDict[aDealerNameValuePairs[0]]['WorkTrucksStock']=aDealerNameValuePairs[4]

};

myDict['Sam Butter Chevrolet']['oem'];

//this way gives the correct result of "Chevrolet".
// END of WORKING CODE

It seems that something "weird" is happening to my string of data when it is passed as a variable into my calculated Podio field. Any ideas on what may be happening? When I "echo" the value of the variable in a simple calculated field, it looks the same as the value I am inputting to the DataFromDW field.

Thanks
Justin

Justin Onstot

Please sign in to leave a comment.