The CreatorCon Call for Content is officially open! Get started here.

How to create group via script

SNOW39
Tera Expert

When a catalog form is submitted, how can i create group with the name mentioned in "group name" field.

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi,

Try this script and modify it with your variables or fields.

 

var mygr = new GlideRecord("sys_usr_group");

mygr.initialise();

mygr.name = current.variables.groupName;

mygr.manager = "sysid";. // Manager sysid or current.variables.manager field

mygr.type = "type"; // add type here or sysid

mygr.insert();

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

5 REPLIES 5

Sagar Pagar
Tera Patron

Hi,

Try this script and modify it with your variables or fields.

 

var mygr = new GlideRecord("sys_usr_group");

mygr.initialise();

mygr.name = current.variables.groupName;

mygr.manager = "sysid";. // Manager sysid or current.variables.manager field

mygr.type = "type"; // add type here or sysid

mygr.insert();

 

Thanks,

Sagar Pagar

The world works with ServiceNow

tried with onsubmit and script includ.. its not working

Hi,

 

This script you have to use in workflow run script activity. Or in flow have to try this.

 

Sagar Pagar

The world works with ServiceNow

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi SNOW,

It's not possible to create create a group onSubmit because record must be created on the server-side and onSubmit shouldn't asynchronously call server-side script.

The way to create group is to create a Service Catalog item and Flow Designer flow to create a group.

1. Service Catalog

  1. Create a Service Catalog item with variable "Group name" of type "Single Line Text".
    find_real_file.png

2. Create a Flow Designer flow

  1. From Application Navigator, select "Process Automation" > "Flow Designer"
  2. Select "New" - "Flow"
    find_real_file.png
  3. Enter the name of the Flow and click on the "Submit" button.
    find_real_file.png
  4. Click on "Add a trigger" and select "Service Catalog"
    find_real_file.png
  5. Click on "Add action" and select "Get Catalog Variables".
    find_real_file.png
  6. Drag and drop "Requested Item Record" to "Submitted Request" field and select the name of Service Catalog that was created above. Catalo Variable should display "group_name". Move it from "Available" to "Selected"
    find_real_file.png
  7. Select "Add an action" again and select "Create Record".
    find_real_file.png
  8. Select "Group [sys_user_group]" as table and select "Name" as field. Drag and drop "group_name" under "1 - Get Catalog Variable" as the value of the field.
    find_real_file.png
  9. Click on "Save" button and then on "Activate" button.
    find_real_file.png

 3. Set Flow to execute when Service Catalog is submitted.

  1. Re-open Service Catalog that was created.
  2. Select "Process Engine" tab and select Flow that was create above. Save the Service Catalog.
    find_real_file.png

Execution sample

step 1: enter a group name and submit the form.
find_real_file.png

step 2: Open "Groups" to check that the group is create.

find_real_file.png