Show only Approved and Rejected in the Approval State

mfhaciahmetoglu
Mega Sage

Hi guys,

 

I have a requirement to show only Approved and Rejected in the State field of sysapproval_approver table. 

 

So here, when the approver opens the form, he or she needs to see only the "Approved" and "Rejected" (and perhaps "Requested" as well). And this should be applied only to a specific catalog item.

 

Any ideas?

 

Thanks.

2 ACCEPTED SOLUTIONS

Tai Vu
Kilo Patron
Kilo Patron

Hi @mfhaciahmetoglu 

Let's try the approach below to hide Approval States on a specific Catalog Item.

1. Define an OnLoad Client Script

2. Call an Ajax script include to validate the catalog item by passing the associated RITM as param.

Sample below.

function onLoad() {
    if (g_form.getValue('source_table') !== 'sc_req_item') {
        return;
    }
	var ga = new GlideAjax('CLIncidentUtilsAJAX'); //your client callable script include
	ga.addParam('sysparm_name', 'checkCatalogItem');
	ga.addParam('sysparm_ritm_id', g_form.getValue('document_id'));
	ga.getXMLAnswer(function(answer){
		if(answer === 'true'){
			g_form.removeOption('state', 'not requested');
			g_form.removeOption('state', 'cancelled');
			g_form.removeOption('state', 'not_required');
			g_form.removeOption('state', 'more_info_required');
			g_form.removeOption('state', 'duplicate');
		}
	});
}

 

3. Create an Ajax script include with the function named checkCatalogItem

4. Query to get the Requested item, then validate the Catalog Item

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

	checkCatalogItem: function(){
		var grRITM = new GlideRecord('sc_req_item');
		if(grRITM.get(this.getParameter('sysparm_ritm_id'))){
			//you can store the catalog item sys_id in a system property to avoid hard-coding
			return grRITM.getValue('cat_item') === 'e591f18b47eaf990ab9bb6bf016d430b'; //your catalog item sys_id 
		}
		return false;
	},

    type: 'CLIncidentUtilsAJAX'
});

 

Cheers,

Tai Vu

 

View solution in original post

Hi Timi,

 

I found out why the code did not work.

 

I has to do with the very first line indeed.

 

I changed it to the following and everything works fine:

 

if (g_form.getValue('source_table') === 'sc_req_item') {
        alert('hello hello');
        return;
    }
 
As to why, I am not sure. I understood your logic: if the source table is not sc_req_item, then don't run the code -- which is to say, if it is sc_req_item, then follow the rest of the code. But for some reason, source table is sc_req_item, I don't see that alert and code runs fine. I don't get it but it works.
 
Thanks a lot for the help.

View solution in original post

10 REPLIES 10

Tai Vu
Kilo Patron
Kilo Patron

Hi @mfhaciahmetoglu 

Let's try the approach below to hide Approval States on a specific Catalog Item.

1. Define an OnLoad Client Script

2. Call an Ajax script include to validate the catalog item by passing the associated RITM as param.

Sample below.

function onLoad() {
    if (g_form.getValue('source_table') !== 'sc_req_item') {
        return;
    }
	var ga = new GlideAjax('CLIncidentUtilsAJAX'); //your client callable script include
	ga.addParam('sysparm_name', 'checkCatalogItem');
	ga.addParam('sysparm_ritm_id', g_form.getValue('document_id'));
	ga.getXMLAnswer(function(answer){
		if(answer === 'true'){
			g_form.removeOption('state', 'not requested');
			g_form.removeOption('state', 'cancelled');
			g_form.removeOption('state', 'not_required');
			g_form.removeOption('state', 'more_info_required');
			g_form.removeOption('state', 'duplicate');
		}
	});
}

 

3. Create an Ajax script include with the function named checkCatalogItem

4. Query to get the Requested item, then validate the Catalog Item

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

	checkCatalogItem: function(){
		var grRITM = new GlideRecord('sc_req_item');
		if(grRITM.get(this.getParameter('sysparm_ritm_id'))){
			//you can store the catalog item sys_id in a system property to avoid hard-coding
			return grRITM.getValue('cat_item') === 'e591f18b47eaf990ab9bb6bf016d430b'; //your catalog item sys_id 
		}
		return false;
	},

    type: 'CLIncidentUtilsAJAX'
});

 

Cheers,

Tai Vu

 

Hi Timi,

 

Thanks a lot for the detailed reply.

I implemented the solution as follows:

 

mfhaciahmetoglu_0-1705322643470.png

 

And:

mfhaciahmetoglu_1-1705322682911.png

 

But for some reason, it does not work. I added a debug line in the script include and I do not see it when I submitted that relevant request. It seems that the client script does not call the script include. The alert message that I added to client script is not shown either when I submit the request.

 

Any idea why?

 

Thanks!

Best,

Firat

 

Hi @mfhaciahmetoglu 

 

The issue might stem from the incorrect creation of the script include initially, lacking the client callable checkbox. You could attempt to delete it and then recreate it.

Regarding your query about the business rule approach – "why don't we write a BR instead of a client script?"

A business rule won't afford you the capability to show or hide an option of a field. However, you can employ a business rule to initiate a scratchpad variable on display. Subsequently, in your client script, you can use that variable to hide options.

Sample.

#Business Rule

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

	var grRITM = new GlideRecord('sc_req_item');
	if(grRITM.get(current.getValue('document_id'))){
		//you can store the catalog item sys_id in a system property to avoid hard-coding
		g_scratchpad.hideApprovalState = (grRITM.getValue('cat_item') === 'e59c31a0930731900c7d72f48aba106b'); //your catalog item sys_id 
	}

})(current, previous);

 

#Client Script

function onLoad() {
    if (g_form.getValue('source_table') !== 'sc_req_item') {
        return;
    }

    if (g_scratchpad.hideApprovalState === true) {
        g_form.removeOption('state', 'not requested');
        g_form.removeOption('state', 'cancelled');
        g_form.removeOption('state', 'not_required');
        g_form.removeOption('state', 'more_info_required');
        g_form.removeOption('state', 'duplicate');
    }

}

 

Enjoy your result

Timi_0-1705377295514.png

 

Cheers,

Tai Vu

Hi Timi, 

 

Thanks again for this solution.

 

I have really no idea why the first solution does not work. I deleted everything and created again, and gave the ACL of the client callable access rule snc_internal. But nothing works. I put that alert there, so once I load the sysapproval_approval form, that alert should pop up but it does not. So the client script is not running, I think for some reason. 

 

function onLoad() {

    if (g_form.getValue('source_table') !== 'sc_req_item') {

        return;

    }

    var ga = new GlideAjax('XXX');

    alert('hello hello');

 

There is another client script that I had written, which also modifies that field but it is not active. Do you think even though it is not active it would have an impact?

 

-- Concerning the second solution.

 

I create that BR on which table, Approval? And what is the action to trigger that BR? I haven't used any display BR and could not find any information about it?

 

Thanks!

Best,

Firat