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

If a assignment group does not have any user the form is not able to submit/update.

Suyash Joshi
Tera Contributor

Hi Everyone,
I have a small requirement related to assignment group if a user is select any group in assignment group and the group does not have any member so the user cannot submit or update the form but if it has any user then it will submit.
I use client script onSubmit.
My script:-

function onSubmit() {
  var a = g_form.getValue('assignment_group');
  if (a.length == 0) {
    alert("Invalid Insert: The assignment group has no members.");
  } else {
    alert("false");
  }
}
But when I check it. It does not submit record if user is in group as well as user are not.
Every help will be grateful
Thanks and Regards,
Suyash
4 REPLIES 4

Community Alums
Not applicable

You need to use  Script include to find whether the group members are there in the group and based on that you can you can display alert.

Robbie
Kilo Patron
Kilo Patron

Hi @Suyash Joshi,

 

To achieve this, you're going to have to perform a lookup and invoke a server call. This is achieved via Script Include from your Client Script. The good thing is I've supplied the 2 scripts needed below.

 

Please note, you won't be able to achieve this via the onSubmit due to the nature of the lookup so it's best implemented via an onChange event so the users are advised when they are selecting the value. I've handled this in the client script for you with a field message advising the user(s).

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

 

Client Script:

Type: onChange

Field name to action on: Assignment Group

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
 
var grpSysid = g_form.getValue('assignment_group'); //get asignment group sysid
var gaGroupCount = new GlideAjax('global.CheckGroupCount'); //script include name
gaGroupCount.addParam('sysparm_name', 'getGroupCount'); //function name
gaGroupCount.addParam('sysparm_name_sysid', grpSysid); //passing sysid to server
gaGroupCount.getXMLAnswer(getGroupCount);
function getGroupCount(response) {
if (response < 1) {
g_form.setValue('assignment_group', '');
g_form.showFieldMsg('assignment_group', 'The group selected has no group members associated, please select another group', 'info');
}
}
}
 
Script Include:
 Make sure you check the 'Client callable' checkbox.
 
var CheckGroupCount = Class.create();
CheckGroupCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupCount: function() {
var groupSysid = this.getParameter('sysparm_name_sysid'); //getting groupsysid from client
var groupCountGA = new GlideAggregate('sys_user_grmember');
groupCountGA.addQuery('group.sys_id', groupSysid); //filtering on group
groupCountGA.addAggregate('COUNT');
groupCountGA.query();
var count = -1; // default to error state since 0 is a valid count
if (groupCountGA.next()) {
count = groupCountGA.getAggregate('COUNT');
}
return count;
},
type: 'CheckGroupCount'
});

Robbie
Kilo Patron
Kilo Patron

Hi @Suyash Joshi,

 

Thanks for marking my response as helpful. Please let me know what else you would need to implement a solution so I can help close this out. The scripts provided in my previous post have been written so you can simply copy and paste.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

Hi Robbie, 

 

I have implement your code in my instance to Case table. Unfortunately, its not working for some reason. Anything  i need to enable or check further?