Create new phones over existing ones for each party contact_mechanism ( migration )

Hi ! I am tryng to update existing phones list and add some new ones over a script and what i can do is to create new parties with all the data, but what i can’t do is to update for each partie the extra phone number.

What i succeded is to migrate multiple addreses for each partie.
What i can’t understand, is how should i do to add each phone for each “original” partie.

I am sure that some of you allready did this before :slight_smile: please help ! any piece of code to see how is done? or explanation ? anything that could help ! thanks in advance !

Contact mechanism are linked to party through a One2Many contact_mechanisms. So you just have to fill the field with as many instances as needed.

Thanks for answering so fast Cédric !
That, understanding that i have to do for example:

p = Party()
mechanism = p.contact_mechanism.new()
mechanism.save()    # but this didn't work well.. 

tried also:

mechanism = ContactMechanism.find([])

if Party.find(['id', '=',  ids_from_xslx_file[i]], limit=1)
for cm in mechanism: 
     try: 
          if phone_number.startswith(..):
                 cm.value = phone_number
                 cm.type = 'phone'  
                 cm.save()

But this one overwrites the existing phones… not so good. Maybe i didn’t understood exactly how it works internally. Also i am kind of noob with Python in general but I’m willing to learn more :slight_smile:

You have to save the party, you just created.

Of course as you update existing mechanism.
If you want new mechanism, you must instantiate new record.

       
        for i in range(len(df)):
            if Party.find(['code', '=', "C" + sanitize(df['ID_Tryton'][i])], limit=1):
                p = Party()   
                cm = p.contact_mechanisms.new()
                try:
                    if sanitize_phone(df['TELF1'][i]).startswith('9'):
                        cm.value = sanitize_phone(df['TELF1'][i])
                        cm.type = 'phone'
                    elif sanitize_phone(df['TELF1'][i]).startswith('6'):
                        cm.value = sanitize_phone(df['TELF1'][i])
                        cm.type = 'mobile'
                except Exception as e:
                    print(e)
                    pass
                to_save.append(p)
        Party.save(to_save)`

I think I’m getting close to solve it :smiley:
Parties are created, but as new ones, not by adding each phone number for an existing party wich i try to find it by internal code as i have stored clients as C+ inicial_id from the migration file.xlsx.

[proteus.Model.get('party.party')(2454),
 proteus.Model.get('party.party')(2455),
 proteus.Model.get('party.party')(2456),
 proteus.Model.get('party.party')(2457),
 proteus.Model.get('party.party')(2458),
 proteus.Model.get('party.party')(2459),
 proteus.Model.get('party.party')(2460),
 proteus.Model.get('party.party')(2461),

Of course that you create new party because:

If you want to add to the searched parties, you must use the result of you Party.find call.

1 Like

I tought about that…because instanciation of Party() is used when i create new records…doing a clean migration…

if Party.find(['code', '=', "C" + sanitize(df['ID_Tryton'][i])], limit=1):
    # p = Party()
    p, = Party.find(['code', '=', "C" + sanitize(df['ID_Tryton'][i])])
    if p:
        cm = p.contact_mechanisms.new()
        try:
            if sanitize_phone(df['TELF1'][i]).startswith('9'):
                if sanitize_phone(df['TELF1'][i]):
                    cm.value = sanitize_phone(df['TELF1'][i])
                    cm.type = 'phone'
            elif sanitize_phone(df['TELF1'][i]).startswith('6'):
                if sanitize_phone(df['TELF1'][i]):
                    cm.value = sanitize_phone(df['TELF1'][i])
                    cm.type = 'mobile'
            to_save.append(p)
            # print(cm.value, cm.type)
        except Exception as e:
            print(e)
            pass

Party.save(to_save)

Replaced the search ID_Tryton with Client_ID and it worked :relaxed: :upside_down_face:
And this is just the start… Thank you for your patience ! Now all i have to do is validate the numbers with +34 and write the comments for each one :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.