Bind NetSuite SuiteScript Client Side Code in Form View Mode

There are times when you need to run a client side script when a NetSuite internal form loads in view mode.  It’s not obvious how to do this — there is no “on load” form event when the the form is loading for view mode.  Hence, it isn’t easy to link and fire your custom NetSuite script when the form loads.

Here is what you need to do.  Create a dynamic inlinehtml field server side that will push a client side script into the HTML stream.  Use a UserEvent and call the script on the OnInit method.  See below for the sample code.

var html = '<SCRIPT language="JavaScript" type="text/javascript">';
html += "function bindEvent(element, type, handler) {if(element.addEventListener) {element.addEventListener(type, handler, false);} else {element.attachEvent('on'+type, handler);}} "; html += 'bindEvent(window, "load", function(){'; html += 'alert("window onload event fired - put our logic here");'; html += '});'; html += '</SCRIPT>';
// push a dynamic field into the environment
var field0 = form.addField('custpage_alertmode', 'inlinehtml', '',null,null); field0.setDefaultValue(html);

With this approach, you will be able to include your custom client side logic.   Many thanks to Sean, one of our senior developers, for producing a simple example here.  Do you see a different way to accomplish the same goal?


Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - YouTube

| Tags: , , , , , | Category: NetSuite, Technical | 18 Comments

18 thoughts on “Bind NetSuite SuiteScript Client Side Code in Form View Mode

  1. Joe Son says:

    Marty, you are a life saver.
    THIS little trick got me through!!

    What an AWESOME tip!

    I will be writing about this and referencing this page!

    Thanks!

  2. This was very helpful, thanks Marty!

  3. Vijay Joshi says:

    Is it possible to Deploy user event script to get all fields and values on suitelet deployed in Netsuite ERP, same a we do for records?

  4. Marty Zigman says:

    Hi Vijay,

    I am not sure I understand what you suggesting. Can you state it a different way?

    Marty

  5. Hitesh Kumar says:

    I am getting the error form is not defined.
    I have created a script put your code in that and inserted in netsuit custom form.

  6. Marty Zigman says:

    Hello Hitesh,

    Are you able to read any form data?

    Marty

  7. kumar says:

    Hi sir,

    I have a component item with group of items, and i need to show this items on advanced pdf, earlier in the item record ‘ DISPLAY COMPONENTS ON TRANSACTIONS’ checkbox not checked, now i chewcked this check box and i am gettin items on the print.
    Here my problem is the old sales orders which are created before i modified item record, those group of items are not coming on pdf print for old sales orders.

    Please help me on this sir. I should not edit the sales orders to resolve this issue.

  8. Marty Zigman says:

    Hi Kumar,

    There is a good chance that the old Sales Orders need to be “Touched”. In our Prolecto Utilities Bundle, we have a Mass Update custom action that effectively allows you to edit and save a transaction without changing data. You may be able to do this also with a CSV upload.

    Marty

  9. Gus Larson says:

    I was thinking I could use this trick to run a client script from the Order Requisitions page but there is no way to trigger a user event script from going to that page, as far as I know. Our users want to be able to override the item rate that is in the item record. I know you can do that once you get to the Order Requisitions page by changing Order Rate, but they want to do it initially when creating the Requisition by using the Estimated Amount, Estimated Rate fields and have that carry over to the PO. Is there a way to run a script on the Order Requisitions page?

  10. Marty Zigman says:

    Hi Gus,

    I haven’t done programming yet on the Purchase Order Requisition page. I have always assumed it was basically the purchase order record revealed in a limited user context. I suspect, at worst, I could get at it when it becomes a traditional purchase order.

    Finally, for a client with a paperless purchase order / vendor bill approval ambition, we built our own purchase requisition systemwith powerful transaction approval routing using SuiteLet’s here.

    Marty

  11. Gus Larson says:

    Nice! I used the advanced procurement workflows and made significant changes to them for our requisition approval process. I ended up just transforming the Requisition to a Purchase Order in the workflow upon final approval and just skipping the Order Requisitions page. Then the Item Rate doesn’t get changed back to what was on the Item record and that’s fine for us because we never put more than one Requisition on a PO. Now I’m having trouble because we are using Demand Planning and the Order Items feature goes right to making a PO. I need it to make a Requisition instead. Any ideas about that?

  12. Marty Zigman says:

    Hi Gus,

    I don’t have specific recommendations as we haven’t seen this use case. But I suspect we could work through it. We tend to go to script to get full control. If you want to have one of our team members help, send me a note here.

    Marty

  13. Pat says:

    Hi Marty,

    I’m setting the inline-html value for each custom column field (of the Quote’s Item sublist). The setting is done via:

    itemList.setLineItemValue(‘custcol_my_new_field’, i, html);

    However it appears that there is a limit to how long the inline-html code can be. The code is intended to generate a button along with JavaScript handler function for each Item row. If the code is short enough, everything seems to work fine. If it exceeds a certain length, the rendering of the 1st row doesn’t even complete. Viewing the page source, it’s seen that the rendering ends with “(more…)”.

    Is there such a length limit?

    Thank you!

  14. Marty Zigman says:

    Hello Pat,

    I believe there may be a 300 character limit on line-level elements. Might you be able to modify your code to reference a separately supplied function?

    Marty

  15. Kirankanth says:

    wow! it worked like a charm.

Leave a Reply

Your email address will not be published. Required fields are marked *