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

Try to debug it by keeping logs in Script include and alerts in client script and track where it is failing.


Dave Smith1
ServiceNow Employee
ServiceNow Employee

vemffm wrote:



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 ...


... and you've made the classic newbie developer mistake of presuming a client script is needed here.  



Assignment Rules are created specifically for this purpose, and UI Policies can also achieve what you want (but for setting assignment, Assignment Rules are the best approach)



Generally, client scripts should be the LAST approach.   Have a search through this community, you'll see people advising that requirements are best satisfied through ACLs, Business Rules, Policies - then scripting.   It's safer to use the inbuilt tools, and only try to roll your own when the provided functionality doesn't meet those needs.


Hi Dave,


I'm 100% with you and also tried with Assignement Rules.



But was able to ONLY assign group if assigned to is member of only ONE group of a specific type.


Can assign him always to specific group .. but not only in this case!


So if he is member of 2 incident groups - NO assignment should happen ...



Would be happy if you could advice how to do OOB!


Dave Smith1
ServiceNow Employee
ServiceNow Employee

vemffm wrote:



But was able to ONLY assign group if assigned to is member of only ONE group of a specific type.


Can assign him always to specific group .. but not only in this case!


So if he is member of 2 incident groups - NO assignment should happen ...



Would be happy if you could advice how to do OOB!


It appears you're confused about how assignment works.



Any task (incident, problem, etc) can be assigned to an individual - they then see that task show up in My Work.



However, someone raising (or reviewing) the task may not know whom specifically to assign it, hence the idea of an assignment group.   By specifying a group, members of that group can then see outstanding tasks if they look in My Group Work.   It is recommended that auto-assign rules attempt to select a suitable group, then perhaps identify a specific individual within that group if there is only one selection, but group assignment is safest.



The end objective of assignment is to direct it to someone whom can do the work - the assignment group is simply an intermediate step, throwing the ticket at a whole group and letting those members decide who should take it to work on it.   Consequently, if the assigned_to field is filled, the group field has no meaning.



It sounds like you're trying to do this backwards: once the assigned_to field is filled, you want to go back and retrospectively fill in the group name.



Let's review the purpose of raising tickets: to get work done.   It shouldn't matter who is doing the work, just that someone is doing it - and if that's the wrong person, then it needs to be reallocated to someone else. That's an issue the group needs to address.



The affected customer doesn't care if the group is unfilled, if the right nor wrong person is working on it. They're concerned with outcomes: has it been fixed? Can I continue working?


So if he is member of 2 incident groups - NO assignment should happen ...


Why should this matter?   This suggests to me that anyone whom belongs to two groups can't be auto-assigned a task.   Assignment criteria should be based upon most appropriate skills to handle the work, not upon number of group membership.



Can you honestly imagine walking into a garage repairing your car and the salesman apologetically explaining: "well, Fred would have worked on your brakes but trouble is, he's a wheel mechanic AND he's an emissions tester - so because he's skilled in two areas, we couldn't assign him to work on your car...."


The requirement came direct out of the daily work of the service desk.


There are simply some conditions where auto assignment will not work - i.e. if the incident was auto assigned and the 2nd level figures out that the ticket now needs to be done by someone else.


If he just assigns the group - the group can manage their tickets ... but if during the process it is clear, that mr. X should go ahead working on the ticket, it makes sense to directly assign the ticket to him ... . But the form required a group too ... but which one to assign if he is part of more groups???


So this makes a lot of sense for manual reassignment - not for initial autoassignment ...