Script include assign to groups depending on

Fotina Galieb
Tera Contributor

Hello All,

 

I am trying to create Script Include which help me check company field in user record and depending on this Company assign Task to the particular group.

 

var GroupComAssign = Class.create();
GroupComAssign.prototype = {
initialize: function() {
},

// Function to determine assignment group based on company of the requested_for user
getAssignmentGroup: function() {

var requester = requested_for;
var userGR = new GlideRecord('sys_user');
if (userGR.get(requested_for)) {
// Get company from the user record
var userComp = userGR.company;

// Determine the assignment group
switch (userComp) {
case 'Yellow Comp':
return 'k76a3f798675296ee5d64e70c87653f8'; // Yellow Service Desk
case 'Blue Comp':
return '4e7e9803878bb010ee238764690896hj'; // Blue IT Service Desk
case 'Green Comp':
return '5876876866598650okm6586977003mu4'; // Green IT Service Desk
}
}
},

type: 'GroupComAssign'
};

 

I call it with:

new GroupComAssign().getAssignmentGroup() == 'true';
 
Unfortunately, it didnt work as I expected, do you have any advice how to improve it and make it work?
 
Thank you in advance!
4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

In your script, 'requested_for' is undefined.  Depending on where you are calling the script include from - a reference qualifier?  Client Script? Business Rule? you can pass in this value in the call

new GroupComAssign().getAssignmentGroup(current.requested_for)

Then add the variable as a function argument

getAssignmentGroup: function(requested_for) {

In the Script Include you are returning what should be one of three group sys_ids, so the call that == 'true' doesn't make sense.  Where specifically are you using the Script Include call?

JohnnySnow
Kilo Sage

@Fotina Galieb 

1. You are trying to access the requested_for incorrectly, you need to pass the requested for as shown below.

2. Calling the function is incorrect above: new GroupComAssign().getAssignmentGroup() == 'true';

 

Please try below code and let me know. Make sure to pass the sys id or approapriate variable of the requested_for while calling the SI.

var GroupComAssign = Class.create();
GroupComAssign.prototype = {
    initialize: function() {
    },

 
    getAssignmentGroup: function(requested_for) {
        var userGR = new GlideRecord('sys_user');
        
      
        if (userGR.get(requested_for)) {
            var userComp = userGR.company.getDisplayValue();
           switch (userComp) {
                case 'Yellow Comp':
                    return 'k76a3f798675296ee5d64e70c87653f8'; // Yellow Service Desk
                case 'Blue Comp':
                    return '4e7e9803878bb010ee238764690896hj'; // Blue IT Service Desk
                case 'Green Comp':
                    return '5876876866598650okm6586977003mu4'; // Green IT Service Desk
                default:
                    return null; 
            }
        }
        return null; 
    },

    type: 'GroupComAssign'
};

// Example call:
var groupId = new GroupComAssign().getAssignmentGroup('some_sys_user_id');
gs.info('Assignment Group ID: ' + groupId);

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

AshishKM
Kilo Patron
Kilo Patron

Hi @Fotina Galieb , 

 

As per my understanding, when this script include method being called, are you not passing the requester_for as input.

How script include method know about the requestor_for value.

 

I think, you need to change the following code which is calling script include method.

 

From 

new GroupComAssign().getAssignmentGroup() == 'true';

 

To

new GroupComAssign().getAssignmentGroup(requestor_for);

and read this requestor_for in script method as below udpated code, also added break statement in Switch Case.

 

 

var GroupComAssign = Class.create();
GroupComAssign.prototype = {
    initialize: function() {},

    // Function to determine assignment group based on company of the requested_for user
    getAssignmentGroup: function(requestor_for) {

        //var requester = requested_for; 
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(requested_for)) {
            // Get company from the user record
            var userComp = userGR.company;

            // Determine the assignment group
			var serviceDeskGroup;
            switch (userComp) {
                case 'Yellow Comp':
                    serviceDeskGroup = 'k76a3f798675296ee5d64e70c87653f8'; // Yellow Service Desk
					break;
                case 'Blue Comp':
                    serviceDeskGroup= '4e7e9803878bb010ee238764690896hj'; // Blue IT Service Desk
					break;
                case 'Green Comp':
					serviceDeskGroup = '5876876866598650okm6586977003mu4'; // Green IT Service Desk
                    break;
            }
			return serviceDeskGroup;
        }
    },

    type: 'GroupComAssign'
};

 

 

Please test and let us know the result, if it works for you, good one else share the code from where you are calling this script include method.  ( Share Screen shots )

 

Note: You need to make provision of some other default group if user's company not match either of given in case statement, then default case should be in the last and return default group.

 

-Thanks,
AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Abhay Kumar1
Giga Sage

@Fotina Galieb Yes that looks one of issue mentioned by @AshishKM  and also company comparison where that is refrence field so script would expect sys_id instead of name.