Add New User through Scripting

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.

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

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.

Hi KathyApplebaum,

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

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.

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.

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

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

1 Like