Ignition Perspective removeContactInfo for email how to write a script

used following script to execute to change the email address and role of particular user
role is getting changed but not able to remove the existing email id but can able to add new email id

usernametext = self.getSibling("Dropdown_1").props.value

userToChange = system.user.getUser("", usernametext)

UserRole = userToChange.getRoles()

userToChange.removeRole(UserRole[0])

system.user.editUser("", userToChange)

addroles = self.getSibling("Dropdown_2").props.value

userToChange.addRole(addroles)

system.user.editUser("", userToChange)

useremail = userToChange.getContactInfo()


userToChange.removeContactInfo("email",useremail[0])
	
system.user.editUser("", userToChange)

emailtochange = self.getSibling("TextField_4").props.text

contactInfo = {"email":emailtochange}

userToChange.addContactInfo(contactInfo)

system.user.editUser("", userToChange)

What happens when you run this script? Are you getting an error?

Following error found in logviewer Error running action ‘dom.onClick’ on Misc/UserManagment@C/root/Button_2: TypeError: removeContactInfo(): 2nd arg can’t be coerced to String

User.getContactInfo() returns a dictionary of key-value pairs. So instead of referencing useremail[0] try useremail.get('email') to get the value associated with the email key. This will also prevent errors in the future if you add other contact types – assuming the value you want is always the first value can be dangerous.

after changing the code
useremail = userToChange.getContactInfo()

emailidtochange = useremail.get('email')	
		
userToChange.removeContactInfo("email",emailidtochange)

following error appears
Error running action ‘dom.onClick’ on Misc/UserManagment@C/root/Button_2: TypeError: get(): 1st arg can’t be coerced to int

Whoops, getContactInfo does in fact return a list. You can use something like this to (safely) access the first email defined for a user:

user = system.user.getUser("default", "admin")
email = next((ci.value for ci in user.contactInfo if ci.contactType == "email"), None)

You’ll want to make sure that email is not None before continuing execution.

1 Like

Thankyou Paul and Kathy for your support