Updating Customers (Python)

Updating Customers (Python)
 1db = impact.active_database
 2
 3impact.gui.output_toolbox.clear()
 4impact.gui.output_toolbox.add("customer Count: " + str(db.customers.count))
 5impact.gui.output_toolbox.add("**** Find By Name 'Default' ****")
 6c = db.customers.find_by_name("Default")
 7
 8if c.isNone():
 9    impact.gui.output_toolbox.add("Error: Unable to locate a customer with name 'Default'")
10else:
11
12    # save Key so we can locate the same ICustomer again after changing its name
13    custKey = c.key
14
15    impact.gui.output_toolbox.add("Code: " + c.code)
16    impact.gui.output_toolbox.add("Name: " + c.name)
17    impact.gui.output_toolbox.add("Active: " + str(c.active))
18
19    # WARNING: If you are using Enterprise each of these changes will trigger the customer
20    # to be published. If you want to avoid this then use IDatabaseValues and the changes
21    # will only be published once when DoUpdate is called
22    c.code = "DEF_CUST"
23    c.name = "Default (Renamed)"
24    c.active = False
25
26    # Note: It is important to assign c.values to a local
27    # variable because otherwise we would get a new interface everytime
28    # we used c.values which would return to stored values
29    values = c.values
30
31    values.save("CS_ADDR1", "Some Street")
32    values.save("CS_ADDR2", "Christchurch")
33    values.save("CS_ADDR3", "New Zealand")
34    values.save("CS_CONTAC1", "Rob Whitehead")
35
36    # The values are updated in the database when DoUpdate is called
37    if values.do_update():
38        impact.gui.output_toolbox.add("Successfully updated customer '" + c.name + "'")
39    else:
40        impact.gui.output_toolbox.add("Error: Unable to update customer")
41
42    # To ensure any changes are reflected internally you also need to call
43    # ICustomers.refresh to force all customers to be reloaded from the database
44    # if you don't then old cached values may be used.
45
46    # NOTE: If you are iterating an ICustomers collection and updating individual ICustomer's
47    # it is important not to call ICustomers.refresh until after you have updated all
48    # the customers.
49    db.customers.refresh()
50    c = db.customers.find_by_key(custKey)
51
52    impact.gui.output_toolbox.add("Code: " + c.code)
53    impact.gui.output_toolbox.add("Name: " + c.name)
54    impact.gui.output_toolbox.add("Active: " + str(c.active))