Prevent duplicate requests from being submitted

Nicholas Omoruy
Tera Contributor

I want to avoid end users creating duplicate requests if they've already made a submission by displaying a message that informs them of the duplicate request. To achieve this, I tried replicating a Catalog Client Script + Ajax example I found on this forum but to no avail.

 

The Unique value that I'm using is the field 'computer'. It's a list collector thats referencing a hardware table. Nothing appears to happen when moving a previously submitted item in the list collector from the Available section to the Selected section.

 

Any assistance will be extremely appreciated!

 

 

onChange Catalog Client Script for the Computer field

 

function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading)
              return;
      g_form.hideFieldMsg('computer', true);
      if (newValue == '')
              return;
      var ga = new GlideAjax('AjaxFunctions');
      ga.addParam('sysparm_name', 'checkUserRequest');
      ga.addParam('sysparm_user', newValue); // User
      ga.addParam('sysparm_item', g_form.getUniqueValue()); // Item
      ga.getXMLAnswer(function(answer) {
              if (answer && answer != 'none')
                      var msg = 'The user already has an open Request(s) for this item: ' + answer;
                      g_form.showFieldMsg('computer', msg, 'error');
              }
      });
}
 
 
Script Include:
Name: AjaxFunctions
Client-callable: true
 
var AjaxFunctions = Class.create();
AjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkUserRequest: function() {
          var user = this.getParameter('sysparm_user');
          var item = this.getParameter('sysparm_item');
          var requests = [];
          var ritm = new GlideRecord('sc_req_item');
          ritm.addActiveQuery();
          ritm.addQuery('request.requested_for', user);
          ritm.addQuery('cat_item', item);
          ritm.query();
          while (ritm.next()) {
                  requests.push(String(ritm.number));
          }
          if (requests.length > 0)
                  return requests.join(', ');
          return 'none';
  },
    type: 'AjaxFunctions'
});
 
7 REPLIES 7

James Chun
Kilo Patron

Hey @Nicholas Omoruy,

 

A few things that need to be modified:

  • Missing a '{' in the client script.
function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading)
              return;
      g_form.hideFieldMsg('computer', true);
      if (newValue == '')
              return;
      var ga = new GlideAjax('AjaxFunctions');
      ga.addParam('sysparm_name', 'checkUserRequest');
      ga.addParam('sysparm_user', newValue); // User
      ga.addParam('sysparm_item', g_form.getUniqueValue()); // Item
      ga.getXMLAnswer(function(answer) {
              if (answer && answer != 'none'){ // <----- missing here
                      var msg = 'The user already has an open Request(s) for this item: ' + answer;
                      g_form.showFieldMsg('computer', msg, 'error');
              }
      });
}​

 

  • If the onChange is on the 'Computer' variable, why are you passing its newValue as the user sys_id?

Fix the above and see what happens, cheers

Thanks for the reply, James. I got the message to display but need to query the sc_item_option_mtom as it has the information that I need and I'm having trouble formatting my filter. The only records I need to retrieve is the duplicate values entered into the question variable called Affected Computer(s) or Server(s) and the requestor that raised the request. Once I have the records, I want to display the RITMS in the alert if the newValue has already been requested.

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading)
        return;

    g_form.hideFieldMsg('computer', true);

    if (newValue == '')
        return;
    var ga = new GlideAjax('AjaxFunctions');
    ga.addParam('sysparm_name', 'checkUserRequest');
    ga.addParam('sysparm_computer', newValue); // Computer
    ga.addParam('sysparm_request_item.short_description', g_form.getDisplayBox('short_discription').value); // Item or Short Description
    ga.addParam('sysparm_request_item.request.requested_for', g_form.getDisplayBox('requested_for').value); // User
    ga.getXMLAnswer(function(answer) {

        if (answer && answer != 'none') {
            var msg = 'The Requestor already has an open Request(s) for this item: ' + answer;
            alert(msg);
            g_form.clearValue('computer');
        }

    });
}
var AjaxFunctions = Class.create();
AjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkUserRequest: function() {
        var computer = this.getParameter('sysparm_computer');
        var desc = this.getParameter('sysparm_request_item.short_description');
        var user = this.getParameter('sysparm_request_item.request.requested_for');

        var requests = [];
        var ritm = new GlideRecord('sc_item_option_mtom');
        ritm.addActiveQuery();
        ritm.addQuery('sc_item_option.item_option_new.name','question_text');
		ritm.addQuery('sc_item_option.value', 'Affected Computer(s) or Server(s)');
        ritm.addQuery('request_item.request.requested_for', user);
        ritm.addQuery('sc_item_option.request_item.short_description', desc);
        ritm.query();
        while (ritm.next()) {
            requests.push(String(ritm.number));
        }
        if (requests.length > 0)
            return requests.join(', ');
        return 'none';
    },
    type: 'AjaxFunctions'
});

 

Hey @Nicholas Omoruy,

 

Made some changes, see below:

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading)
        return;

    g_form.hideFieldMsg('computer', true);

    if (newValue == '')
        return;
    var ga = new GlideAjax('AjaxFunctions');
    ga.addParam('sysparm_name', 'checkUserRequest');
    ga.addParam('sysparm_computer', newValue); // Computer
    ga.addParam('sysparm_short_description', g_form.getValue('short_description')); //Not sure if the typo here is intentional (i.e. short_discription -> short_description)
    ga.addParam('sysparm_requested_for', g_form.getValue('requested_for')); // User
    ga.getXMLAnswer(function(answer) {

        if (answer && answer != 'none') {
            var msg = 'The Requestor already has an open Request(s) for this item: ' + answer;
            alert(msg);
            g_form.clearValue('computer');
        }

    });
}

 

Script Include:

var AjaxFunctions = Class.create();
AjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkUserRequest: function() {
        var computer = this.getParameter('sysparm_computer');
        var desc = this.getParameter('sysparm_short_description');
        var user = this.getParameter('sysparm_requested_for');

        var requests = [];
        
		var ritm = new GlideRecord('sc_item_option_mtom');
		ritm.addQuery('request_item.active', true);
		ritm.addQuery('request_item.request.requested_for', user);

		//------Unsure about this section
        ritm.addQuery('sc_item_option.item_option_new.name', 'question_text'); 
        ritm.addQuery('sc_item_option.value', 'Affected Computer(s) or Server(s)');
        ritm.addQuery('sc_item_option.request_item.short_description', desc);
        ritm.query();
		//-------

        while (ritm.next()) {
            requests.push(ritm.request_item.number.toString());
        }

        if (requests.length > 0)
            return requests.join(', ');
        return 'none';
    },
    type: 'AjaxFunctions'
});

 

In regards to the Script Include, I wasn't sure about the query logic and the data structure, so I left it as it was.

What is the variable type of 'Affected Computer(s) or Server(s)' and what is the purpose of querying the 'short description' as well? I would assume you would have to query with the following conditions:

  • Currently active Request/RITM,
  • Same requested for, and
  • Same computer/server

If you can clarify the above, I can modify the query script as well.

 

Cheers

'Affected Computer(s) or Server(s)' is a List Collector that references our Hardware table and the short description contains the name of the catalog item. So, the conditions will be: 

  • Currently active Request/RITM,
  • Same requested for, and
  • Same computer/server
  • Short description i.e. Catalog Item