This topic contains 10 replies, has 0 voices, and was last updated by JacksonP 7 years, 9 months ago.

  • Author
    Posts
  • #1624

    BrettKnights

    In SS1.0 we could set a client script file for a record in view mode and do useful things. I’ve tried the same in SS2.0 using context.form.clientScriptModulePath = … ; The code works for a record in edit mode but not view mode. Anyone have a decent workaround?
    This is a cached copy. Click here to see the original post.

  • #1625

    darrenhillconsulting

    Ya, it sucks. I’ve added a inline html field on the beforeload and injected the javascript as a value. Of course, this way, you’re limited to the size of you script then (unless you have the script simply LOAD in a Netsuite file that contains your actual client script).

    So, obviously, a variation on that is to add an inline html field on the form, and set the default value as the script (make sure you uncheck the formula)

    *Don’t forget to wrap the script in the tags using the methods above

  • #1626

    chanarbon

    Hi BrettKnights ,

    I have tested the details on my end for view mode and it is successfully loaded. Here’s my sample:

    The Client Script:

    Code:
    define([], function(){
    function myFunc(){
    alert(“Hello World!”);
    }
    return {
    buttonfxn : myFunc
    }
    });
    The User event code

    Code:
    /**
    *
    * @NApiVersion 2.0
    * @NScriptType UserEventScript
    *
    */

    define([], function(){

    function myBeforeLoad(context){
    if(context.type == context.UserEventType.VIEW){
    var form = context.form;

    form.addButton({
    id : “custpage_sample_button”,
    label : “Sample Button”,
    functionName : “buttonfxn”
    });

    form.clientScriptModulePath = “./sampleClientModule.js”;
    }
    }

    return {
    beforeLoad : myBeforeLoad
    }

    });

  • #1627

    david.smith

    What chanarbon suggested should work.

    Keep in mind that events do not fire in view mode. There is no pageinit or fieldchange.

  • #1628

    BrettKnights

    What happens is chanarbon ‘s sample does work but the client script is not actually loaded until you click the button. The use case driving this is a client script that uses the messaging api in view mode to report the results of some action. with SS1.0 I used to be able to test whether the page was loaded and fire my client script in view mode — yes there was no pageInit but at least the script was present and normal javascript worked e.g. this should work but nothing loads until I press the button. Then the showMessage function is triggered twice — once from the setTimeout and once from the button click.

    Code:
    define([‘N/ui/message’, ‘N/currentRecord’], function(msg, currentRecord){
    window.console.log(‘processing script’);
    function showMessage(rec) {
    window.console.log(‘record status is ‘+ rec.getValue(‘status’));
    //if(‘Pending Approval’ == rec.getValue(‘status’)){
    var myMsg = msg.create({
    title: “PAYMENT ERROR”,
    message: rec.getValue(‘status’), //’Please Approve’,
    type: msg.Type.ERROR
    }).show({
    duration: 100000
    });
    //}
    }

    setTimeout(function(){
    showMessage(currentRecord.get());
    }, 1500);

    return {
    buttonfxn:function(){
    showMessage(currentRecord.get());
    }
    };

    });

  • #1629

    BrettKnights

    So hacking a bit on the button code gives a hack-y but working example not triggered by the button:

    Server Script:

    Code:
    /**
    *@NApiVersion 2.x
    *@NScriptType UserEventScript
    */
    define([‘N/record’, ‘N/log’, ‘N/ui/serverWidget’],
    function(record, log, ui) {
    function beforeLoad(context) {
    log.debug({title:’before load with ‘+ context.type +’ on ‘+ context.form.title});
    if (context.type != ‘view’) return;
    log.debug({title:’setting client script’});

    var inline = context.form.addField({
    id:’custpage_trigger_it’,
    label:’not shown’,
    type: ui.FieldType.INLINEHTML,
    });
    inline.defaultValue = “setTimeout(function(){ require([‘/SuiteScripts/testSS2/testSimpleClient’], function(mod){ console.log(‘loaded’); mod.showMessage();},500);”;

    //context.form.clientScriptModulePath = ‘./testSimpleClient.js’;
    }

    return {
    beforeLoad: beforeLoad
    };
    });
    Client Script:

    Code:
    define([‘N/ui/message’, ‘N/currentRecord’], function(msg, currentRecord){
    window.console.log(‘processing script’);
    function showMessage() {
    var rec = currentRecord.get();
    window.console.log(‘record status is ‘+ rec.getValue(‘status’));
    if(‘Pending Approval’ == rec.getValue(‘status’)){
    var myMsg = msg.create({
    title: “Not Committed”,
    message: rec.getValue(‘status’), //’Please Approve’,
    type: msg.Type.ERROR
    }).show({
    duration: 10000
    });
    }
    }

    return {
    showMessage:showMessage
    };
    });

  • #1630

    darrenhillconsulting

    Out of curiosity, why the timeout? Is there a reason you don’t use document.ready?


    BrettKnights replied on 10/28/2016, 08:36 AM: Fixed the previous example. The client script setTimeout was probably the one you were asking about. No reason at all. It was part of trying to figure out if the module was actually loading.

  • #1631

    BrettKnights

    This is just a poc and that it’s the simplest code that works. It’s also a little more independent of the life-cycle of the page — There are things that NS loads async. I don’t think they’ve gotten there with view pages (i.e. I think the views are all rendered server side) so, given that jQuery is dependably available on NS pages and this is meant to be run in view mode in production I’d probably use something like:

    Code:
    inline.defaultValue = “jQuery(function($){require([‘/SuiteScripts/testSS2/testSimpleClient’], function(mod){ console.log(‘loaded’);mod.showMessage();});})”;

  • #1632

    chanarbon

    Agreeing with David, the events don’t trigger in view mode. And yes, the library seems to loads only right after the button gets clicked

  • #1633

    smurphy820

    this looks like a situation a workflow action script triggered on before load would give you exactly what you wanted. No?

  • #1634

    JacksonP

    I haven’t used module path but I’ve got a Client script on a button using clientScriptFileId

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType UserEventScript
    * @NModuleScope Public
    */
    define([‘N/search’],

    function(search) {

    function beforeLoad(scriptContext) {

    var estimateRecord = scriptContext.newRecord;

    var entity = estimateRecord.getValue({
    fieldId: ‘entity’
    });

    var form = scriptContext.form;

    form.addButton({
    id : ‘custpage_ss_touchpointbutton’,
    label : ‘Log Touchpoint’,
    functionName : ‘loadTouchpointForm’
    });

    form.clientScriptFileId = 5458877;

    }

    return{
    beforeLoad: beforeLoad
    };
    });

You must be logged in to reply to this topic.