# Ignition Perspective removeContactInfo for email how to write a script

**URL:** <https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [October 20, 2021, 5:38pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174 "2021-10-20T17:38:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![subramaniam.gopal](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@subramaniam.gopal](https://forum.inductiveautomation.com/u/subramaniam.gopal)\
**Post date:** [October 20, 2021, 5:38pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/1 "2021-10-20T17:38:44Z")

</div>

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)
```

---

<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:** [October 20, 2021, 6:06pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/2 "2021-10-20T18:06:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![subramaniam.gopal](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@subramaniam.gopal](https://forum.inductiveautomation.com/u/subramaniam.gopal)\
**Post date:** [October 21, 2021, 3:49am UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/3 "2021-10-21T03:49:08Z")

</div>

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

---

<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:** [October 21, 2021, 2:23pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/4 "2021-10-21T14:23:13Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![subramaniam.gopal](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@subramaniam.gopal](https://forum.inductiveautomation.com/u/subramaniam.gopal)\
**Post date:** [October 21, 2021, 2:34pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/5 "2021-10-21T14:34:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 21, 2021, 3:22pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/6 "2021-10-21T15:22:05Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![subramaniam.gopal](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@subramaniam.gopal](https://forum.inductiveautomation.com/u/subramaniam.gopal)\
**Post date:** [October 21, 2021, 3:54pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/7 "2021-10-21T15:54:49Z")

</div>

Thankyou Paul and Kathy for your support

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [March 22, 2023, 3:27pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/8 "2023-03-22T15:27:23Z")

</div>

@paul-griffith Thank you so much. Your solution was helpful to me.

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [March 22, 2023, 4:03pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/9 "2023-03-22T16:03:00Z")

</div>

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

@paul-griffith please any suggestions?

```auto
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

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [March 22, 2023, 4:18pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/10 "2023-03-22T16:18:51Z")

</div>

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [March 22, 2023, 4:28pm UTC](https://forum.inductiveautomation.com/t/ignition-perspective-removecontactinfo-for-email-how-to-write-a-script/52174/11 "2023-03-22T16:28:18Z")

</div>

@pascal.fragnoud Thank you for your quick response.

Worked perfectly!!
