Thursday, May 19, 2011

Working with Scriptable Cart in NetSuite Webstore - Lessons Learned

We've MOVED!!!! www.codeboxllc.com/ksc

It's been a while since my last post. I have a good reason. I've been struggling with Scriptable Cart feature in NetSuite advanced Webstore. After close to 9 days of none stop work, I think I've finally have it working.

This is new feature NetSuite released and it's still in an infant stage yet very powerful when coded correctly.
First thing I struggled with the most was not being able to see scripts' behavior when executed from the Webstore. You can read about on "How to debug NetSuite Webstore Scriptable Cart".

This gave me some major insight as to how NetSuite Webstore processes each addition/subtraction of items in the cart.

I want to give shout out to Jason K. for providing massive assistance in getting my cart script to work properly!

Here are some things I've found out that could help you:
  1. You can get execution environment variable using nlapiGetContext().getEnvironment() call. I didn't think this was possible but using this in your cart script actually returns SANDBOX, BETA or PRODUCTION from webstore.
     This will be very handy if you have to switch item ID depending on the environments.

  2. nlapiCommitLineItem('item') Is a MUST Call if you want your recalc to correctly recalculate totals. I tried taking the easy way out by not calling commit. What a newb mistake that was.

  3. nlapiLookupField() call can not be used. Obviously right? Well, I made this mistake as well. You may have an instance where you need to create item list table to add pro grammatically. I highly recommend using JSON object to create your item look up table.

  4. User selected items will ALWAYS be at line number 1 of the cart. The order of items may change after you've been to check out summary page but when it's added to the cart by the user; not script, it'll always be on the first line of item list.

  5. Jason K's trick 1: nlapiSelectLineItem results in an error if there's uncommitted values in the item list. He recommended to commit all items before adding new.
    I especially had issues with this. My work around was to use nlapiInsertLineItem with nlapiSetCurrentLinetItemValue method.

  6. Not sure if this will apply to everyone but IF you end up getting NetSuite Script Notice page during your execution of cart script or you end up getting Webstore maint. screen, it usually means there is an error in your script. For me, it was infinite loop issue. If you see your form re-initializing, it usually indicates error.

  7. Jason K's trick 2: Using global variable in the cart script sometimes fails. The workaround is to create custom body field on your form and use that as temporary global field that you set and get using nlapiGetFieldValue() call.
  8. Jason K's trick 3: When doing extra commit, always check for valid item in the current row. 
Some quick sample code from Jason K on safeSelectLine and safeSelectNewLine

** notEmpty is a custom function which simply checks to see if passed in value is null or empty string

safeSelectNewLine function:
function safeSelectNewLineItem() {
  if (notEmpty(nlapiGetCurrentLineItemValue('item','item'))) {
    nlapiCommitLineItem('item');
  }
  nlapiSelectNewLineItem('item');
}
safeSelectLine function:
function safeSelectLineItem(itemLine) {
  if (notEmpty(nlapiGetCurrentLineItemValue('item','item'))) {
    nlapiCommitLineItem('item');
  }
  nlapiSelectLineItem('item', itemLine);
}
Most important lesson I've learned is that cart script behaves differently in webstore. Do test your code on the external form but be extra careful not to fully trust the result. When testing on webstore, keep close eye on your log execution files. If you ever see page init more than once, you know you have an error somewhere.

As I mentioned before on my previous post, turning on logging on webstore will slow down your cart since it's calling to your external suitelet so keep that in mind as well. 

Hope this helps you. Let me know if you get stuck. I'll try my best to help you out.

3 comments:

  1. Hi,

    ieve been working with Netsuite webstores for close to 5 years... and i gotta say its an absolute disaster.. so much so that we built our own CMS that integrates into NS so our customers could have the best of the best without having to shell out the cost of the Magento Connector... I gotta say it is a joke that they havent made the store functionality worthwhile and sooo many bugs still exist... Lets hope the release next year with greater customisations and scripting actually works..

    Its quite funny because the scriptable checkout was released a few years ago... and then it wasnt supported as they knew it was a disaster... but good to see someone got some functionality out of it...

    ReplyDelete
  2. Hi David. Thx for your comment. What other areas of NetSuite do you see needs improving other than Webstore?

    ReplyDelete
  3. Hi,
    We have scriptable cart enabled and had only two client scripts with some simple logics and have followed all the best practices and guidelines as specified in developer documentation but its still taking 20 secs or 15 secs of time to go to check out page from cart

    ReplyDelete