Affected CI Filter via Script Include

mmmgawa
Kilo Expert

Hello my fellow developers!

I have a requirement to filter the list that you get using the Affected CI related list edit button. I have a script include that works perfectly in filtering the list or the slush bucket items when manually entered like you see below. The condition is javascript:GetRelCis('sys_id of a CI') and it basically walks a straight line up and down the relationship tree and returns related objects. (similar to a flattened dependency view).

find_real_file.pngfind_real_file.png

The problem is that this script takes a CI sys_id as a parameter and I would like the default filter to use the CI sys_id on the incident or change as this parameter.

I can put the script includes call with a hard coded parameter into the Default filter field for the related list and have it work perfectly, but I can't for the life of me figure out how to pass in the sys_id of the current tasks selected CI. I have looked into trying to use an on load client script for this form but again I can't seem to find how to grab the task.cmdb_ci from the originating form. a dynamic filter option is not available on sys_id and I am not sure that would work for this scenario. Please help!

1 ACCEPTED SOLUTION

mmmgawa
Kilo Expert

Thank you to everyone that replied. As is often the case there are many ways to accomplish the same goal and while I was able to get a few of these suggestions up and going I ultimately went another direction that I came up with on my own. I am not sure how this will work long term but let me document what I did.



So there is a related list that is on the tasks for Affected CI and you can edit the list control on this related list. In the list controls if you set a default filter then that filter will be applied by default when you hit the edit button. The best thing about this is that you can clear out the filter and get all CI's in the list or provide your own filter. The other great thing is the list controls are in a table sys_ui_list_control.


What I did at first was to create an on display business rule that looked up the appropriate list control and then I could programmitically set the edit_default_filter property and give it the filter that I wanted by forming a string



var rel_list = new GlideRecord('sys_ui_list_control');


rel_list.addQuery('name', current.getTableName()); //Get the related lists controlls for this table


rel_list.addQuery('related_list', 'task_ci.task'); //Get the related list control for this list


rel_list.query();


if(rel_list.next()){


if(current.cmdb_ci == ''){


rel_list.edit_default_filter = '';


rel_list.update();


}


else{


var filter = 'sys_id=javascript:GetRelCis(\"' + current.cmdb_ci.toString() + '\")^sys_class_name=cmdb_ci_service^ORsys_class_name=u_cmdb_service_application^ORsys_class_name=u_cmdb_service_environment^ORsys_class_nameINSTANCEOFcmdb_ci_server';


rel_list.edit_default_filter = filter;


rel_list.update();


}


}


Ultimately I decided that setting the default filter on the list control when the page loaded would likely not be good because who knows how many people are going to be looking at how many incidents at the same time. I moved the code into a script includes and altered the "edit" button UI action to set the filter based on the context when the button is pressed. There is still an opportunity for the filter to be incorrect when the slush bucket opens but the window for the filter to be overwritten is very small.


View solution in original post

10 REPLIES 10

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Scott,



I think your field is dependent on some other variable in that case you can create a onchange client script and in that script you can reset the filter of the list. Please refer the sample client script below.



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


if (isLoading || newValue == '') {


return;


}


var ga = new GlideAjax('Refresh_list');


ga.addParam('sysparm_name','getUser');


ga.addParam('sysparm_group',newValue);


ga.getXMLWait();



var filterString=ga.getAnswer();


setMyFilter(filterString);



function setMyFilter(filterString) {



usrg_filter.reset();//Replace"usr"   here with the list collector name on catalog item


usrg_filter.setQuery(filterString);//Replace"usr" here with the list collector name on catalog item


usracRequest(null);//Replace"usr" here with the list collector name on catalog item


}




}




-----------------Script include-------------------



var Refresh_list = Class.create();


Refresh_list.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getUser: function()


{


var uarr=[];


if (this.getParameter('sysparm_group').nil)


{


var grm=new GlideRecord('sys_user_grmember');


grm.query();


while(grm.next())


{


if (uarr.length > 0)


{


uarr += (',' + grm.user);


}


else


{


uarr = grm.user+'';


}



}



}


else


{


var gr=new GlideRecord('sys_user_grmember');


gr.addQuery('group',this.getParameter('sysparm_group'));


gr.query();


while(gr.next())


{


if (uarr.length > 0)


{


uarr += (',' + gr.user);


}


else


{


uarr = gr.user+'';


}



}



var filterString = 'sys_idIN'+uarr;


return filterString;


},


type: 'Refresh_list'


});



Hope this helps.



Regards


Ujjawal


sifynm
Tera Expert

Scott Sumida,



                Here is a link I found which may help you Configuration Item tied to Computers Assigned To .


mmmgawa
Kilo Expert

Thank you very much for your responses Ujjawal and Nandan. I have tried to adapt the suggestions that you have made to the related list "edit" button features but have not been successful as of yet. I will keep trying and let you know if I can get it to work.


Dear Mr. Scott Sumida,



                  I am interested to know how you solved it. Thank you.