Advertisement
matthewpoer

SugarCRM Notes Import (Young)

Feb 28th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.95 KB | None | 0 0
  1. -- Backstory: customer's team was able to import all of their existing
  2. -- Accounts/Contacts/Leads into SugarCRM all by themselves, but not the notes. So they
  3. -- handed us three Excel documents with the notes exported from their old
  4. -- system. Each held similar (but not quite the same) data/field definitions.
  5. --
  6. -- I cleaned the files up a bit using Excel/Calc and saved these three
  7. -- files as CSVs and put them into a MySQL database for easier manipulation. The tricky
  8. -- part of importing Notes is that you need to related them to something, so I had to
  9. -- export Accounts/Contacts/Leads from SugarCRM to get the SugarCRM GUIDs for these
  10. -- records. I mashed 'em all together into a master 'notes' table and exported to
  11. -- a single CSV file which should, hopefully, import pretty cleanly into SugarCRM.
  12. --
  13. -- Only caveat on the import was to skip importing the ID field. That was just a local
  14. -- ref and not a very quality SugarCRM GUID.
  15. --
  16. -- In total, 1104 notes were succesfully imported with Account/Lead/Contact references, and
  17. -- of those that didn't make it to the system, it is because I couldn't match them with
  18. -- any of the records in SugarCRM. 415 Accounts Notes, 6 Contact Notes and 7 Lead Notes were
  19. -- skipped over.
  20.  
  21. -- mysql -u root -p young_moving_notes_import --local-infile=1
  22.  
  23. create table account_notes (
  24.     id int not null auto_increment primary key,
  25.     subject varchar(255),
  26.     description text,
  27.     date datetime,
  28.     account_name varchar(255)
  29. );
  30.  
  31. create table contact_notes (
  32.     id int not null auto_increment primary key,
  33.     date datetime,
  34.     subject varchar(255),
  35.     description text,
  36.     first_name varchar(255),
  37.     last_name varchar(255),
  38.     account_name varchar(255)
  39. );
  40.  
  41. create table leads_notes (
  42.     id int not null auto_increment primary key,
  43.     subject varchar(255),
  44.     date datetime,
  45.     description text,
  46.     first_name varchar(255),
  47.     last_name varchar(255)
  48. );
  49.  
  50. create table sugar_accounts (
  51.     account_name varchar(255),
  52.     id varchar(40)
  53. );
  54.  
  55. create table sugar_contacts (
  56.     first_name varchar(255),
  57.     last_name varchar(255),
  58.     id varchar(40)
  59. );
  60.  
  61. create table sugar_leads (
  62.     first_name varchar(255),
  63.     last_name varchar(255),
  64.     id varchar(40)
  65. );
  66.  
  67. load data local infile 'account_notes.csv' into table account_notes
  68. fields terminated by ',' enclosed by '"' lines terminated by '\n' IGNORE 1 LINES
  69. (subject,description,@var,account_name)
  70. set date = STR_TO_DATE(@var,'%m/%e/%Y');
  71.  
  72. load data local infile 'contact_notes.csv' into table contact_notes
  73. fields terminated by ',' enclosed by '"' lines terminated by '\n' IGNORE 1 LINES
  74. (@var,subject,first_name,last_name,account_name)
  75. set date = STR_TO_DATE(@var,'%m/%e/%Y');
  76.  
  77. load data local infile 'lead_notes.csv' into table leads_notes
  78. fields terminated by ',' enclosed by '"' lines terminated by '\n' IGNORE 1 LINES
  79. (subject,@var,description,first_name,last_name)
  80. set date = STR_TO_DATE(@var,'%m/%e/%Y');
  81.  
  82. load data local infile 'Accounts.csv' into table sugar_accounts
  83. fields terminated by ',' enclosed by '"' lines terminated by '\n' IGNORE 1 LINES
  84. (account_name,id);
  85.  
  86. load data local infile 'Contacts.csv' into table sugar_contacts
  87. fields terminated by ',' enclosed by '"' lines terminated by '\n' IGNORE 1 LINES
  88. (first_name,last_name,id);
  89.  
  90. load data local infile 'Leads.csv' into table sugar_leads
  91. fields terminated by ',' enclosed by '"' lines terminated by '\n' IGNORE 1 LINES
  92. (first_name,last_name,id);
  93.  
  94. alter table account_notes add column account_id varchar(36);
  95. alter table contact_notes add column contact_id varchar(36);
  96. alter table leads_notes add column leads_id varchar(36);
  97.  
  98. -- Associates 386 of the 801 Total
  99. update account_notes
  100. join sugar_accounts on account_notes.account_name = sugar_accounts.account_name
  101. set account_notes.account_id = sugar_accounts.id;
  102.  
  103. -- 262/268 matched
  104. update contact_notes
  105. join sugar_contacts on contact_notes.last_name = sugar_contacts.last_name
  106.     and contact_notes.first_name = sugar_contacts.first_name
  107. set contact_notes.contact_id = sugar_contacts.id;
  108.  
  109. -- 427/434
  110. update leads_notes
  111. join sugar_leads on leads_notes.last_name = sugar_leads.last_name
  112.     and leads_notes.first_name = sugar_leads.first_name
  113. set leads_notes.leads_id = sugar_leads.id;
  114.  
  115. create table notes (
  116.     id int not null auto_increment primary key,
  117.     date_entered datetime,
  118.     date_modified datetime,
  119.     name varchar(255),
  120.     parent_type varchar(255),
  121.     parent_id varchar(36),
  122.     description text
  123. );
  124.  
  125. insert into notes (date_entered,date_modified,name,parent_type,parent_id,description)
  126. select date,NOW(),subject,'Accounts',account_id,description from account_notes where account_id is not null;
  127.  
  128. insert into notes (date_entered,date_modified,name,parent_type,parent_id,description)
  129. select date,NOW(),subject,'Contacts',contact_id,description from contact_notes where contact_id is not null;
  130.  
  131. insert into notes (date_entered,date_modified,name,parent_type,parent_id,description)
  132. select date,NOW(),subject,'Leads',leads_id,description from leads_notes where leads_id is not null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement