Skip to content
Nov 20 10

Close Popup Window and Refresh Opener

by admin

Here’s the scenario, you created a custom button in one of your page (say, Customer) and when clicked you want a popup window to open another page (say, Task). Sounds Easy Right? Your code in creating the button might be similar to this:

var url = nlapiResolveURL("RECORD", "task", null, false);
var script = "win = window.open('" + url + "', 'win', 'resizable=0,scrollbars=0,width=950,height=700');";
form.addButton("custpage_newtask", "New Task", script);

When you click the button, a popup window will open. The Create New Task page will be loaded in the newly opened window. All seems to be working fine until you click the Save button. The task will be saved but after the postback, the customer record will be loaded INSIDE the same popup window.

How to fix this? Calling a window.close() inside the saveRecord function of the Task’s client-side SuiteScript will do no good since saveRecord will trigger before the actual saving process. One possible workaround would be to add an afterSubmit function in our server-side SuiteScript that will redirect the page to a Suitelet. The suitelet will be a blank/white page when viewed but it will trigger the refresh of the opener and the closing of the popup window. Let me explain this via code:

The afterSubmit function of the Task can be like this:

function afterSubmit(type)
{
    if (type == "create")
    {
        var params = new Array();
        params["custpage_customerid"] = 12345;
        nlapiSetRedirectURL("SUITELET", "customscript_blankredirect", "customdeploy_blankredirect", null, params);
    }
}

And the suitelet code can look like this:

function main(request,response)
{
    var customerid = request.getParameter("custpage_customerid");

    var html = "";
    html +=" <html>";
    html +=" <head>";
    html +=" <script>";
    html +=" window.opener.location='" + nlapiResolveURL("RECORD","customer", customerid, null) + "';";
    html +=" window.close();";
    html +=" </script>";
    html +=" </head>";
    html +=" </html>";

    response.write(html);
}

There you go! Happy coding.

Oct 26 10

Why I Do Not Trust a NetSuite Date

by admin

I learned this the hard way. Consider the following code:

var so = nlapiLoadRecord("salesorder", "3479274");
var tranDate = so.getFieldValue("trandate");
var adjustedDate = nlapiAddMonths(tranDate, 2);

At first look all seems fine but the above code will throw this error as it passes through the third line:

UNEXPECTED_ERROR TypeError: Cannot find function getTime.

I didn’t call any getTime function. How come I’m having this error?

Debugging my code reveals that the .getFieldValue(“trandate”) part returns the date as string. It also follows the date formatting defined in the user preference. If the trandate of the SO is 12/15/2009 and your date formatting is set to DD-MONTH-YYYY, the resulting value would be “15-DECEMBER-2009”. Having said that now it’s obvious that doing nlapiAddMonths(“15-DECEMBER-2009”, 2) will definitely throw an error because the first parameter is not a valid date value.

date-formatting

Luckily, NetSuite API has a nlapiStringToDate() method to solve this dilemma. Adding the method in our code solves our problem:

var so = nlapiLoadRecord("salesorder", "3479274");
var tranDate = nlapiStringToDate(so.getFieldValue("trandate"));
var adjustedDate = nlapiAddMonths(tranDate, 2);

On a similar note, getting the value of a date field in a nlobjSearchResult object will also give the date as string following the format defined in the user preference.

Happy coding!

Oct 19 10

recalc: Inserting New Item Line Dynamically

by admin

We can easily achieve this if the line will be added upon page load or if a button in the form is clicked. But the requirement is to add a new Item line after inserting another Item. How can we do this?

This can be achieved via the client event function called recalc. According to NetSuite help, is an

Event occurs after a line has been added to a sublist. This allows for any global actions that change whenever the contents of the sublist change.

The function accepts one parameter, type, which is the sublist internal id.

As an example, the Sales Team requires us to add a discount item in the Sales Order whenever a customer orders a particular item. Below is what our recalc function looks like:

function doRecalc(type)
{
    if (type == "item" && nlapiGetRecordType() == "salesorder")
    {
        if (nlapiGetCurrentLineItemValue("item", "item") == SPECIAL_ITEM)
        {
            nlapiSelectNewLineItem("item");
            nlapiSetCurrentLineItemValue("item", "item", DISCOUNT_ITEM, true, true);
            nlapiCommitLineItem('item');
        }
    }
}

Note that SPECIAL_ITEM and DISCOUNT_ITEM is the internal id of our sales item and discount item respectively.

Jan 13 10

SuiteScript: Inventory Transfer via Code

by admin

Pre 2009.2, transferring inventory quantity from one location to another via SuiteScript can only be done through inventoryadjustment. First you have to add quantity to destination location.

xfer.setLineItemValue("inventory", "item", 1, ITEM_ID);
xfer.setLineItemValue("inventory", "location", 1, LOC_DESTINATION);
xfer.setLineItemValue("inventory", "adjustqtyby", 1, "5");

Then you have to deduct the quantity from the origin location

xfer.setLineItemValue("inventory", "item", 2, ITEM_ID);
xfer.setLineItemValue("inventory", "location", 2, LOC_ORIGIN);
xfer.setLineItemValue("inventory", "adjustqtyby", 2, "-5");

Putting it all together, the code will roughly look like this:

read more…

Nov 5 09

Show Field’s Internal ID

by admin

NetSuite’s Field Help is a small pop-up window that displays a brief description of the field associated to it. To open it, simply click the field label.

internal-id-01

read more…

Oct 29 09

New Implementation of nlapiStringToDate in 2009.2

by admin

All of a sudden, our users complained that one of our NetSuite process failed. They reported this error:

TypeError: Cannot call method “getTime” of null (some_code.js$sys#791)

I tried to debug the code using nlapiLogExecution can still couldn’t locate where the error occured. I up the ante by using the script debugger and found that the error happens because the code is trying to call the getTime of a null object. Pretty self explanatory, but why did it happen? The code roughly looks like this:

var billDateStr = rec.getFieldValue("custrecord_billdate")
var billDate = nlapiStringToDate(billDateStr);
var billTime = billDate.getTime();

read more…

Oct 9 08

Document Outline for Client Script in Visual Studio 2005

by admin

One feature I like most in Visual Studio 2003 is their Document Outline Panel. I usually abuse this feature on my JavaScript code to easily see and jump through my functions.

read more…

Jun 18 08

Import Yahoo! 360 Blog To WordPress

by admin

Before having my own domain for my blog, I was a huge fan of Yahoo! 360. I love its simplicity and its integration with other Yahoo services. But my enthusiasm slowly faded since Yahoo decides to pull the plug on Y360. Then I found WordPress and I fell in love again. Now my major problem is how do I migrate all my Y360 entries into WP? The most obvious solution is to manually copy all my Y360 entries and paste it to WP one by one. If your post has comments and tags, you should do the same if you want it completely “save” the entry. But what if you have over 50 entries in Y360 with more than 20 comments each? What a nightmare.
read more…

May 23 08

Querying the Latest Record

by admin

It was my long standing problem when querying the latest history of a record. The scenario is that I have a parent table and its transaction log (say, history records) are stored in a child table linked by the parent’s primary key id. The parent table stores the main information regarding a transaction while the child table stores historical data of what happened in the parent’s table info.

parent-child relationship

read more…

May 19 08

PHP: Cannot Modify Header Information

by admin

I downloaded a very cool theme for this blog and luckily, I successfully installed it in my local instance of WordPress. Satisfying the OC in me, I immediately do some minor twicking to further personalize my page. I tested the page’s comment engine, posting, plugin and widget capabilities but was repeatedly bombarded with this error every time I click a button that causes the page to reload:

Warning: Cannot modify header information – headers already sent by (output started at / my_site/wp-content/themes/new_theme/functions.php:2) in /my_site /wp-includes/pluggable.php on line 694

read more…