Call a Suitelet script from another Suitelet script

Generally, when we create a suitelet page, we add the script(a suitelet script which is going to be called from another suitelet) to the button of another suitelet page, by which clicking that button from the first suitelet, we can redirect to another suitelet page.

But that will be possible, if either we don’t need any parameter or we know the parameters which are going to be sent to the next suitelet from the beginning(while creating the first suitelet).

Here is an example:

Let’s say, we want a suitelet page, which will be called from an Item record.

That Suitelet page will contain That Item and some other information and a button called “Redirect”.

We want another suitelet script which will be called by clicking that “Redirect” button and we want that Item should be sent as a parameter to the second suitelet to show information regarding that Item.

Here when we will create the first suitelet, we will get the Item from Item record and will populate that in the suitelet page. So here we know the parameter(item) from the beginning. So we can attach the second suitelet to the “Redirect” button of first suitelet.

Sample Code:
SuiteletForm1.js

function CreateFirstSuiteletForm(request, response)

{

   if (request.getMethod() == "GET") {

      //Get Item ID

      var item = request.getParameter("record");

 

      //Create Form

      var form = nlapiCreateForm("Suitelet Form 1");

      var itemList = form.addField("itemname", "select", "Item", "item");

      itemList.setDefaultValue(item);

      itemList.setDisplayType("inline");

      //Get second suitelet URL and attach parameter to it

      var suiteletUrl = nlapiResolveURL('SUITELET', 'customscript_suitelet_form_2', 'customdeploy_suitelet_form_2', false);

      var param = '&record=' + item;

       var finalUrl = suiteletUrl + param;

       var newWindowParams = "width=750, height=400,resizeable = 1, scrollbars = 1," +

       "toolbar = 0, location = 0, directories = 0, status = 0, menubar = 0, copyhistory = 0";

       var setWindow = "window.open('" + finalUrl + "','Suitelet Form 2','" + newWindowParams + "')";

      //Add Button and attach suitelet to that button

      form.addButton("custpage_redirect", "Redirect", setWindow);

      response.writePage(form);

   }

}

Then in the second suitelet, we will get the parameter and will do as per our requirement.

Now comes the second situation.

Let’s say there will be a suitelet page, where we will have a Item dropdown list and a button called “Redirect”. Now we have to select one Item from the Item dropdown and click the “Redirect” button.

Then we need to redirect to the second suitelet with the parameter(the selected Item).

Now the problem is we can’t know the Item which will be selected while creating the first suitelet. So we can’t attach the second suitelet to the button “Redirect” of the first suitelet.

Here is a solution to this problem.

Fist we will create a Suitelet(First Suitelet), where in the “GET” part we will add a Itemselect field and a submit button(Redirect).

Then in the else part which is the submit button click event, we will redirect to the Second Suitelet.

EXAMPLE:

SuiteletForm1.js

function CreateFirstSuiteletForm(request, response)

   {

   if (request.getMethod() == "GET") {

 

      //Create Form

      var form = nlapiCreateForm("Suitelet Form 1");

      form.addField("itemdropdown", "select", "Item", "item");

      form.addSubmitButton("Redirect");

      response.writePage(form);

   }

   //“Redirect” Button Click

   else

   {

      //Get selected item from Item DropDown

      var selectedItem = request.getParameter("itemdropdown");

      //Create an array to store the required parameter

      var param = new Array();

      param['item'] = selectedItem;

      //Redirect to second Suitelet Page

      response.sendRedirect("SUITELET", "customscript_suitelet_form_2", "customdeploy_suitelet_form_2", false, param);

   }

}

Then in the second suitelet script:

SuiteletForm2.js

function CreateSecondSuiteletForm(request, response)

{

   if(request.getMethod() == “GET”)

   {

      //Get Item from parameter

      var Item = request.getParameter("item");

      //do whatever you want

   }

}

Note:

SendRedirect: It is used as a method of nlobjResponse.

Parameters: Only in this context

1.type: SUITELET

2.Suitelet script ID: customscript_suitelet_form_2

3.Suitelet script deployment ID: customdeploy_suitelet_form_2

4.make it false for internal suitelet or true for external suitelet : false

5.required parameters as an array: param

To know more about sendRedirect, search in Netsuite Help.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!