Saturday, August 27, 2011

Script Parameters: Where have you been all my life??

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

Ok. perhaps script parameters been around for a while. I just failed to see the usefulness of it. You can read all about script parameters by going to NetSuite Help > SuiteFlex > SuiteScript > Creating Script Parameters (Custom Fields)

While back, I had one of NetSuite Custom Script guy review my codes. I asked him, "Please, imagine you are paying me to do your work and you are doing a code review of my work. Just rip it part and be very critical".
And he did just that. One of the suggestion he gave me was to utilize Script Parameters to turn my scripts into "Configurable" script.

What does configuration give you? Re-usability. As you see your script library grow, re-usability plays very important role in keeping your NetSuite account clean and organized.

Think of following situations where you can use power of configuration
1. Script that applies to multiple entity record types that sets same fields depending on the situation:
- You could hard code multiple entity  record types in to the script but that's not scalable. If you end up wanting to add new record, you need update the script and deploy.

- An alternative would be to create one script that takes record types as a parameter and DEPLOY it multiple times. It can easily be extended out to other entity record types by creating another deployment of the script with value of parameter as records internal id.

2. Script that takes different saved searches but ultimately does same thing for each records:
- You could hard code different types of saved searches but again, how scalable is that?

- An alternative would be to create one script that takes internal id of a saved search and DEPLOY it multiple times.

I think you are starting to see the trend here. Whats really cool about script parameter is that it can be text field, drop down list or a check box. Why is this cool? It's easy to set the parameters. Typing in internal ID might not be too user friendly but what if you want to set list of items, departments, or custom record as your parameter? Instead of looking up the internal ID of those records, you simply set it. EASY RIGHT!!!!

Keep in mind, if you want to set custom parameter specific for your script, you leave the Preference as blank.
You have two other options for this preference; Company and User. You can see full details by going to NetSuite Help > SuiteFlex > SuiteScript > Setting Script Parameter Preferences


You create your parameter(s) during your script creation stage. You set values for those parameters when you deploy your script. 

ID of your custom parameter starts with custscript prefix. Once you know what your ID is going to be, you can gain access to it with following code:
nlapiGetContext().getSetting('SCRIPT','YOUR CUSTOM FIELD ID');
On my screen shot above, I named my parameter _testparam. Full sample code will look like this:

 nlapiGetContext().getSetting('SCRIPT','custscript_testparam');
I can't wait to go back and rewrite all my scripts. Happy coding everyone!




1 comment:

  1. Reading your article I have discovered answers for number of questions that have been troubling me for long time now. Its not easy to find professional publications on the web as many such publications are made by people with no knowledge of the topic. Your publication is excellent and definitively worth reading. I’ll sure be back to check for updates in few days.

    ReplyDelete