Created
June 21, 2013 02:29
-
-
Save matthewpoer/5828437 to your computer and use it in GitHub Desktop.
Customer custom module has a relate field to Account. Need to swap that for a one:many relationship to contacts and maintain as much data as possible.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
truncate contacts_campa_campaigncommittee_1_c; | |
select * from contacts_campa_campaigncommittee_1_c; | |
-- Copy all of the data, using account_id for contact_id even though it's wrong | |
insert into contacts_campa_campaigncommittee_1_c | |
(id,date_modified,deleted, | |
contacts_campa_campaigncommittee_1contacts_ida, | |
contacts_campa_campaigncommittee_1campa_campaigncommittee_idb) | |
select campa_campaigncommittee_cstm.id_c,now(),0, | |
campa_campaigncommittee_cstm.account_id_c, | |
campa_campaigncommittee_cstm.id_c | |
from campa_campaigncommittee_cstm | |
join campa_campaigncommittee on campa_campaigncommittee_cstm.id_c = campa_campaigncommittee.id | |
where campa_campaigncommittee.deleted = 0 and account_id_c is not null and account_id_c <> ''; | |
-- Look up the Accounts that have the same name as a Contact, fuzzy match on first+last name | |
select accounts.id as account_id,accounts.name,contacts.id as contact_id,contacts.last_name | |
from accounts,contacts | |
where accounts.name is not null and accounts.name <> '' and accounts.deleted = 0 | |
and contacts.first_name is not null and contacts.first_name <> '' and | |
contacts.last_name is not null and contacts.last_name <> '' and contacts.deleted = 0 | |
and accounts.name = ltrim(rtrim(concat(contacts.first_name,' ',contacts.last_name))) | |
order by accounts.name; | |
-- Update the parent account to matching name-based contact | |
update contacts_campa_campaigncommittee_1_c | |
join accounts on accounts.id = contacts_campa_campaigncommittee_1contacts_ida | |
join contacts new_contacts on accounts.name = ltrim(rtrim(concat(new_contacts.first_name,' ',new_contacts.last_name))) | |
set contacts_campa_campaigncommittee_1contacts_ida = new_contacts.id | |
where contacts_campa_campaigncommittee_1_c.deleted = 0 | |
and accounts.deleted = 0 | |
and new_contacts.deleted = 0; | |
-- just drop leftovers; if it didn't match just kill it | |
-- update contacts_campa_campaigncommittee_1_c | |
-- set deleted = 1 | |
-- where contacts_campa_campaigncommittee_1contacts_ida in | |
-- ( | |
-- select id from accounts | |
-- ); | |
-- select * from contacts_campa_campaigncommittee_1_c where deleted = 1; | |
-- delete from contacts_campa_campaigncommittee_1_c where deleted = 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment