Modal triggered from case in workspace

Mathias Vandend
Mega Sage

Hi,

for our customer we have the requirment to filter catalog items based on the contact. When using the ootb 'Creat Request' button we are being redirecte to the swp portal in the workspace where the agent can see all active catalog items. 
There is no way to filter the catalog items based on the contact on the customer case.

My idea was to create a ui actioin that opens a model that has the contact prefilled. A field that references to the catalog items. I have a script that returns all catalog item sys_id's the user has access to. I would like to use this to filter the catalog item reference field on the modal. 
When the catalog item is selected, I would then want to re-use the code '

g_service_catalog.openCatalogItem('sc_cat_item', '<catalog item id>', params);' to open a new tab to fill in the request. By using this the new request should link automatically to the customer case.
the modal should also load these params from the case
params.sysparm_parent_table = "sn_customerservice_case";
params.sysparm_parent_sys_id = g_form.getUniqueValue();
As my knowledge is very limited to create such modals I would love to hear how to build this
1 ACCEPTED SOLUTION

Mathias Vandend
Mega Sage

I've found a way to achieve this.
I've created a new ui action to open a modal. This modal show a catalog item choice field. The field is populated with the catalog items the contact or internal user has access to. With the g_service_catalog.openCatalogItem it opens the catalog item in a new tab on workspace.

 

Ui action

 

function onClick(g_form) {
	var userId;
	if(g_form.getValue('contact')){
		userId = g_form.getValue('contact');
	}
	else if(g_form.getValue('internal_user')){
		userId = g_form.getValue('internal_user');
	}
    var params = {};
    params.sysparm_parent_table = "sn_customerservice_case";
    params.sysparm_parent_sys_id = g_form.getUniqueValue();
    var ga = new GlideAjax('global.CatalogUtilsAjax');
    ga.addParam('sysparm_name', 'getCatalogItemsByUserId');
    ga.addParam('sysparm_user_name', userId); 
    ga.getXMLAnswer(itemParse);

    function itemParse(response) {
        var itemChoice = JSON.parse(response);
        var fields = [
            {
                type: 'choice',
                name: 'selectItem',
                label: getMessage('Select the catalog item you want to request'),
                choices: itemChoice,
                mandatory: true
            },
        ];
        g_modal.showFields({
            title: 'Create Request',
            fields: fields,
            size: 'lg'
        }).then(function(fieldValues) {
            g_service_catalog.openCatalogItem('sc_cat_item', fieldValues.updatedFields[0].value, params);
        });
    }
}

 

 

GlideAjax

 

getCatalogItemsByUserId: function(){
			var user_id = this.getParameter("sysparm_user_name");
			var items = new global.CatalogUtils().getCatalogIdsByUserId(String(user_id));
			return JSON.stringify(items);
		},

 

 

Script include

 

getCatalogIdsByUserId: function(userId) {
        //Get user's user criteria's
        var getCriterias = new sn_uc.UserCriteriaLoader.getUserCriteria(userId, 'sc_cat_item_user_criteria_mtom');

        var options = [];
        //filter catalog items available for
        var gr = new GlideRecord("sc_cat_item_user_criteria_mtom");
        gr.addEncodedQuery('user_criteria.sys_idIN' + getCriterias + '^sc_cat_item.active=true');
        gr.query();
        while (gr.next()) {
            if (gr.sc_cat_item.getDisplayValue())
                options.push({
                    'value': String(gr.sc_cat_item.sys_id),
                    'displayValue': gr.sc_cat_item.getDisplayValue()
                });
        }
        //sort display value alfabetical
        options.sort(function(a, b) {
            if (a.displayValue < b.displayValue) {
                return -1;
            }
            if (a.displayValue > b.displayValue) {
                return 1;
            }
            return 0;
        });
        return options;
    },

 

 

 

View solution in original post

1 REPLY 1

Mathias Vandend
Mega Sage

I've found a way to achieve this.
I've created a new ui action to open a modal. This modal show a catalog item choice field. The field is populated with the catalog items the contact or internal user has access to. With the g_service_catalog.openCatalogItem it opens the catalog item in a new tab on workspace.

 

Ui action

 

function onClick(g_form) {
	var userId;
	if(g_form.getValue('contact')){
		userId = g_form.getValue('contact');
	}
	else if(g_form.getValue('internal_user')){
		userId = g_form.getValue('internal_user');
	}
    var params = {};
    params.sysparm_parent_table = "sn_customerservice_case";
    params.sysparm_parent_sys_id = g_form.getUniqueValue();
    var ga = new GlideAjax('global.CatalogUtilsAjax');
    ga.addParam('sysparm_name', 'getCatalogItemsByUserId');
    ga.addParam('sysparm_user_name', userId); 
    ga.getXMLAnswer(itemParse);

    function itemParse(response) {
        var itemChoice = JSON.parse(response);
        var fields = [
            {
                type: 'choice',
                name: 'selectItem',
                label: getMessage('Select the catalog item you want to request'),
                choices: itemChoice,
                mandatory: true
            },
        ];
        g_modal.showFields({
            title: 'Create Request',
            fields: fields,
            size: 'lg'
        }).then(function(fieldValues) {
            g_service_catalog.openCatalogItem('sc_cat_item', fieldValues.updatedFields[0].value, params);
        });
    }
}

 

 

GlideAjax

 

getCatalogItemsByUserId: function(){
			var user_id = this.getParameter("sysparm_user_name");
			var items = new global.CatalogUtils().getCatalogIdsByUserId(String(user_id));
			return JSON.stringify(items);
		},

 

 

Script include

 

getCatalogIdsByUserId: function(userId) {
        //Get user's user criteria's
        var getCriterias = new sn_uc.UserCriteriaLoader.getUserCriteria(userId, 'sc_cat_item_user_criteria_mtom');

        var options = [];
        //filter catalog items available for
        var gr = new GlideRecord("sc_cat_item_user_criteria_mtom");
        gr.addEncodedQuery('user_criteria.sys_idIN' + getCriterias + '^sc_cat_item.active=true');
        gr.query();
        while (gr.next()) {
            if (gr.sc_cat_item.getDisplayValue())
                options.push({
                    'value': String(gr.sc_cat_item.sys_id),
                    'displayValue': gr.sc_cat_item.getDisplayValue()
                });
        }
        //sort display value alfabetical
        options.sort(function(a, b) {
            if (a.displayValue < b.displayValue) {
                return -1;
            }
            if (a.displayValue > b.displayValue) {
                return 1;
            }
            return 0;
        });
        return options;
    },