pageInit Function and Checking Context

The pageInit function is called when the transaction form finishes loading or when the form is reset. On the frontend, this can happen at numerous points:

A good way to describe these events is that the system is being asked to check if the user has a transaction record, and if they don’t, one is initialized.

Remember that these functions are also called on the backend. This means that this function will be called when an employee goes to create a new sales order or when an existing one is edited.

Remember that these functions are called on both the front and backends, so you may want to add a context checker to your functions. By using conditional checks to determine the context, frontend or backend, you can run the correct code for each environment. Checking context is not simply a design choice, it can be important because of the differences between these two environments with permissions and other considerations.

You can include context checking with your custom pageInit function as shown in the following steps:

  1. To edit the basic script in NetSuite, go to Documents > Files > SuiteScripts.

  2. Find Example Scriptable Cart in the list and click Edit.

  3. On the File page, find the Media Item field and click Edit.

  4. Add the getContext utility function to the top of the script, directly below the existing debug function.

                  function getContext ()
    {
      return nlapiGetContext().getExecutionContext();
    } 
    
                
  5. Add the following function below the existing customValidateLine function in the script.

                  function customPageInit ()
    {
      if (getContext() === 'webstore')
      {
        debug('You\'re in the frontend!');
      }
      else if (getContext() === 'userinterface')
      {
        debug('You\'re in the backend!');
      } 
    
                
  6. Click Save.

  7. Make sure this customPageInit function is associated with the NetSuite pageInit function.

    Go to Customization > Scripting > Scripts, click the Edit option for Example Scriptable Cart.

  8. In the pageInit Function field, enter customPageInit.

  9. Click Save.

  10. Test the updated script as follows:

    1. Go to your Commerce website, sign out, and then add one or more items to the shopping cart.

      So, let’s trigger this. On the frontend of your site, sign out, and then add an item to your cart. Visit the execution log and you should see the messages you added before, as well as the new one. Now, in the backend, go to create a new sales order. After the page has finished loading, refresh the execution log and you’ll see the other message in your conditional.

    2. Go to Customization > Scripting > Scripts to check the script execution log for the messages generated by your script.

    3. In the Execution Log tab, check for messages like these from the new function you added to the script: You're in the frontend!.

    4. In NetSuite, create a new sales order. For instructions, see Creating Sales Orders.

    5. Go back to the execution log for your script, refresh the log, and check for messages like these: You're in the backend!

Checking for Guest Users

If your script attempts to pull data from the user’s record, you first need to check that the user is logged in. The following code sample shows how to ensure a user is logged in.

In the customPageInit function, add the following code:

          var userId = nlapiGetFieldValue('entity');
if (parseInt(userId) === 0)
{
  debug('Guest user');
}
else
{
  debug('User id: ' + userId);
} 

        

If there is no value for the entity field, then a value of 0 is returned, indicating that the user is not logged in and is a guest user.

The following sample shows how the customPageInit function looks after you add this code:

          function customPageInit ()
{
  if (getContext() === 'webstore')
  {
    debug('You\'re in the frontend!');
  }
  else if (getContext() === 'userinterface')
  {
    debug('You\'re in the backend!');
  }

  var userId = nlapiGetFieldValue('entity');
  if (parseInt(userId) === 0)
  {
    debug('Guest user');
  }
  else
  {
    debug('User id: ' + userId);
  }
} 

        

After adding the code to check if a user is logged in, you can edit and test your script as described in the preceding steps.

Related Topics

Basic Custom Functions
validateLine Function
saveRecord and Updating a Custom Transaction Body Field

General Notices