I'm new to this site and to programming so I apologize upfront for any errors. But I'm trying hard to learn JavaScript.
I'm using JavaScript to parse an XML file which contains location records. If the XML file contains more than 1 location record then the below code works as expected and creates the correct number of location records. If the XML file contains only 1 location record then it fails to create the location record.
I've found numerous posts on this site regarding this issue which stated "JavaScript object does not have a length propertyy, only Arrays do". And we are converting the XML file into an object. However, I have not been able to figure out how the suggested solutions are to be applied to my code. When I have 1 location record the location.length = undefined but when there are 2 records the location.length = 2.
What needs to be changed in my code so I can successfully create the Location record when the XML file only contains 1 record? Thank you for your help!
var xmlHelper = new XMLHelper(source.u_ebo_ebopayload);
var obj = xmlHelper.toObject();
JSUtil.logObject(obj);
var location = obj['ebo:Loc'];
gs.log('LocTransform - location object: ' + obj['ebo:Loc']);
//One record in XML returns '[object Object]'
//Two records in XML returns '[object Object],[object Object]'
gs.log('LocTransform - location length: ' + location.length);
//One record returns 'undefined'
//Two records return '2'
//Start loading data for each location record
for (var i = 0; i < location.length; i++) {
gs.log('LocTransform - Inside the For Loop with ' + location[i]['ebo:LocNbr']);
//One record does not get inside FOR loop so no location record is created
//Two records do get inside FOR loop and create both location records
var gr = new GlideRecord('imp_location');
gr.u_locnbr = location[i]['ebo:LocNbr'];
gr.u_locnm = location[i]['ebo:LocNm'];
gr.insert();
}
No comments:
Post a Comment