Disabling order now button for the agent

is_12
Tera Contributor
Hello Community !..
 
When agent is ordering the item from configurable workspace then write a logic as follows. On change of primary contact, fetch the primary contact's account and then go to RITM table and check for the catalog item and the account fetched from primary contact. If there is an existing record then restrict the agent from ordering and also display the alert message "You are not allowed to order as there is an existing request for this customer"
 
already there is a UC for controlling the visibility for the items for customer.
 
Thanks,
 
 
2 ACCEPTED SOLUTIONS

SumanthDosapati
Mega Sage
Mega Sage

@is_12 

Try below approach.

  1. Write an onChange client script on primary contact as below"
    function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue == '') {
            return;
        }
    
        // Call GlideAjax to check existing RITMs
        var ga = new GlideAjax('CheckUserRITMs');
        ga.addParam('sysparm_name', 'hasExistingRITM');
        ga.addParam('sysparm_user_id', newValue);
        ga.addParam('sysparm_catalog_item', g_form.getUniqueValue()); // catalog item sys_id
        ga.getXMLAnswer(function(response) {
            if (response == 'true') {
                g_form.showFieldMsg('primary_contact', 'This user has already requested this item.', 'error'); //check field name
            } else {
                g_form.hideFieldMsg('primary_contact', true); //check field name
            }
        });
    }
    ​
  2. Write a New Client callable script include as below
    Name: CheckUserRITMs
    Accessible from: All application scopes
    Client Callable: True
    var CheckUserRITMs = Class.create();
    CheckUserRITMs.prototype = Object.extendsObject(AbstractAjaxProcessor, {
        
        hasExistingRITM: function() {
            var userId = this.getParameter('sysparm_user_id');
            var catalogItemId = this.getParameter('sysparm_catalog_item');
    
            if (!userId || !catalogItemId) {
                return 'false';
            }
    
            var ritmGR = new GlideRecord('sc_req_item');
            ritmGR.addQuery('cat_item', catalogItemId);
            ritmGR.addQuery('u_primary_contact', userId); // replace with actual field name if different
            ritmGR.query();
    
            if (ritmGR.hasNext()) {
                return 'true';
            }
    
            return 'false';
        }
    
    });
    

This will show an error message at the field level and also user cannot submit even if they click the order button.

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

View solution in original post

@is_12 

it seems script include is not getting called and hence the 3 parameters are coming as empty

a) account variable refers to which table?

b) the onChange is running on which table? that variable refers to which table?

Client Script:

1) pass the catalog item sysId as hard-coded value

    function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue == '') {
            return;
        }
        if (g_user.hasRole('agent_workspace_user')) {
            alert('welcome');
            var ga = new GlideAjax('BT_CSR_SSR_CatalogUtils');
            ga.addParam('sysparm_name', 'hasExistingRITM');
            ga.addParam('sysparm_user_id', newValue);
            ga.addParam('sysparm_account', g_form.getValue('account'));
            ga.addParam('sysparm_catalog_item', 'catalogItemSysId'); // catalog item sys_id
            ga.getXMLAnswer(function(response) {
                alert(response);
                if (response == 'true') {
                    alert(true);
                    g_form.showFieldMsg('contact_name', 'This user has already requested this item.', 'error'); //check field name
                } else {
                    alert(false);
                    g_form.showFieldMsg('contact_name', 'This user has already nottttt requested this item.', 'error'); //check field name
                }
            });
        }
    }

Script Include: it should be client callable

1) pass the correct sysparm name

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

    hasExistingRITM: function() {
        var checkingRITM;
        var userId = this.getParameter('sysparm_user_id');
        var catalogItemId = this.getParameter('sysparm_catalog_item');
        var userAccount = this.getParameter('sysparm_account');

        gs.info('agentIsh279 ' + userId);
        gs.info('agentIsh280 ' + catalogItemId);
        gs.info('agentIsh281 ' + userAccount);

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addEncodedQuery('cat_item.sys_id=' + catalogItemId + '^company.sys_id=' + userAccount + '^requested_for.sys_id=' + userId);
        ritmGR.setLimit(1);
        ritmGR.query();
        gs.info('agentIsh292 ' + ritmGR.getRowCount());
        return ritmGR.hasNext().toString();
    },

    type: 'BT_CSR_SSR_CatalogUtils'
});

AnkurBawiskar_0-1747144069548.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

31 REPLIES 31

@is_12 

Is this line not printed ?

gs.info('agentIsh291');

@SumanthDosapati it is not coming up

hasExistingRITM: function() {
        var checkingRITM;
        var userId = this.getParameter('sysparm_userId');
        var catalogItemId = this.getParameter('sysparm_catalogItem');
        var userAccount = this.getParameter('sysparm_account');

        gs.info('agentIsh279 ' + userId);
        gs.info('agentIsh280 ' + catalogItemId);
        gs.info('agentIsh281 ' + userAccount);

        // var accountGR = new GlideRecord('core_company');
        // if (accountGR.get(userAccount)) {
        //     var companySysId = accountGR.sys_id;
        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addEncodedQuery('cat_item.sys_id=' + catalogItemId + '^company.sys_id=' + userAccount + '^requested_for.sys_id=' + userId);
        //ritmGR.addQuery('company', userAccount);
        //ritmGR.addQuery('cat_item', catalogItemId);
        //ritmGR.addQuery('requested_for', userId);
        ritmGR.queryNoDomain();
        ritmGR.query();
        //gs.info('agentIsh292 ' +ritmGR.getRowCount());
        if (ritmGR.next()) {
            gs.info('agentIsh291');
            checkingRITM = true;
            return checkingRITM;
        } else {
            checkingRITM = false;
            gs.info('agentIsh297');
            return checkingRITM;
        }
    },
is_12_0-1747143238558.png

ritm_record is there, then also it is returning false, instead it should true

is_12_1-1747143313675.pngis_12_2-1747143457996.png

 

@is_12 

it seems script include is not getting called and hence the 3 parameters are coming as empty

a) account variable refers to which table?

b) the onChange is running on which table? that variable refers to which table?

Client Script:

1) pass the catalog item sysId as hard-coded value

    function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue == '') {
            return;
        }
        if (g_user.hasRole('agent_workspace_user')) {
            alert('welcome');
            var ga = new GlideAjax('BT_CSR_SSR_CatalogUtils');
            ga.addParam('sysparm_name', 'hasExistingRITM');
            ga.addParam('sysparm_user_id', newValue);
            ga.addParam('sysparm_account', g_form.getValue('account'));
            ga.addParam('sysparm_catalog_item', 'catalogItemSysId'); // catalog item sys_id
            ga.getXMLAnswer(function(response) {
                alert(response);
                if (response == 'true') {
                    alert(true);
                    g_form.showFieldMsg('contact_name', 'This user has already requested this item.', 'error'); //check field name
                } else {
                    alert(false);
                    g_form.showFieldMsg('contact_name', 'This user has already nottttt requested this item.', 'error'); //check field name
                }
            });
        }
    }

Script Include: it should be client callable

1) pass the correct sysparm name

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

    hasExistingRITM: function() {
        var checkingRITM;
        var userId = this.getParameter('sysparm_user_id');
        var catalogItemId = this.getParameter('sysparm_catalog_item');
        var userAccount = this.getParameter('sysparm_account');

        gs.info('agentIsh279 ' + userId);
        gs.info('agentIsh280 ' + catalogItemId);
        gs.info('agentIsh281 ' + userAccount);

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addEncodedQuery('cat_item.sys_id=' + catalogItemId + '^company.sys_id=' + userAccount + '^requested_for.sys_id=' + userId);
        ritmGR.setLimit(1);
        ritmGR.query();
        gs.info('agentIsh292 ' + ritmGR.getRowCount());
        return ritmGR.hasNext().toString();
    },

    type: 'BT_CSR_SSR_CatalogUtils'
});

AnkurBawiskar_0-1747144069548.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Now values of account, cat_item, account  are coming up in the logs and even in the catalog script I have changed the sysparm name's in the client script also. 

    if (g_user.hasRole('agent_workspace_user')) {
        //var accountsysid = g_form.getValue('account');
        //var primaryContact = g_form.getValue('contact_name');

        var ga = new GlideAjax('BT_CSR_SSR_CatalogUtils');
        ga.addParam('sysparm_name', 'hasExistingRITM');
        ga.addParam('sysparm_userId', g_form.getValue('account'));
        ga.addParam('sysparm_account', g_form.getValue('contact_name'));
        ga.addParam('sysparm_catalogItem', g_form.getUniqueValue());

        // alert('accountsysid' +accountsysid);
        // alert('primaryContact' +primaryContact);
        // alert('catalog sys id'+g_form.getUniqueValue());
        ga.getXMLAnswer(function(response) {
            alert(response);
            if (response == true) {
                alert(true);

                g_form.showFieldMsg('contact_name', 'This user has already requested this item.', 'error');
            } else {
                alert(false);
                g_form.showFieldMsg('contact_name', 'This user has no item.', 'error');
            }
        });
    }
Thanks
 

 

@is_12 

you didn't provide answers to my earlier question

what debugging did you do next?

a) account variable refers to which table?

b) the onChange is running on which table? that variable refers to which table?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader