Search code examples
javascriptnetsuitesuitescript

Close a Suitelet window after execution and remain on original page


I have a button on a saved search that runs a suitelet. It all works great except that the button opens a blank suitelet page when pressed.

This page is blank and requires no user interaction.

Is there any way to close it automatically either via another function within the button itself or by a function at the end of the script?

I have tried putting several variations of a window.close function in myself but it never closes.

The link from the search button and the Suitelet which it runs are below. Any help you can offer will be greatly appreciated.

(PS I have noticed that when I click the button on the search it navigates away from the search to a basic item search and I have to press the back button, how do I stop it doing this? Thank you)

Thank you

Button:

'<button onclick="window.open(/app/site/hosting/scriptlet.nl?script=602&deploy=1&compid=*******_SB2&field_id=' || {systemnotes.field} || '&rec_id=' || {internalid} || ');>Approve'

Suitelet:

/**

* @NApiVersion 2.x

* @NScriptType Suitelet

* @NModuleScope SameAccount

*/



define (['N/record'], function (record) {

function onRequest(scriptContext) {

var rec_id=scriptContext.request.parameters.rec_id; //getting parameter

var field_id=scriptContext.request.parameters.field_id; //getting parameter



var itemRecObj= record.load({ type: record.Type.INVENTORY_ITEM, id: rec_id, isDynamic:true });

if (field_id =='custom1'){

itemRecObj.setValue({ fieldId: 'checkbox1', value: true });

}

    else if (field_id=='custom2'){

itemRecObj.setValue({ fieldId: 'checkbox2', value: true });

}

else if (field_id=='custom3'){

itemRecObj.setValue({ fieldId: 'checkbox3', value: true });

}

    {

itemRecObj.save();

    }

function closer(){

window.open('','_self').close()();

}

}



return {

onRequest: onRequest

};

});

Solution

  • You can do something like this in button url after adding url "window.open(/app/site/hosting/scriptlet.nl?script=5737&deploy=1&compid=&h=&recId=' || {internalid} || ',_blank, toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400);"

    /**
    
    * @NApiVersion 2.x
    
    * @NScriptType Suitelet
    
    * @NModuleScope SameAccount
    
    */
    
    define (['N/record'], function (record) {
    
    function onRequest(scriptContext) {
    
    var rec_id=scriptContext.request.parameters.rec_id; //getting parameter
    
    var field_id=scriptContext.request.parameters.field_id; //getting parameter
    
    var itemRecObj= record.load({ type: record.Type.INVENTORY_ITEM, id: rec_id, isDynamic:true });
    
    if (field_id =='custom1'){
    itemRecObj.setValue({ fieldId: 'checkbox1', value: true });
    
    }
    
        else if (field_id=='custom2'){
    
    itemRecObj.setValue({ fieldId: 'checkbox2', value: true });
    
    }
    
    else if (field_id=='custom3'){
    
    itemRecObj.setValue({ fieldId: 'checkbox3', value: true });
    
    }
       
    itemRecObj.save();
    
     scriptContext.response.write("Item Approved");   
    
    }
    
    return {
    
    onRequest: onRequest
    
    };
    
    });
    

    It will help. Further Check for window.open in js and you need to use it in saved search not in script.