Pop Notification

PK14
Kilo Guru

Hi SN experts, 

I am writing an catalog client script to restrict user to not submit the ticket (on catalog item) if they are part of ServiceDesk assignment group but they can submit the request for others. 

 

Following is the script include : 

 

var CheckAssignmentGroup = Class.create();
CheckAssignmentGroup.prototype = {
initialize: function() {},
type: 'CheckAssignmentGroup'
};
CheckAssignmentGroup.isUserInGroup = function(groupName) {
var user = gs.getUser();
return user.isMemberOf(groupName);
};

 

and client script is 
function onSubmit() {
var groupName = "ServiceDeskDL"; 
var ga = new GlideAjax('CheckAssignmentGroup');
ga.addParam('sysparm_name', 'isUserInGroup');
ga.addParam('sysparm_groupName', groupName);
ga.getXMLAnswer(function(response) {
if (response == 'true') {
alert("You are already a member of the ServiceDesk DL assignment group. Cannot submit the request.");
return false; // Prevent form submission
} else {
return true; // Allow form submission
}
});
}

Can you please help me on this or assist me the right path. 

Regards,

Priyanka

9 REPLIES 9

SANDEEP28
Mega Sage

@PK14 Have you tried my script include code.

 

Script include -

 

var CheckAssignmentGroup = Class.create();
CheckAssignmentGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isUserInGroup: function() {
        var groupName = this.getParameter('sysparm_groupName');
        if (groupName)
            return gs.getUser().isMemberOf(groupName);
        else
            return false;
    },

    type: 'CheckAssignmentGroup'
});

 

Client script

 

function onSubmit() {

var groupName = "Service Desk";  //put your service desk group
var ga = new GlideAjax('CheckAssignmentGroup');
ga.addParam('sysparm_name', 'isUserInGroup');
ga.addParam('sysparm_groupName', groupName);
ga.getXMLAnswer(function(response) {
if (response == 'true') {
g_form.addErrorMessage('You are already a member of the ServiceDesk DL assignment group. Cannot submit the request.');
return false;
 // Prevent form submission
} else {
return true; // Allow form submission
}
});
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Utpal Dutta
Tera Guru

Hi there,

Your onSubmit client script is not working because when you do Ajax call to the server it is Async. So when you submit the request and your Client Script triggers it gets response from the script include asynchronously not synchronously & that's why submission happens and the response gets to the client script after submission.

 

To achieve this you can create a True/False type field on Catalog item and keep it hidden so it doesn't show up on catalog item and then you can run an onLoad client script with the same code that you have in onSubmit client script. In script when you have checked that the user belongs to ServiceDesk assignment group make the True/False field of the catalog item True or False accordingly & then check this True or False field in your onSubmit client script that if it's false then abort submission if not the let the submit happen.

 

If my answer helps then please mark it Helpful or Correct. If you need more help then please let me know.

 

Thanks,

Utpal

@PK14 Please try my solution

SANDEEP28
Mega Sage

@PK14 Try below client script.  I tested this my PDI and it working.

 

Also refer explanation given in below links

https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta...

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

 

function onSubmit() {
    if (g_scratchpad.isFormValid)
        return true;
    var groupName = "ServiceDeskDL";
    var ga = new GlideAjax('CheckAssignmentGroup');
    ga.addParam('sysparm_name', 'isUserInGroup');
    ga.addParam('sysparm_groupName', groupName);
    ga.getXMLAnswer(setAnswer);
    return false;


    function setAnswer(answer) {
        if (answer == 'true') {
            g_form.addErrorMessage('You are already a member of the ServiceDesk DL assignment group. Cannot submit the request.');
            return false;
        }
        var actionName = g_form.getActionName();
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);

    }
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Hi
g_scratchpad is not defined? This is the error I receive.