Bulk Update of Custom Attributes?

All,

Has anyone found a way to “bulk upload” custom attributes to a subset of users? For example, I need to:

  1. Add the Country custom attribute to a subset of users
  2. Populate the Country attribute with 1 to many countries (it is already set as a multi-value attribute) based on the user id. Ie. some users will only have one country where a regional person will have multiple.

Worst case we do it by hand, but I figure someone has found a better way. Thanks.

Hi Christopher,

Anything that you can do in the UI can also be done using the vast library of open APIs that Dundas BI has. In this case, you can use the following steps (assuming you already have the custom attribute created in your Dundas BI instance):

  1. /Account/Query - Get a list of all the user accounts. You can play around with the filter property to get the subset of user accounts that satisfy a particular condition.

  2. /Account/UpdateCustomAttributesForAccount/{id}/ - Then you can iterate through account list and add the custom attribute and its value to their account.

You can find the appropriate JavaScript functions to achieve the above steps from our JS library - https://www.dundas.com/support/api-docs/js/

I hope this helps.

Thanks,
Pankaj

Thanks Pankaj, I used this plus the pydundas library that @guillaume made several years ago (thanks!) to get to the below script, which works for a single account and update of the “country” multivalue attribute. I included it in case anyone wants to use for a similar exercise (keys and username/pwd information need to be updated accordingly).

However, when I try to use a csv file to populate the name and attribute (commented out below), the Country is being read in with extra image characters and it is making the request fail. Have you run into this issue before? For example, if I have [‘Spain’] in the csv, it is being read in asimage when passed to the json. (had to paste as pictures because the post editor removes the image )

from pydundas import Api, Session
#from csv import DictReader
import sys
import json

url = 'url'
user = 'apiaccount'
pwd = '***'

#with open('emails.csv', 'r') as read_obj:
#    csv_dict_reader = DictReader(read_obj)
#    for row in csv_dict_reader:
#        name = row['Name']
#        countries = row['Country']

name = 'email@domain.com'
countries = ['Spain','Morocco']

with Session(user=user, pwd=pwd, url=url, loglevel='warn') as d:
    userinfo = d.post('Account/Name/', **{'json': name}).json()
    userid= userinfo['id']
    payload = [{"__classType":"dundas.account.CustomAttribute","key":"f23bebed-d0e2-4868-b89c-c9c4f2b77eb8","value":{"__classType":"dundas.account.CustomAttributeValue","attributeId":"f23bebed-d0e2-4868-b89c-c9c4f2b77eb8","values":countries}}]
    updateatt = d.post('account/updatecustomattributesforaccount/' + userid,**{'json' : payload}).json()
    print(updateatt)

Hi Chris,

I have used csv.reader to read csv files in python - https://docs.python.org/3/library/csv.html. Here you can define some additional parameters escapechar to escape certain characters while reading the csv file.
Besides, this I think you can also replace the backslash in the countries using something like - row[‘Country’].replace("\", “”).

I hope this helps.

Regards,
Pankaj

Hi Pankaj,

Root cause was I needed to convert from a string to a list. I am pasting the below code for completion.

with open('emails.csv', 'r', newline='') as read_obj:
csv_dict_reader = csv.reader(read_obj,dialect='excel')
line_count=0
for row in csv_dict_reader:
    if line_count == 0:
        print(f'Header')
        line_count=1
    else:
        name = row[0]
        country = row[1]
        countries = list(country.split(","))
        print(name)
        print(country)
        print(countries) 
        with Session(user=user, pwd=pwd, url=url, loglevel='warn') as d:
            a = d.post('Account/Name/', **{'json': name}).json()
            #print(a)
            userid= a['id']
            payload = [{"__classType":"dundas.account.CustomAttribute","key":"f23bebed-d0e2-4868-b89c-c9c4f2b77eb8","value":{"__classType":"dundas.account.CustomAttributeValue","attributeId":"f23bebed-d0e2-4868-b89c-c9c4f2b77eb8","values":countries}}]
            #print(payload)
            b = d.post('account/updatecustomattributesforaccount/' + userid,**{'json' : payload}).json()
            print(b)
2 Likes