SeamusSmyth
ServiceNow Employee

When you access the menu item named “Scripted Extension Points”

SeamusSmyth_0-1794899475297.png

 

The following item is provided out of the box:

SeamusSmyth_1-1794899475298.png

 

sn_pw.preSaveHandelrExtension

The description provided for this item is :

“This extension invokes from the ProjectWorkspaceSaveApi script include. Write an implementation to modify or validate the project workspace updates.”

SeamusSmyth_2-1794899475301.png

 

From the screen above, when you choose to “Create Implementation” a new Script Include is created

The SI has 4 items passed to it

  • Data
  • Columns
  • baseTable
  • topTask

 

To examine the content of these 4 items, add the following logging to the SI

 gs.info("Value for Basetable is :" + baseTable);

        gs.info("Value for topTask is :" + topTask);

        gs.info("Content of DATA is :" +JSON.stringify(data));

        gs.info("Content of COLUMN is :" +JSON.stringify(columns));

 

 

By doing this, when a task is added or updated in the Project Workspace Gantt console, the following entries can be seen in the System Log:

  1. The parent table of the record being edited– pm_project

SeamusSmyth_3-1794899475301.png

 

  1. The SysId of the project

SeamusSmyth_4-1794899475301.png

 

  1. The data being edited by the user. Here we get 1 entry for the task that the user is editing and 1 entry for the project (summary task)

SeamusSmyth_5-1794899475308.png

 

  1. The columns present

SeamusSmyth_6-1794899475315.png

 

 

 

 

Usage Example

Scenario : we want to force the user to select a value in a specific column on the Workspace before a task can be saved

 

The field in question is a String value with a default value (Select) and 3 additional values (A, B, C)

SeamusSmyth_7-1794899475316.png

 

 

In this scenario we want to prevent the user from saving the task with the value of ‘Select’

This sample SI was created to address the requirement

var alm2_preSaveHandelrExtension = Class.create();

alm2_preSaveHandelrExtension.prototype = {

    initialize: function() {},

 

    process: function(data, columns, baseTable, topTask) {

        gs.info("Basetable is :" + baseTable);

        gs.info("topTask is :" + topTask);

        gs.info("DATA SOURCE is :" +JSON.stringify(data));

        gs.info("COLUMN SOURCE is :" +JSON.stringify(columns));

        var tasks = data.tasks || [];

        for (var i = 0; i < tasks.length; i++) {

            var task = tasks[i];

            gs.info("This is the Value of the Category Field"+ task.u_ac.value);

 

         if ((task.u_ac.value == 'Select' || task.u_ac.value=='') && task.parent!=null) {

                        gs.info("Blocked field edit: ");

                        return {

                            data: data,

                            status: 'error',

                            message: 'A value for "Category" is required for: ' +task.short_description.displayValue+ '. Please refresh the page and try again'

                        };

                    }

           

       

        else{

            return {

            data: data,

            status: 'success', //success or error

            message: '', //if status is error then return the error message also

        };

        }

     

    }

    type: 'alm2_preSaveHandelrExtension'

    }

};

 

When this SI is active, the following will be the outcome when a user creates a task but does not set a value in the column we want to be filled

SeamusSmyth_8-1794899475318.png

 

To correct this situation the user should refresh the page and add the task once more with the correct values selected.

Version history
Last update:
18m ago
Updated by:
Contributors