Possible to block a user from submitting a cat item if already have one in progress

HenryD
Tera Guru

Hello, trying to find a way to implement if a User has a req_item request in progress that they are not able to submit the same cat item unless the first one is cancelled. Current process is a user submits a cat item after they finish making their order guide selections.  Right now, I have a business rule, but it is not functioning, screenshots are attached. Was wondering if anyone has experience with something like this or something they would suggest? 

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@HenryD I found that syntax in the business rule slightly deviates from a standard business rule syntax. Instead of a previous object, you have gsr and gs object as method arguments.

 

A business rule usually have the following syntax. 

 

(function executeRule(current, previous /*null when async*/) {

//Business rule logic

})(current, previous);

Please keep your logic to check existing opened request in the above code and let me know if the fix works.

 

Also, there is another way to validate if there already an active request opened by the same user,  you can create a client callable script include to check if the user has already an active request and call this script include from an onSubmit client script using GlideAjax. 

 

Here is an example.

onSubmit Client Script.

function onSubmit() {
    var ga = new GlideAjax('CheckCatalogItemRequest');
    ga.addParam('sysparm_name', 'checkExistingRequest');
    ga.addParam('sysparm_user', g_user.userID);
    ga.addParam('sysparm_cat_item', g_form.getUniqueValue());

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer === 'true') {
            alert('You already have an active request for this item. Please cancel the existing request before submitting a new one.');
            // You can't cancel form submission here directly.
            // Workaround: Set a scratchpad value to block submission.
            g_form.submitted = false;
        } else {
            g_form.submit(); // Manually submit the form if allowed
        }
    });

    // Prevent the default submission while waiting for the async response
    return false;
}

 

Script Include:

 

var CheckCatalogItemRequest = Class.create();
CheckCatalogItemRequest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkExistingRequest: function() {
        var userId = this.getParameter('sysparm_user');
        var catItemId = this.getParameter('sysparm_cat_item');

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addQuery('cat_item', catItemId);
        ritmGR.addQuery('requested_for', userId);
        ritmGR.addQuery('state', 'NOT IN', '3,4,6,7'); // 3=Closed Complete, 4=Cancelled, 6=Closed Incomplete, 7=Closed Skipped
        ritmGR.query();
        return ritmGR.hasNext() ? 'true' : 'false';
    }
});

@Sandeep Rajput Yes changing to the regular current, previous made my script work as I wanted to! 

I was wanting this to instead of it being once they submit, if there was a way to do it on the order guide it is linked to before it heads to this cat item

 

You can write a Catalog Client Script that checks if the user has already ordered the same catalog item. The script should run when the user selects or views the catalog item and disable the Submit button based on whether they have an open request for that item.

was thinking of having an OnLoad client script for the order guide, will that work the same way? i want to disable to next button on that order guide if the user already has a request from submitting that cat item from that order guide