# Add New User through Scripting

**URL:** <https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506>\
**Category:** General Discussion\
**Created:** [March 16, 2021, 3:14am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506 "2021-03-16T03:14:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tohhy-pm17](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tohhy-pm17](https://forum.inductiveautomation.com/u/tohhy-pm17)\
**Post date:** [March 16, 2021, 3:14am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/1 "2021-03-16T03:14:14Z")

</div>

Have a good days guys.

Currently I’m doing the feature of add new users through scripting but I have faced a problem which I’m not able to apply a role to a user through scripting but other column of User Source able to insert such as Username, Last Name, First Name.

Below is my sample code for add new user through scripting

```
          userToGet = system.user.getNewUser("default", user_name)
      userToGet.set("firstname", first_name)
      userToGet.set(userToGet.LastName, last_name)
       
      # Assign role to user
      self.getSibling("Availability_Check").props.text = user_role
      user_role = self.getSibling("Role_TextField").props.roles
      userToGet.set("roles", user_role)
      
       
      # Add contact info & password
      phone_number = self.getSibling("Contact_No_TextField").props.text
      entered_password = self.getSibling("Password_TextField").props.text
      contactInfo = {"phone":phone_number}
      userToGet.addContactInfo(contactInfo)
      userToGet.set("password", entered_password)
       
      # Adds a user to the the "default" usersource.
      system.user.addUser("default", userToGet)

```

Anyone can help or give any advice. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tohhy-pm17](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tohhy-pm17](https://forum.inductiveautomation.com/u/tohhy-pm17)\
**Post date:** [March 16, 2021, 3:17am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/2 "2021-03-16T03:17:14Z")

</div>

![UMANGEMENT](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/1/a1b0efe2f0dd93e7ff0fc88e6b1cf1b408839bff.png)

---

<div class="post-metadata">

**Author:** ![tohhy-pm17](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tohhy-pm17](https://forum.inductiveautomation.com/u/tohhy-pm17)\
**Post date:** [March 16, 2021, 3:18am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/3 "2021-03-16T03:18:15Z")

</div>

This is the sample layout which all the rows are able to insert to user source except role row.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [March 16, 2021, 3:27pm UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/4 "2021-03-16T15:27:47Z")

</div>

If you do a dir() on the user object you got back, you should see a couple of functions, `addRole()` and `addRoles()`. The first takes a string (the name of the role) and the second takes a list of roles. I believe that will work – let me know if it doesn’t.

---

<div class="post-metadata">

**Author:** ![tohhy-pm17](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tohhy-pm17](https://forum.inductiveautomation.com/u/tohhy-pm17)\
**Post date:** [March 17, 2021, 4:02am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/5 "2021-03-17T04:02:47Z")

</div>

Hi KathyApplebaum,

I have try the code that you mentioned but it cant work for me.

---

<div class="post-metadata">

**Author:** ![tohhy-pm17](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tohhy-pm17](https://forum.inductiveautomation.com/u/tohhy-pm17)\
**Post date:** [March 17, 2021, 4:56am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/6 "2021-03-17T04:56:09Z")

</div>

Hi Ms. may I know why the addContactInfo() can work which the contact no information are able to add to the user source and show it in the table but for role when I use addRoleInfo() it did not have any response but the other data such as username, first name , last name able to insert to the user source and show it in the table but only the role rows is show as N/A.

In conclusion, what I want to ask is does Ignition have the function of addRoleInfo() ?

Hope I can get your help. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tohhy-pm17](https://avatars.discourse-cdn.com/v4/letter/t/e19adc/32.png) [@tohhy-pm17](https://forum.inductiveautomation.com/u/tohhy-pm17)\
**Post date:** [March 17, 2021, 9:12am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/7 "2021-03-17T09:12:19Z")

</div>

Hi Ms., may I know why the addContactInfo() can work which the contact no information are able to add to the user source and show it in the table but for role when I use addRoleInfo() it did not have any response but the other data such as username, first name , last name able to insert to the user source and show it in the table but only the role rows is show as N/A.

In conclusion, what I want to ask is does Ignition have the function of addRoleInfo() ?

Hope I can get your help. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 17, 2021, 11:00am UTC](https://forum.inductiveautomation.com/t/add-new-user-through-scripting/44506/8 "2021-03-17T11:00:56Z")

</div>

Copying the same thing a few hours apart won't make things go any faster. Also keep time zones in mind. 😉

addRoleInfo() does not exist. use addRole() or addRoles(). Kathy posted an example in this thread.

> [@Setting user roles when creating a user via script](http://forum.inductiveautomation.com/t/setting-user-roles-when-creating-a-user-via-script/27368/2):
>
> You’re close. Due to technical limitations, some things get accessed differently than others. Here’s the sample script I gave to training: def printResponse(responseList): if len(responseList) \> 0: for response in responseList: print "", response else: print " None" # Make a brand new 'blank' user. Not saved until we, well, save username = event.source.parent.getComponent('Text Field').text user = system.user.getNewUser("", "myAwesomeUser") # Let's fill in…
