indexOf not working as expected

Community Alums
Not applicable

Hey Everyone, 

 

I need an ui action to display only if the user is in the L2 assignment group and not the team lead 1, team lead 2, or manager. 

 

Everything works until I try to use indexOf. It isn't working for the L2 assignment groups. Below is my script include code. 

 

What am I doing wrong? This ui action only needs to display if the current assignment group starts with L2 and the user isn't a team lead 1, team lead 2, or manager. 

 

var showChangePriority = Class.create();
showChangePriority.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
    checkCondition: function(current) {
        if ( gs.getUser().isMemberOf(current.assignment_group).toString().indexOf("L2") > -1 && (!gs.getUser().isMemberOf(current.assignment_group.manager) || !gs.getUser().isMemberOf(current.assignment_group.u_team_lead_1) || !gs.getUser().isMemberOf(current.assignment_group.u_team_lead_2))) {
            return true;
        } else {
            return false;
        }

    },
    type: 'showChangePriority'
});

 This is the condition field for UI action. 

new showChangePriority().checkCondition(current);
2 ACCEPTED SOLUTIONS

Robbie
Kilo Patron
Kilo Patron

Hi @Community Alums,

 

I see a couple of issues with the logic here (Assuming I understand what you're trying to achieve correctly)

The 'isMemberOf' function returns a boolean, as in true or false. You therefore don't need to use an additional indexOf check.

 

Use the following tweaks to your script to achieve the same (based on my understanding) which I've tried and tested.

I would also consider only calling this script from your UI action if the group starts with 'L2' in the first place, but the following will work

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

 

 

 

 

 

var showChangePriority = Class.create();
showChangePriority.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
    checkCondition: function(current) {
        if (current.assignment_group.name.startsWith('L2')
        && gs.getUser().isMemberOf(current.assignment_group) 
	&& gs.getUser() != current.assignment_group.manager
	&& gs.getUser() != current.assignment_group.u_team_lead_1
	&& gs.getUser() != current.assignment_group.u_team_lead_2
     ) {
            return true;
        } else {
            return false;
        }

    },
    type: 'showChangePriority'
});

 

 

 

Based on your feedback, you can also try the below:

var showChangePriority = Class.create();
showChangePriority.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkCondition: function(current) {
        // Check if the user is in the L2 assignment group
        if (gs.getUser().isMemberOf(current.assignment_group) && current.assignment_group.name.startsWith("L2") &&
            gs.getUserID() != current.assignment_group.manager && 
			gs.getUserID() != current.assignment_group.u_team_lead_1 &&
			gs.getUserID() != current.assignment_group.u_team_lead_2


        ) {
            // Check if the user is not a team lead 1, team lead 2, or manager

            return true; // User meets the conditions
        } else {
            return false;
        } // User does not meet the conditions
    },
    type: 'showChangePriority'
});

View solution in original post

Community Alums
Not applicable

Hey! 

 

Thank you so much. Your code got the closest to what I needed. I couldn't use getUser() method because it returns the current user object. Instead, I used getUserID() method to return the sys_id of the user and compare that value to the current.assignment_group.manager sys_ids. 

 

Below is the code that I'm using: 

 

var showChangePriority = Class.create();
showChangePriority.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkCondition: function(current) {
        // Check if the user is in the L2 assignment group
        if (gs.getUser().isMemberOf(current.assignment_group) && current.assignment_group.name.startsWith("L2") &&
            gs.getUserID() != current.assignment_group.manager && 
			gs.getUserID() != current.assignment_group.u_team_lead_1 &&
			gs.getUserID() != current.assignment_group.u_team_lead_2


        ) {
            // Check if the user is not a team lead 1, team lead 2, or manager

            return true; // User meets the conditions
        } else {
            return false;
        } // User does not meet the conditions
    },
    type: 'showChangePriority'
});

 

View solution in original post

12 REPLIES 12

Sonam_Tiwari
Kilo Sage

Hi @Community Alums ,

 

current.assignment_group

 

This might be the sys_id which is why indexOf isn't working for you. Try dot walking to name or getDisplayValue() and then do indexOf().

 

 

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Community Alums
Not applicable
gs.getUser().isMemberOf(current.assignment_group).getDisplayValue().indexOf("L2") > -1

 

I tried both ways, but the same issue. 

Looks like @Sohithanjan G's 1st point  holds valid as isMemberOf will return a boolean and we are trying to find L2 from a true or false value which is not correct.

 

gs.info(gs.getUser().isMemberOf('ITSM App-Dev')&& current.assignment_group.name.indexOf("L2") > -1); should work

sonamtiwari_0-1708965286421.png

 

 
 
 
 
Consider indicating the response as helpful and marking it as correct if it meets your needs.

chetanb
Tera Guru

Please check with search() method instead of indexOf