UI action for adding members to group

Sanket Pawar
Tera Contributor

I have a requirement for creating a UI action which should add users to the groups mentioned in the orignal description of the sc_task.

how can we achive this functionallity?

1 REPLY 1

Amit Gujarathi
Giga Sage
Giga Sage

HI @Sanket Pawar ,
I trust you are doing great.
In the "Onclick" field, write the script to add users to the groups mentioned in the sc_task's original description. Below is the sample code:

function addUsersToGroups() {
  var taskId = g_form.getUniqueValue(); // Get the unique ID of the current sc_task
  var taskRecord = new GlideRecord('sc_task');
  if (taskRecord.get(taskId)) {
    var description = taskRecord.getValue('description'); // Get the original description of the sc_task
    var groups = extractGroupsFromDescription(description);
    
    // Add users to each group mentioned in the description
    groups.forEach(function(groupName) {
      var groupRecord = new GlideRecord('sys_user_group');
      if (groupRecord.get('name', groupName)) {
        groupRecord.addMember(g_user.userID); // Add the current user to the group
        // You can also add specific users to the group using groupRecord.addMember(userSysID);
      }
    });
  }
}

function extractGroupsFromDescription(description) {
  // Implement your logic here to extract group names from the description.
  // You can use regular expressions or any custom parsing mechanism.
  // For example:
  // var regex = /Group: (\w+)/g;
  // var groups = description.match(regex);
  // return groups;
  // Note: The above example uses a simple regular expression to extract group names.
}

addUsersToGroups(); // Call the function when the UI action is clicked.

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi