JavaScript calls

JavaScript calls.

About this task

An example of a JavaScript call is the following code:
javascript:new myScriptInclude().my_refqual()

This code calls a function named my_refqual() in a script include named myScriptInclude(). The function must return a query string that can filter the options available on a reference field.

JavaScript example: Limiting the Assigned to Field by Users with a Specified Role

This example shows how to restrict an incident's Assigned to choices to only the users with the itil_admin role. You could also change itil_admin to any other role on a reference field that refers to the User table.

Procedure

  1. Open an incident.
  2. Right-click the Assigned to field and select Configure Dictionary.
  3. In the Reference qual field, enter javascript:"sys_idIN"+getRoledUsers("itil_admin").join(",").
  4. Save the record.
  5. To see the base-system business rule that this JavaScript code calls, navigate to System Definitions > Business rules.
  6. Open getRoledUsers.
  7. The business rule uses the following JavaScript code:
    // Return an array of sys_ids of the users that have at least one role
    // optional parameters allow the exclusion (NOT IN) of some roles or// look for specific roles (IN)
    //
    // optional: queryCondition - 'IN' or 'NOT IN'
    // optional: roleList - a comma separated list of role names
    //
    function getRoledUsers(queryCondition, roleList){
       var roleListIds;
       if(queryCondition && roleList){
         roleListIds = getRoleListIds(roleList);}
     
       var users ={};
       var gr =new GlideRecord('sys_user_has_role');
       if(roleListIds){
          gr.addQuery('role', queryCondition, roleListIds);
       }
       gr.query();
       while(gr.next()){
          users[gr.user.toString()]=true;}
     
       var ids =[];
       for(var id in users)
          ids.push(id);
     
       return ids;
    }