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.

2 Likes

Thankyou Paul and Kathy for your support

@PGriffith Thank you so much. Your solution was helpful to me.

for email work perfectly as per your solution. but in my case two phones. I have tried but every time got the same number.

@PGriffith please any suggestions?

Telefon = next((ci.value for ci in user.contactInfo if ci.contactType == "phone"), None)
print Telefon

Telefax = next((ci.value for ci in user.contactInfo if ci.contactType == "phone"), None)
print Telefax

Thanks,
Priyanka

First, here's why:
next gets you the next element from an iterator.
Here, your iterator is (ci.value for ci in user.contactInfo if ci.contactType == "phone").
The thing is, since you're redefining the same iterator for the second next call, it's actually a new iterator and you're starting from the start all over again.

You can instead define your iterator once, then call next on it as many times as you want:

contact_infos_iterator = (ci.value for ci in user.contactInfo if ci.contactType == "phone")
phone = next(contact_info_iterator, None)
fax = next(contact_info_iterator, None)

This should result in your 2 numbers.

4 Likes

@pascal.fragnoud Thank you for your quick response.

Worked perfectly!!