How to disable custom button button after one click in NetSuite SuiteScript2.0

2.1k Views Asked by At

I have created a custom button on a record using suitescript in userevent, when click that button it calls the client-script then the client-script will run after page will be refreshed. After click button it takes some time to refresh the page at that time button should be disable to avoid multiple clicks.

userevent script:

if(userRole == 3 || prStatus == "Partially Ordered"){

    scriptContext.form.addButton({
        id: "custpage_fullyorder_button",
        label: "Fully Order",
        functionName: 'make_PR_FullyOrder'
        });    
}

client script:

 function make_PR_FullyOrder(){
    //var sublistFieldName = context.fieldId; 
    if(typeof window != 'undefined' && window.document){
            window.document.querySelector('#custpage_button').disabled = true; // id from add button call in user event script
        }
    console.log('pr id:'+recordId);
    var pr_record = record.load({
      type:recordType,
      id: recordId,
      isDynamic: true
    });
   
   /* pr_record.setValue({
  fieldId:FullyOrderCheckBox,
  value:true
});*/
    console.log('pr loaded');
   // $("#custpage_fullyorder_button.rndbuttoninpt.bntBgT" ).prop( "disabled", true );
    console.log('2');
    //$("#custpage_fullyorder_button.rndbuttoninpt.bntBgT").attr("disabled", "disabled");
    //document.getElementById("custpage_fullyorder_button.rndbuttoninpt.bntBgT").disabled = true;
    //document.getElementByName('custpage_fullyorder_button').setAttribute("disabled","disabled");
    
//closed unlinked po item lins
var sublistLineCount = pr_record.getLineCount({sublistId:'item'});
        log.debug('item line count:',sublistLineCount);
    console.log('line count:'+sublistLineCount);

these are tried and commented. is there any jquery code include in suitescript???

1

There are 1 best solutions below

1
On

Share your code and I can be more specific. Generally to disable a button after it's been pressed once/executed you can do the following.

  1. Choose or create an identifier on the record that distinguishes the record before and after the button is pressed. Make sure the identifier used is accurate and distinct since anytime there is a value present, the button will not be displayed. I recommend creating a custom disabled field that's hidden on the system information tab (and can only be set programmatically).
  2. In the Client Script before the page refreshes set the value for the field mentioned in step 1.
  3. In the User Event script add logic to show the button if the field has a value, and hide/do not display the button if the field does not have a value.
//get the value from the identifying field
var identifier = curRec.getValue({fieldId: 'customfieldId'});
//if identifying field is true show the button
if (identifier == true){
  var customButton = form.addButton({
      id : 'custpage_button',
      label : 'Call Client Script',
      functionName: 'myLongRunningFunction'
  });
  form.clientScriptModulePath = './longRunningFunction_CS.js';
//if identifying field is false do not show the button/do nothing
} else {
  //do nothing
}

longRunningFunction_CS.js

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
define(["N/currentRecord"], function (currentRecord){
    function myLongRunningFunction(){
        if(typeof window != 'undefined' && window.document){
            window.document.querySelector('#custpage_button').disabled = true; // id from add button call in user event script
        }
        var rec = currentRecord.get();
        // do whatever processing...
        var custRec = record.load({ type: rec.type, id: rec.id });
    }

    return {
        myLongRunningFunction : myLongRunningFunction
    };

});