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

Community Alums
Not applicable

It still isn't working as expected. 

gs.getUser().isMemberOf(current.assignment_group.name).search("L2")

 

 

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Community Alums 

 

It seems there are a couple of issues with your code. Let's address them:

  1. Using indexOf() with isMemberOf(): The isMemberOf() function returns a boolean value indicating whether the user is a member of the specified group. It doesn't return a string that you can use with indexOf(). Instead, you should directly check if the user is a member of the group using isMemberOf().

  2. Checking L2 Assignment Groups: If you want to check if the current assignment group starts with "L2", you should use the startsWith() function instead of indexOf().

Here's the corrected version of your script include code:

 

var showChangePriority = Class.create();
showChangePriority.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
    checkCondition: function(current) {
        // Check if the user is in the L2 assignment group
        if (current.assignment_group.startsWith("L2")) {
            // Check if the user is not a team lead 1, team lead 2, or manager
            if (!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; // User meets the conditions
            }
        }
        return false; // User does not meet the conditions
    },
    type: 'showChangePriority'
});

 

 

This code checks if the current assignment group starts with "L2" using startsWith(), and then checks if the user is not a member of the manager, team lead 1, or team lead 2 groups. If both conditions are met, it returns true; otherwise, it returns false.

Make sure to use this script include with your UI action to determine if the action should be displayed.

 

Please mark my answer as Accepted as Solution & hit helpful !!! 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

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'
});

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'
});

 

Hi @Community Alums,

 

Thanks for circling back. That is strange as I tested on my PDI (Personal Dev Instance) before posting but glad you got it working.

I'll update my response to make sure it covers both options for you.

 

So as to help others and recognize the contribution, please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie