IF name is true OR if role is true (contained the role)

Before you post here, first search for an existing topic to Hi guys, I am trying to add another condition in my JS code but it does not work.

this works:

/Show edit IDEA if Responsible of Idea"/
$(document).on(‘knack-view-render.view_265’, function(event, view, data) {
$(’#view_265 div.kn-details-link’).hide();
var LoggedInUser = (Knack.getUserAttributes().name);
var Responsible = $(’#view_265 div.kn-detail.field_271 > div.kn-detail-body’).text();
if(LoggedInUser === Responsible) {
$(’#view_265 div.kn-details-link’).show();
};
});see if this question has already been asked and answered. If not, create a new topic!

Be concise, but don’t forget to provide context for your question, explaining what you need help with using specific examples. Screenshots and links encouraged.

This does not work:

/*Show edit button IF Responsible of the Idea OR Coordinator (contains the role) */

$(document).on(‘knack-view-render.view_265’, function(event, view, data) {
$(’#view_265 div.kn-details-link’).hide();
var LoggedInUser = (Knack.getUserAttributes().name);
var Responsible = $(’#view_265 div.kn-detail.field_271 > div.kn-detail-body’).text();
if(LoggedInUser === Responsible) { $(’#view_265 div.kn-details-link’).show(); }
else if( (Knack.getUserAttributes().roles(‘Coordinator’)) ) { $(’#view_265 div.kn-details-link’).show(); } ;
});

tks for any help

Hello Michael,

Knack.getUserAttributes().roles gives you roles in for of object_xyz, not as a role name.

try below
if(Knack.getUserAttributes().roles.indexOf(‘object_xyz’)>0)

Change xyz to original object nme.

Regards,
Sunny Singla
ssingla1985@gmail.com

1 Like

Perfect Sunny! Works at the first try.
tks you!
Much user-cases I have for that.

I spoke to soon. Doesn’t work yet.

if(Knack.getUserAttributes().roles.indexOf(‘object_xyz’)>0)

Here I don’t see where the object roles are compared to the role (Coordinator) I am looking for.

Check that object id in builder when you click on that role it show that role id in url.


Like this it’s object_26

also you can check in console by running Knack.getUserAttributes().roles it give you all roles connected to that user.

Thanks
Sunny Singla

I am looking for the roles of the Logedin User.
So if I run Knack.getUserAttributes().roles it gives me all roles of the logedin user.

now I need to check if one of those roles is equal to Coordinator. how to do that?

else if( (Knack.getUserAttributes().roles(‘Coordinator’)) ) { $(’#view_265 div.kn-details-link’).show(); } ;

here I need to compare Knack.getUserAttributes().roles with Coordinator. :thinking:

That i already show you go to builder and click on that object (Coordinator) you will see at top(URL) for object id
as i show in screenshot

I was using the Users object instead of the role object. Ok, changed it.
Still does not work.

$(document).on(‘knack-view-render.view_265’, function(event, view, data) {
$(’#view_265 div.kn-details-link’).hide();
var LoggedInUser = (Knack.getUserAttributes().name);
var Responsible = $(’#view_265 div.kn-detail.field_271 > div.kn-detail-body’).text();
if(LoggedInUser === Responsible) { $(’#view_265 div.kn-details-link’).show(); }
else if(Knack.getUserAttributes().roles.indexOf(‘object_5’)>0) { $(’#view_265 div.kn-details-link’).show();
} ;
});

You need to create a Role.

and assign that role to logged in user.

For any professional help, you can mail me.

Screenshot show that it’s not a Role it’s a simple object

Regards,
Sunny Singla
ssingla1985@gmail.com

I do have the Coordinator role which is assigned to the corresponding users.

If you say that for the object icon, I notified Knack support 2 weeks ago to better have a user icon when it is a Role.

first check → Knack.getUserAttributes().roles

then check if it contain roles of that object or not.

Then check url of this role and use that id.

Thanks Sunny. I will follow this structure.

Tell you soon.

@Sunny_Singla , at the end I got this one!

I had left it alone to work on styling issues, and looking at it again today with a fresh look I discovered 2 things very fast:
a) for some reason, I wasn’t using the normal coding single quotation marks,
b) I changed the page access to all users.

And now works perfectly! THANK YOU Very much!