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

Chuck Tomasi
Tera Patron

Take a look at using Assignment Rules for this. No scripting necessary. It's all data driven and you define the rules.



Define assignment rules


Hi Chuck,


I'd love to use OOB function without scripting ... but unfortunately this will not work in our case. At least as far I was looking into it.


I ONLY assign the group, if assigned_to is in ONLY ONE relevant group ... otherwise empty.


I'm a bit confused on this part:



I ONLY assign the group, if assigned_to is in ONLY ONE relevant group ... otherwise empty.



You can set additional conditions/rules on your assignment. Make it based on priority, category, a combination of those, etc. In your case, group type.



BTW, group type is going to be a sys_id. It's a list field which is a comma separated field of sys_ids, so it will never contain 'incident'.


Yeah - tried with sys_id before too ... but did not work. But now it might with the corrections from above but I was told not to use Glide Record in Client Script ;-(



Regarding the conditions/rules .. this will not work without scripting (at least I do not get it)


The assigned to could be member of 3 groups .. and 2 of those groups are with type incident. So it should NOT assign any group. If the user only has ONE group assigned with that type ... then the group should be set.


Hi,



Check this



http://wiki.servicenow.com/index.php?title=Reference_Qualifiers#gsc.tab=0


4.3.2.2 JavaScript Example: Constraining the Assignment Group Field

This example shows how to restrict an incident's Assignment group choices to only the groups that contain the user already specified in the Assigned to field.


  1. Open an incident.
  2. Right-click the Assignment group field and select Configure Dictionary (Personalize Dictionary in versions prior to Fuji).
  3. Enter javascript:new BackfillAssignmentGroup().BackfillAssignmentGroup() in the Reference qual field.
  4. Save the record.
  5. Navigate to System Definitions > Script Includes.
  6. Click New.
  7. Create a script include with the following JavaScript code:



var BackfillAssignmentGroup = Class.create();


BackfillAssignmentGroup.prototype = {


initialize: function() {


},



BackfillAssignmentGroup:function() {


var gp = ' ';


var a = current.assigned_to;



//return everything if the assigned_to value is empty


if(!a)


return;


//sys_user_grmember has the user to group relationship


var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('user',a);


grp.query();


while(grp.next()) {


if (gp.length > 0) {


//build a comma separated string of groups if there is more than one


gp += (',' + grp.group);


}


else {


gp = grp.group;


}


}


// return Groups where assigned to is in those groups we use IN for lists


return 'sys_idIN' + gp;


},


type: 'BackfillAssignmentGroup'


}



The next time you create an incident, select a user in the Assigned to field. Then click the Assignment group lookup icon. Only the groups that contain the user you just selected appear. For example, if Bob Smith belongs to the Database group and the Networking group and you assign an incident to Bob, the only options you can select for the assignment group are Database and Networking.