Client script to set assignment group automatically ... depending on type ?!

Zod
Giga Guru

Again I'm struggling with my poor developer knowhow and would appreciate some help.

I'd like to a client script on change of assigned_to that sets the assignment_group if the group is empty and the assigned_to to only belongs to ONE group with a specific group_type ...

client script.JPG

>>> this is what I get on change of assigned_to ...

onChange script error: ReferenceError: assignment_group is not defined function onChange_incident_assigned_to_4(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue == '') { return; } if (assignment_group == '') { var gr = new GlideRecord("sys_user_grmember"); gr.addQuery('user',assigned_to); gr.addQuery('group.type','CONTAINS', "incident"); gr.query(); if (gr.getRowCount() == '1'); assignment_group = gr.group; } }

To clarify ... a group could belong to more than one type at a time ... and if the user belongs to more than one group with the specific type - nothing should happen.

1 ACCEPTED SOLUTION

Zod
Giga Guru

Final Version working now:



Script Include:


Get1GroupAjax:


Client callable true


var Get1GroupAjax = Class.create();



GetSingleGroupAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


                              getAssignmentGroup: function() {


                                                            var assignedTo = this.getParameter('sysparm_a');


                                                            var typus = "97ff1cecdba462006b2ebc2ffe96xxx"; // incident type


                                                            var aGroup ="";


                                                            var gr = new GlideRecord('sys_user_grmember');


                                                            gr.addQuery('user',assignedTo);


                                                            gr.addQuery('group.type','CONTAINS', typus);


                                                            gr.query();


                                                            if (gr.getRowCount() == '1')   {


                                                                                            gr.next();


                                                                                            aGroup = gr.group;


                                                            }


                              return aGroup;


                              },


                              type: 'Get1GroupAjax'


});




****************************



Client Script: AutoSet1Group


Table Incident


On Change of assigned_to



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


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


                                                            return;


                              }


                              if(newValue != oldValue){


                                                            var ga = new GlideAjax('Get1GroupAjax');


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


                                                            ga.addParam('sysparm_a',g_form.getValue('assigned_to'));


                                                            ga.getXML(HelloWorldParse);


                              }


                              return;


}


function HelloWorldParse(response) {


                              var answer = response.responseXML.documentElement.getAttribute("answer");


                              g_form.setValue('assignment_group',answer);


                              g_form.showFieldMsg('assigned_to', +answer);


}



… still if you can manage to use Assignment Rules … u should … I was not able to get them work in this case …


View solution in original post

35 REPLIES 35

Yes, this is helpful .. but not what I'm asking for.


This we have in place already, but it should automatically pick the group if only one relevant entry is available to pick ...


Can we define assignment rule for multiple groups.. say if the value of assignment group has to change after every specified time.


Hi Shubham,



Assignment rule: yes you can define, you need to use script tab and mention the script there.



find_real_file.png




BUT if you want it to execute on specific time then better use schedule job and mention your script there.


Zod
Giga Guru

Still struggling with this one ...



I understood that I should use an AJAX script include ... to get the group (again - group only to be set, if there is only ONE group that fit to the relevant type - incident in this case) - otherwise nothing should happen).



So my SI looks like this (is client callable):


****



var Get1GroupAjax = Class.create();


Get1GroupAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getAssignmentGroup: function() {


var assignedTo = this.getParameter('sysparm_a');


var typus = "97ff1cecdba462006b2ebc2ffe96xxxx";   // sys_id of incident type


var aGroup ="";


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('user',assigned_to);


gr.addQuery('group.type','CONTAINS', typus);


gr.query();


if (gr.getRowCount() == '1')   {


aGroup = gr.group;


}


return aGroup;


},


type: 'Get1GroupAjax'


});





now the client script (On Change of assigned to looks currently like this ..)


***



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


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


return;


}


if(newValue != oldValue){  


var ga = new GlideAjax('Get1GroupAjax');


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


return ga;


}  


return;


}




I'm sure the client script is not complete/correct ... but as I'm no developer ... this is not so easy to understand/fix for me ... so appreciating you help on this.



In fact the whole requirement should be common best-practice ... as it makes no sense to choose a value from a list, if there is only one valid record available. In fact this should be some standard feature on all fields that should be set to active ... ;-/


Try this one



SI: Make sure you check client callable check box


var Get1GroupAjax = Class.create();


Get1GroupAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getAssignmentGroup: function() {


var assignedTo = this.getParameter('sysparm_a');


var typus = "97ff1cecdba462006b2ebc2ffe96xxxx";   // sys_id of incident type


var aGroup ="";


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('user',assignedTo);


gr.addQuery('group.type','CONTAINS', typus);


gr.query();


if (gr.getRowCount() == '1')   {


aGroup = gr.sys_id;


}


return aGroup;


},


type: 'Get1GroupAjax'


});



CS:


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


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


return;


}


if(newValue != oldValue){


var ga = new GlideAjax('Get1GroupAjax');


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


ga.addParam('sysparm_a',g_form.getValue('assigned_to'));


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    g_form.setValue('assignment_group',answer);


}


}


return;


}