i Cant understand these lines of code . could anyone please elaborate what each line does

Shyna1
Tera Contributor

1       if (current.state != 1 && (current.assigned_to.nil() || current.assignment_group.changes()) && current.sys_updated_by != gs.getProperty('xyz.remedy')) {

2           var getgrpUsers = new GlideRecord('sys_user_grmember');

3               getgrpUsers.addQuery('group', current.assignment_group);

4                 getgrpUsers.query();

5               if (getgrpUsers.next()) {

6                       current.assigned_to = getgrpUsers.user;

7               } else {

8                   current.assigned_to = "b18115a74fa39200717c2f9ca310c666";

              }

9       } else if (current.state != 1 && current.assignment_group.changes() && current.sys_updated_by == gs.getProperty('xyz.remedy') && previous.assignment_group != ""){

                                                              current.assigned_to = "";

                              }

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Let me try to explain. Hope this helps.


   


if (current.state != 1 && (current.assigned_to.nil() || current.assignment_group.changes()) && current.sys_updated_by != gs.getProperty('xyz.remedy')) { // It's checking the condition like if state is not equal to 1 and assigned_to is not null or assignment_group changes and system's updated by field is not matching with system propert of xyz.remedy


2           var getgrpUsers = new GlideRecord('sys_user_grmember'); // You are doing a GlideRecord to have a virtual view of all the records from sys_user_grmember table


3               getgrpUsers.addQuery('group', current.assignment_group); // Here you are checking what is the current assignment_group value and passing it to group field of sys_user_grmember table


4                   getgrpUsers.query(); // It's executing the query just like the way you do when you run the database query


5               if (getgrpUsers.next()) { // if condition checks if any record found based upon the given condition in GlideRecord Query


6                       current.assigned_to = getgrpUsers.user; // if record found then assign the user to assigned_to field of current table


7               } else {


8                   current.assigned_to = "b18115a74fa39200717c2f9ca310c666"; // If record not found then you are hard cording the sys_id of the user to assigned_to field of current table


              }


9       } else if (current.state != 1 && current.assignment_group.changes() && current.sys_updated_by == gs.getProperty('xyz.remedy') && previous.assignment_group != ""){


                                                              current.assigned_to = ""; // if conditions do not match then leave the assigned_to field of current table to blank


                              }


View solution in original post

4 REPLIES 4

antin_s
ServiceNow Employee
ServiceNow Employee

I am assuming it (current) is an incident record.



1. If the incident is updated by someone other than 'xyz.remedy', then it finds a user from the assignment_group of the incident record and assigns it to the user. If there is no user in the group, it assigns to a hard coded group (b18115a74fa39200717c2f9ca310c666)



2. If the incident is updated by   'xyz.remedy', then the incident is assigned to none.



There are few other minor conditions like assignment_group.changes()..etc.



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


Shishir Srivast
Mega Sage

Let me try to explain. Hope this helps.


   


if (current.state != 1 && (current.assigned_to.nil() || current.assignment_group.changes()) && current.sys_updated_by != gs.getProperty('xyz.remedy')) { // It's checking the condition like if state is not equal to 1 and assigned_to is not null or assignment_group changes and system's updated by field is not matching with system propert of xyz.remedy


2           var getgrpUsers = new GlideRecord('sys_user_grmember'); // You are doing a GlideRecord to have a virtual view of all the records from sys_user_grmember table


3               getgrpUsers.addQuery('group', current.assignment_group); // Here you are checking what is the current assignment_group value and passing it to group field of sys_user_grmember table


4                   getgrpUsers.query(); // It's executing the query just like the way you do when you run the database query


5               if (getgrpUsers.next()) { // if condition checks if any record found based upon the given condition in GlideRecord Query


6                       current.assigned_to = getgrpUsers.user; // if record found then assign the user to assigned_to field of current table


7               } else {


8                   current.assigned_to = "b18115a74fa39200717c2f9ca310c666"; // If record not found then you are hard cording the sys_id of the user to assigned_to field of current table


              }


9       } else if (current.state != 1 && current.assignment_group.changes() && current.sys_updated_by == gs.getProperty('xyz.remedy') && previous.assignment_group != ""){


                                                              current.assigned_to = ""; // if conditions do not match then leave the assigned_to field of current table to blank


                              }


Shishir Srivast
Mega Sage

If I have answered your question, please mark the response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list. https://community.servicenow.com/docs/DOC-5601


kalyansiva
Kilo Contributor

1       if (current.state != 1 && (current.assigned_to.nil() || current.assignment_group.changes()) && current.sys_updated_by != gs.getProperty('xyz.remedy')) {


2           var getgrpUsers = new GlideRecord('sys_user_grmember');


3               getgrpUsers.addQuery('group', current.assignment_group);


4                 getgrpUsers.query();


5               if (getgrpUsers.next()) {


6                       current.assigned_to = getgrpUsers.user;


7               } else {


8                   current.assigned_to = "b18115a74fa39200717c2f9ca310c666";


              }


9       } else if (current.state != 1 && current.assignment_group.changes() && current.sys_updated_by == gs.getProperty('xyz.remedy') && previous.assignment_group != ""){


                                                              current.assigned_to = "";


                              }



In the first line you are checking for the following conditions:


As current is used it must be a server side script


if (current.state != 1 && (current.assigned_to.nil() || current.assignment_group.changes()) && current.sys_updated_by != gs.getProperty('xyz.remedy')) {


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


if state field value is not equal to 1 and assigned to is empty (or) assignment group is changed and the record is updated by the user present in that system property "xyz.remedy"


If any of the above condition satisified


Gliding Group members table


adding a condition that group is selected assignment group on the record currently


running it


if that group exists in the group members table Then you are setting assigned to as the user in that group record


else


assigning it to a fixed user


else


you are checking the condition in a different combination and setting the assigned to: to empty


Hope this added a bit of understanding


Thanks