Skip to main content
Published: May 02 2012, 2:55:00 PMUpdated: November 28 2020, 10:27:30 PM
Product

Here are some tips on Do's and Don't s while writing your JS files for MIP Transformation.

Get Value of an Element

element.toString();

Example Payload:

<Product>
    <Title>Hello World</Title>
</Product>

Code: PRODUCT_INRECORD.Title.toString();

Result: Hello World

    

Get Name of an Element

element.name().toString();

Payload:

<Product>
    <Title>Hello World</Title>
</Product>

Code: PRODUCT_INRECORD.children()[0].name().toString();

Result: Title

   

Get Value of an Attribute

element.@attribute.toString()

Payload:

<Product>
    <Weight unit="lbs">23</Weight>
</Product>

Code: PRODUCT_INRECORD.Weight.@unit.toString();

Result: lbs

   

Check for Existence of an Element

If you have an optional element, e.g. <baz> inside of <foo> in this example:

<foos>
    <foo>
        <bar>value</bar>
        <baz>somevalue</baz>
    </foo>
    <foo>
        <bar>avalue</bar>
    </foo>
</foos>

And your CONFIG.RecordPath = '/foos/foo', you can check for the existence of <baz> by doing:

if( PRODUCT_INRECORD.baz.length() == 1 ) {
    //Do something.
}
     

Check if Element is a Leaf

var children = element.children();
if(children.length() == 1 && children[0].children().length() == 0) {
    //element is a leaf
}
else {
    //element is not a leaf
}
        

Get All Leaf Elements from a Root Element

var elements = root.descendants();
for each(var element in elements) {
    var children = element.children();
    if(children.length() == 1 && children[0].children().length() == 0) {
        //element is a leaf
    }
    else {
        //element is not a leaf
    }
}
       

For Loop Over Array Using for-each

var elements = functionWhereYouGetYourArrayReturned();
for each(var element in elements) {
    //Do work, e.g. element.toString(), etc.
}
    

For Loop Over XMLList Using for-each

Example Payload:

<Image>
    <SKU>ABC</SKU>
    <Picture>http://host.com/1.jpg</Picture>
    <Picture>http://host.com/2.jpg</Picture>
    <Picture>http://host.com/3.jpg</Picture>
</Image>

var pictures = IMAGE_INRECORD.Picture; //an XMLList
for each(var picture in pictures) {
    IMAGE_SERVICE.addToProduct(PRODUCT_INRECORD.SKU.toString(), picture);
}
// ++++++++++++ OR, ALTERNATIVELY ++++++++++++
var pictures = IMAGE_INRECORD.Picture; //an XMLList
for (var i = 0; i < pictures.length(); i++) {
    IMAGE_SERVICE.addToProduct(PRODUCT_INRECORD.SKU.toString(), pictures[i].toString());
}

   

For Loop Warning: Always Use Var!

Bad:

for(i = 0; i < 5; i++) {
    //Do stuff
}

Good:

for(var i = 0; i < 5; i++) {
    //Do stuff
}

If you accidentally omit the "var" in front of "i", it is treated as a global variable. If you repeat the same mistake later in the code, it could use stale values of "i", etc. This caused an infinite loop once, so be careful!

    
Undefined vs. Null
  • PRODUCT.CustomFields.Foo will return undefined if it has not been set. (same logic applies for ItemSpecifics)
      

WARNING: HTML Entities Unescaped Upon toString()

If a user has an element like the following:

<Title>Nike&amp;reg; AirMax 95</Title>

and you have

var title = PRODUCT_INRECORD.Title.toString();

The variable "title" will evaluate to

Nike&reg; AirMax 95

vs. what you might have expected:

Nike&amp;reg; AirMax 95

How well did this answer your question?