Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to update assignment group based on department

Nitesh Chandana
Kilo Expert

Hello,

I had a table u_master, if i entered 'u_name' in u_master table,

Assignment group in another table named u_ticket should be updated based on the department of user(u_name) in u_master table.

If user belongs to IT department then the assignment group be changes to Software in U-ticket table.

my code is

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

  // Add your code here

var gr= new GlideRecord('u_ticket');

gr.initialize();

gr.category=current.u_category;

  gr.u_short_description=current.u_short_description;

  //gr.u_caller_name=current.u_name;

        gr.u_caller_id=current.u_name.toString();

var target = new GlideRecord('sys_user');  

target.addQuery('department',current.u_bavk);  

target.query();  

if(target.next())  

{  

    //gs.info('found user '+target.sys_id);  

if(target.name.department == '8e7458c7db2132004dba5c00cf9619b2'){ // pass the sys_id of the Axis Department

  gr.u_assignment_group='2605925bdba532004dba5c00cf9619eb';   //Please pass the sys_id of the Axis rectify Group.

    gr.setReadOnly('u_assignment_group','true');

}

//gr.u_assignment_group = target.sys_id;  

}  

  gr.insert();

Can any one help me out??

Thanks.

Nitesh

1 ACCEPTED SOLUTION

Hi Nitesh,



The best way to implement this is via Business Rules. Please create an after Insert Business rule in u_master table with the below script. Please inactive all the existing Client Scripts which were created for this requirement and give it a try.




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


      var name;


      var group;


      var user_name = current.u_name;


      var user = new GlideRecord('sys_user');


      user.addQuery('name', user);


      user.query();


      if(user.next()){


              name = user.sys_id;


              if(user.department == '8e7458c7db2132004dba5c00cf9619b2'){ //Sys_id of Axis Department


                      group = '2605925bdba532004dba5c00cf9619eb'; //Sys_id of Axis Group


              }


             


              var ticket = new GlideRecord('u_ticket');


              ticket.initialize();


              ticket.category = current.u_category;


              ticket.u_short_description = current.u_short_description;


              ticket.u_caller_id = name;


              ticket.u_assignment_group = group;


              ticket.insert();


              gs.addInfoMessage('New Ticket is created');


      }


      else{


              gs.addInfoMessage('New Ticket can not be created. Please provide valid User name');


      }


     


})(current, previous);




I hope this helps.Please mark correct/helpful based on impact


View solution in original post

6 REPLIES 6

nayanawadhiya1
Kilo Sage

Hey Nitesh,




Use ASSIGNMENT RULES for your requirement.



Refer this -


Defining Assignment Rules - ServiceNow Wiki


amlanpal
Kilo Sage

Hi Nitesh,



If you want to do this configuration done in the Client Script please find the below snapshot. Please create a new onChange Clent script on change of the field 'u_name'.


Although, as per Best Practice, I would suggest to do all the server side queries and all in the Script include and call it in the Client Side using GlideAJAX. But I have just modified your code in the client side itself.


find_real_file.png



I hope this helps.Please mark correct/helpful based on impact


thanks for response amlan,


i need to update u_ticket table(create a record) when i click on submit in u_master table.


I am agree with you that Script include and call it in the Client Side using GlideAJAX. But as i am not so much familiar with Script Include, i am using Business rule.


I tried using client script Onchange, but u_ticket table not updating as when clicked submit in i_master table. All fields are changing when i opens the record manually.



Can you please provide me Script Include code.


Hi Nitesh,



Yes, I can provide you the necessary codes. But to do so, I need to clarify couple of things from your side.


1. onSubmit of a New Record in 'u_maste'r table a record should get created in 'u_ticket' table. Correct?


2. Is the field 'u_name' in the 'u_master' table is a reference field referring 'sys_user' table? Also 'u_caller_id' in the 'u_ticket' table is a reference field referring 'sys_user' table?


3. Is 'u_assignment_group' in 'u_ticket' table refers 'sys_user_group'?


4. If the person selected in 'u_name' field in 'u_master' table belongs from 'Axis Department' (bifurcated by sys_id) then the corresponding newly created record in 'u_ticket' should have 'Axis Group' (bifurcated by sys_id) in it's 'u_assignment_group' field. Correct?