How to write script

MohanDegala
Tera Contributor

Create department field on incident table ,choices is
IT
Amin
2)create field as user -reference (sys_user)table

If some one select department IT then all IT related uses are aviator in user field same like admin.
how to write script for this scenario in ServiceNow?

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @MohanDegala 

 

There's no need to write a script; you can make it dependent, similar to how the Caller field is dependent on the Company field. Likewise, you can set it up the same way. You can refer to the example of Assignment Group and Assigned To for guidance.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

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

MohanDegala
Tera Contributor

 Thanks for responding but I know that way,  I want to achieve this through script?

 

Keshav72
Tera Contributor

Hi @MohanDegala ,

If you are using a choice field for Department, then you need to use an advanced reference qualifier in User field
To achieve this, you need to write scripts include:


Script Include
: In this script, you will query the user table using GlideRecord based on the department selected in the choice field.

Script:

users: function(department) {
    var users = "";
    if (department == "it") {
        var user = new GlideRecord("sys_user");
        user.addActiveQuery();
        user.addQuery("department", "sys_id of IT Department");
        user.query();
        while (user.next()) {
            users += user.sys_id + ",";
        }
    } else {

        var user = new GlideRecord("sys_user");
        user.addActiveQuery();
        user.addQuery("department", "sys_id of Admin Department");
        user.query();
        while (user.next()) {
            users += user.sys_id + ",";
        }
    }
    return sys_idIN + users;
    },

adavanced reference qualifier : javascript: new advancedReferenceQualifier().users(current.u_dept);

 

If you find my response helpful in resolving your query, please mark it as helpful.

Thank you!

Regards,
Keshav