# System user functions

**URL:** <https://forum.inductiveautomation.com/t/system-user-functions/94045>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [August 1, 2024, 2:41pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045 "2024-08-01T14:41:52Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 8, 2024, 1:24pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/21 "2024-08-08T13:24:51Z")

</div>

It exists.  
I think I am using .append. I need to use something else?  
I can't tell if setRoles wants a python list.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/d/6d8b6df8ec809cc6cd6f758d5c3a728b49f18ba0.png)  
This doesn't mean anything to me.  
It only has the one line, no examples.  
Needed this done yesterday, such stress.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 8, 2024, 1:28pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/22 "2024-08-08T13:28:22Z")

</div>

> [@Zach\_Larson](#):
>
> Needed this done yesterday, such stress.

Should have called support.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 8, 2024, 1:30pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/23 "2024-08-08T13:30:42Z")

</div>

I thought they don't help us with our scripts.  
I will do this, thanks.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 8, 2024, 1:31pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/24 "2024-08-08T13:31:35Z")

</div>

They won't just write it for you on spec, but they generally try to help with anything. They are too generous IMO.

If you frame the ticket as "hey I've written this script to add roles to a user but I can't figure out why it's not working" I think they'd be able to help you, assuming that statement is actually true.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 8, 2024, 1:35pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/25 "2024-08-08T13:35:00Z")

</div>

> [@Kevin.Herron](#):
>
> but they generally try to help with anything. They are too generous IMO.

This was a little off topic. I do think scripting help is in scope. But it's not unusual to see those guys power through and help with non-Ignition IT/windows/network problems as well. Or at least try for longer than I would.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 8, 2024, 1:42pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/26 "2024-08-08T13:42:44Z")

</div>

use this instead of appending to the role list

```python
user.addRole("Pilot")

```

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [August 8, 2024, 1:44pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/27 "2024-08-08T13:44:09Z")

</div>

First off, stop using ChatGPT. You'll be better off as it won't lead you in the wrong direction on this and you'll actually get a chance to learn debugging and troubleshooting on your own by trial and error.

Secondly, to help with troubleshooting and debugging, stop nesting functions inside functions for now and instead focus on breaking things down into single functions that get assigned to variables. You can print those variables when/where necessary, and sometimes you'll get a value, and other times you will get Java objects (you'll see the difference). Keep adding little bits of code and once you have a section working, you can clean it up and simplify it. Eventually you'll get done what you're wanting, then you can put it in a loop to process all the users/roles.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 8, 2024, 1:46pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/28 "2024-08-08T13:46:51Z")

</div>

@Zach_Larson I found this example script fairly easily, check it out.  
[Setting user roles when creating a user via script - Ignition - Inductive Automation Forum](https://forum.inductiveautomation.com/t/setting-user-roles-when-creating-a-user-via-script/27368/2)

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 8, 2024, 1:58pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/29 "2024-08-08T13:58:55Z")

</div>

It works now!

It seem .setRoles does not work.  
.addRole does.

I had tried importing java string, and then making my string for the role into a java string. Well it sort of worked, but then I got an error that some kind of python object could not be converted.

So then I went with addRole.

Thanks so much for all the help.

```python
# List of emails to check against
email_list = ["email1@example.com", "email2@example.com", "email3@example.com"]

# Retrieve all users
users = system.user.getUsers('default')

# Loop through each user
for user in users:
    # Get contact info for the user
    contacts = user.getContactInfo()
    
    # Check if any contact is an email in the email_list
    for contact in contacts:
        if contact.contactType == "email" and contact.value in email_list:
            # Get the user's current roles
            current_roles = user.getRoles()
            
            
            # Append the role if not already present
            if "roleThatExists" not in current_roles:
                print user                
                user.addRole("roleThatExists")
               
                response =system.user.editUser("default", user)
                print response.getErrors()

```

---

<div class="post-metadata">

**Author:** ![Camila\_Miglio](https://avatars.discourse-cdn.com/v4/letter/c/9fc348/32.png) [@Camila\_Miglio](https://forum.inductiveautomation.com/u/Camila_Miglio)\
**Post date:** [October 28, 2024, 4:57pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/30 "2024-10-28T16:57:32Z")

</div>

I was able to get .setRoles to work by making the new role list a java arrayList.

```python
from java.util import ArrayList
roles = ArrayList()  
userRoles = ['Role1', 'Role2', 'Role3'] # type: List
for role in userRoles:
	roles.add(role) # type: Java array list

```

Using your Aug 07 example:

```python
# List of emails to check against
email_list = ["email1@example.com", "email2@example.com", "email3@example.com"]

# Retrieve all users
users = system.user.getUsers('default')

# Loop through each user
for user in users:
    # Get contact info for the user
    contacts = user.getContactInfo()
    
    # Check if any contact is an email in the email_list
    for contact in contacts:
        if contact.contactType == "email" and contact.value in email_list:
            # Get the user's current roles
            current_roles = user.getRoles() #This is already a java array list
            
            # Append the "Pilot" role if not already present
            if "Pilot" not in current_roles:
                current_roles.add("Pilot") #correct method to add an element to a java.util.ArrayList
                
                # Update the user's roles
                user.setRoles(current_roles)
                
                # Save the changes
                system.user.editUser("default", user)

                print(f"Added 'Pilot' role to user: {user.getName()}")

```

[Previous page](https://forum.inductiveautomation.com/t/system-user-functions/94045.md?page=1)
