- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2022 01:56 PM
Hello,
I currently have a requirement where if a catalog item is submitted, a script needs to run to test if a selected user is already in another group. If the user is not in the group, the submit should fail and an error message should appear. I tried to configure it using a glide record, but I understand that is not best practice and am unsure how to configure it as a script include.
Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2022 09:34 PM
do the below onSubmit() script if you wanna try static
function onSubmit(){
if(!g_user.isMemberOf('group name')){ //If logged-in user is not member of the group
g_form.addErrorMessage('Member is not part of the group');
return false;
}
return true;
If you wanna try dynamic do the below approach with GlideAjax()
onSubmit() Client Script
function onSubmit() {
if (!g_form.ajaxComplete) {
ajaxCall();
return false;
}
}
function ajaxCall() {
var ga = new GlideAjax('script_include');
ga.addParam("sysparm_name", 'function_name');
getXMLAnswer(function(a){
// Do whatever you want to with the answer from script include
// Once you've handle the answer, run these two lines:
g_form.ajaxComplete = true;
g_form.submit();
});
}
Script Include
//Script include function
function_name : function(){
var checkUserGroup = new GlideRecord('sys_user_grmember');
//Do your logic
return 'your response';
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2022 09:34 PM
do the below onSubmit() script if you wanna try static
function onSubmit(){
if(!g_user.isMemberOf('group name')){ //If logged-in user is not member of the group
g_form.addErrorMessage('Member is not part of the group');
return false;
}
return true;
If you wanna try dynamic do the below approach with GlideAjax()
onSubmit() Client Script
function onSubmit() {
if (!g_form.ajaxComplete) {
ajaxCall();
return false;
}
}
function ajaxCall() {
var ga = new GlideAjax('script_include');
ga.addParam("sysparm_name", 'function_name');
getXMLAnswer(function(a){
// Do whatever you want to with the answer from script include
// Once you've handle the answer, run these two lines:
g_form.ajaxComplete = true;
g_form.submit();
});
}
Script Include
//Script include function
function_name : function(){
var checkUserGroup = new GlideRecord('sys_user_grmember');
//Do your logic
return 'your response';
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2022 02:19 PM
The reply marked as correct works pretty well from Service Portal but not so well from Service Catalog. There it spits out errors about g_form.submit() being useless in a client script. I found the following works well from Service Catalog:
g_form.clearMessages();
var answer = '';
var gajax = new GlideAjax("[Your Script Include]");
gajax.addParam("sysparm_name", "[Function within script include]");
gajax.addParam("[Optional other parameter to pass]",[Value to pass]);
gajax.getXMLWait(); // I know; latency
answer = gajax.getAnswer();
if (answer [Your Condition]) {
g_form.addErrorMessage("[Your Error Message if needed]");
}
if (answer [Your Condition])
return false;
return true;