Want to restrict submission of catalog request, if request is already raised for the requestor

Community Alums
Not applicable

We want to restrict submission of duplicate catalog request from service portal, if request is already raised for that requestor. 

 

I have written a client script and script include for the same but something is not working. 

 

Client script: 

 

function onSubmit() {
    if (g_form.getValue('what_is_the_order_type') == 'New Phone' || g_form.getValue('what_is_the_order_type') == 'Replacement Phone') {
        var ga = new GlideAjax('Callforrequest');
        ga.addParam('sysparm_name', 'checkUserRequest');
        ga.addParam('sysparm_user', g_form.getValue('request_for')); // User
        ga.addParam('sysparm_item', g_form.getUniqueValue().toString()); // Item
        ga.getXMLAnswer(setAnswer);
        return false;
 
        function setAnswer(answer) {
alert("answer value from the script" + answer);
            if (answer == 'true') {
                g_form.addErrorMessage("Can not submit the request");
                return false;
            } else {
                return true;
            }
        }
    }
}
 
Script include:
 

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

checkUserRequest: function() {
var user = this.getParameter('sysparm_user');
var item = this.getParameter('sysparm_item');
var result = false;
var ritm = new GlideRecord('sc_req_item');
ritm.addActiveQuery();
ritm.addQuery('requested_for', user);
ritm.addEncodedQuery('cat_item=sys_id of catalog item^stateIN-5,1,2');
ritm.query();
if (ritm.next()) {
result = true;
}
return result;
},

type: 'Callforrequest'
});

 

Please help with this. 

Any help would be appreciated. 

 

Thanks in advance !

2 REPLIES 2

SinghBalwant
Kilo Guru

Sure, you can restrict the submission of a catalog request if a request is already raised for the requestor in ServiceNow using the following steps:

  1. Go to the Catalog Items page.
  2. Click on the Edit button for the catalog item that you want to restrict.
  3. In the Script Include section, add the following code:

function checkForDuplicateRequest(requestedFor)

{

var gr = new GlideRecord('sc_req_item');

gr.addEncodedQuery('cat_item=' + this.getForm().getParameter('sysparm_catitem') + '^state NOT IN(3,4,7)^requested_for=' + requestedFor);

gr.query();

return gr.next();

}

  1. In the Validation section, add the following validation rule:

 

if (checkForDuplicateRequest(this.getForm().getValue('requested_for'))) {

  addErrorMessage('A request has already been raised for this user.');

  return false;

}

  1. Save the catalog item.

Once you have completed these steps, users will not be able to submit a new request for the catalog item if a request for the same user is already in a pending or completed state.

Here are some additional things to keep in mind:

  • The script include and validation rule that you add will apply to all requests for the catalog item, regardless of who is submitting the request.
  • If you need to allow certain users to submit multiple requests for the catalog item, you can create a user exception in the script include.
  • You can also use the script include to check for other conditions, such as whether the user has already used their quota for the catalog item.

 

Community Alums
Not applicable

Hi Balwant, 

Thanks for your response. 

But its not working in my case to provide the expected result. 

We want to apply this restriction only for one catalog item on the basis of Requested for.