This topic contains 6 replies, has 0 voices, and was last updated by silverriaz 8 years, 2 months ago.

  • Author
    Posts
  • #1826

    northern_brewer

    I challenged one of my developers to do his next project in SS2.0.

    He wants to put a button on a page (in this case Work Orders) and trigger a simple JavaScript action when somebody clicks on it.

    He’s using a User Event Script to place the button, like so:

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType UserEventScript
    */
    define([‘N/log’],
    function(log) {

    function beforeLoad(context) {
    var button = context.form.addButton({
    id: “custpage_nb_test_button”,
    label: “Test Button”,
    functionName: “testFunctionOne”
    });

    log.debug(“button info”, button);
    }
    return {
    beforeLoad: beforeLoad
    };
    });
    What we are confused by is that we don’t know how to put a function called “testFunctionOne” into the Global namespace on the Work Order form.

    We tried writing a 2.0 Client Script with a function called “testFunctionOne”, but it appears in a different namespace and can’t be called by the button click.

    Any pointers?
    This is a cached copy. Click here to see the original post.

  • #1827

    david.smith

    You need to add your client script with the entry point.

    Code:
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    define([], function() {
    function testFunctionOne() { alert(“Clicked.”); }

    return {
    testFunctionOne: testFunctionOne };
    });

  • #1828

    northern_brewer

    Tried that, but it is not working. We can’t seem to call our 2.0 ClientScript Javascript functions from our 2.0 UserEventScript because they are in different scopes.

  • #1829

    david.smith

    The following code works for me on the customer record. I’ve used the clientScriptFileId to identify the client script for this UE.

    User Event

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType UserEventScript
    * @NModuleScope SameAccount
    */
    define([],

    function() {

    function beforeLoad(context){
    var button = context.form.addButton({
    id: “custpage_nb_test_button”,
    label: “Test Button”,
    functionName: “dosomething”
    });

    log.debug(“button info”, button);

    context.form.clientScriptFileId = “5832”;
    }

    /**
    * Function definition to be triggered before record is loaded.
    *
    * @param {Object} scriptContext
    * @param {Record} scriptContext.newRecord – New record
    * @param {Record} scriptContext.oldRecord – Old record
    * @param {string} scriptContext.type – Trigger type
    * @Since 2015.2
    */
    function beforeSubmit(scriptContext) {
    log.debug(‘beforeSubmit’,scriptContext);

    var isCustomer = scriptContext.newRecord.type == “customer”;

    require([‘N/error’], function (error) {
    if(isCustomer){
    log.audit(“In the clear”)
    } else {
    throw error.create({
    name : “ERROR_RECEIVED”,
    message : ‘reason’
    });
    log.error(“Error Thrown”);
    }
    })
    }

    return {
    beforeLoad: beforeLoad,
    beforeSubmit: beforeSubmit
    };

    });
    Client Script (File ID = 5832 in the file cabinet)

    Code:
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    define([], function() {
    function dosomething() { alert(“Clicked.”); }
    function pageInit(){}; // needed by NetSuite
    return {
    dosomething: dosomething,
    pageInit: pageInit
    };
    });

  • #1830

    northern_brewer

    Super helpful, thanks. The key thing we didn’t understand was that you had to set the clientScriptFileId.

    Here’s our example code from a Work Order:

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType UserEventScript
    */
    define([‘N/log’],
    function(log) {
    function beforeLoad(context) {
    context.form.clientScriptFileId = 1406;
    var button = context.form.addButton({
    id: “custpage_nb_test_button”,
    label: “Test Button”,
    functionName: “phoneHome”
    });
    }
    return {
    beforeLoad: beforeLoad,
    };
    });

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType ClientScript
    */
    define([‘N/record’],
    function(record) {
    // Scripts have to implement at least one action
    function pageInit(context) {}
    function phoneHome(context) {
    console.log(“hey it works.”);
    }
    return {
    pageInit: pageInit,
    phoneHome: phoneHome
    };
    });


    david.smith replied on 09/08/2016, 10:35 AM: Because the User Event is server side code. Nothing there will be in the client’s browser. So you have to have a Client Script on the page with the function(s) you want the new button to work with. This method is just easier than defining a Client Script record and deployment.

    Admittedly, I think this was easier with SS1.0 but that’s just my humble opinion. I also think that NetSuite should remove the requirement of having one of their return entry points.

  • #1831

    smurphy820

    I do this with a Workflow and workflow Action Script. I Use The workflow to add the button on record load in one stage, then, add a second stage and setup a transition using the button click. On Entry of the 2nd State you can run your workflow action script. You can either put your code right in the WFA or use the WFA to call s library script.

  • #1832

    silverriaz

    … or you can simply call a different page or a suitelet by doing this:

    Code:
    var form = scriptContext.form;
    var trainingSchedulerScriptURL = url.resolveScript({
    scriptId : modulex.SCHEDULE_TRAINING_TRIGGER_SCRIPT,
    deploymentId : modulex.SCHEDULE_TRAINING_TRIGGER_SCRIPT_DEPLOYMENT,
    returnExternalUrl : false,
    params : { “jrecid” : scriptContext.newRecord.id }
    });

    form.addButton({
    “id”: “custpage_btn_schedule_training”,
    “label”: “Schedule Training”,
    “functionName” : ‘window.open(” + trainingSchedulerScriptURL + ”);’
    });

You must be logged in to reply to this topic.