Pandora - Sample - Bulk Associate Contacts with Level1
From SmartWiki
The following HTML can be saved onto an HTML document or placed into a Web Page View to associate Contacts or Companies to Level 1 records in bulk (similar to an autoloader).
It also allows you to populate custom field data that is stored on the role (contact/level 1 intersection).
After selecting Contacts or Companies and filling in the required field information (including up to 10 role-based custom field) the data to be imported should be pasted in the field in the bottom of the page as tab delimited.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <LINK REL="stylesheet" TYPE="text/css" HREF="/css/styles_common.jsp"> <script language="JavaScript"> function include_dom(script_filename) { var html_doc = document.getElementsByTagName('head').item(0); var js = document.createElement('script'); js.setAttribute('language', 'javascript'); js.setAttribute('type', 'text/javascript'); js.setAttribute('src', script_filename); html_doc.appendChild(js); return false; } function includejsfiles() { include_dom("/validate.js"); include_dom("/dynamic_search.js"); include_dom("/Ajax/uta/uta.js"); } </script> </head> <body onload="includejsfiles();"> <table class="Form"> <tr> <th class="Form" width="150px">Import Type:</th> <td class="Form"><select id="importtype" onchange="importtypeOnChange(this.value,this.options[this.selectedIndex].text)"><option value=0>Select One</option> <option value=40>Contact</option> <option value=30>Account</option> </select> </td> </tr> <tr> <th class="Form">Developer Key:</th> <td class="Form"><input type="text" value="" id="developerkey" class="Hundred"> </td> </tr> <tr> <th class="Form">Application ID:</th> <td class="Form"><input type="text" value="" id="appid" class="Hundred"> </td> </tr> </table> <table id="rolecategoryTABLE" style="display: none"> <tr> <th class="Form" width="150px"><div id="rolecategoryDIV"></div> </th> <td class="Form"><input type="text" value="" id="rolecategoryid" class="Hundred"> </td> </tr> </table> <table id="dataTABLE" style="display: none"> <tr> <th class="Form" id="c1TH"></th> <td class="Form"><input type="text" id="cf1"></td> </tr> <tr> <th class="Form">Level 1 identifier field name</th> <td class="Form"><input type="text" id="cf2"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf3"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf4"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf5"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf6"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf7"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf8"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf9"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf10"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf11"></td> </tr> <tr> <th class="Form">UTA Role Custom Field field name</th> <td class="Form"><input type="text" id="cf12"></td> </tr> </table> <textarea id="data" class="XLarge"></textarea> <br> <input type="button" class="Button" value="Submit" onClick="process()"> <div style="height: 200px; overflow: auto;" id="errorDIV"></div> </body> <script> function importtypeOnChange(type, text) { if (type == "0") { document.getElementById("rolecategoryTABLE").style.display = "none"; document.getElementById("dataTABLE").style.display = "none"; return; } document.getElementById("rolecategoryTABLE").style.display = "block"; if (text == "Contact") document.getElementById("rolecategoryDIV").innerHTML = text + " Role ID:"; else document.getElementById("rolecategoryDIV").innerHTML = text + " Category ID:"; document.getElementById("c1TH").innerHTML = text + " identifier field name"; document.getElementById("dataTABLE").style.display = "block"; } function process() { var objecttype = parseInt(document.getElementById("importtype").value); if (isNaN(objecttype) || objecttype != 30 && objecttype != 40) { document.getElementById('errorDIV').innerHTML += "Invalid Import Type."; return; } var data = document.getElementById("data").value; if (data.length == 0) { document.getElementById('errorDIV').innerHTML += "No Data."; return; } var rows = data.split("\n"); ss_developer_key = document.getElementById('developerkey').value; for (i = 0; i < rows.length; i++) { var cells = rows[i].split("\t"); if (cells.length < 2) { document.getElementById('errorDIV').innerHTML += "No Identifier Data on Row " + (i + 1) + ".<br/>"; continue; } var objectid = getObject(objecttype, cells[0], document .getElementById("cf1").value); if (isNaN(objectid) || objectid == 0) { document.getElementById('errorDIV').innerHTML += "Can not find "; if (objecttype == 30) document.getElementById('errorDIV').innerHTML += "account"; else document.getElementById('errorDIV').innerHTML += "contact"; document.getElementById('errorDIV').innerHTML += "on Row " + (i + 1) + ".<br/>"; continue; } var opportunityid = getObject(23, cells[1], document .getElementById("cf2").value); if (isNaN(opportunityid) || opportunityid == 0) { document.getElementById('errorDIV').innerHTML += "Can not find level 1 on Row " + (i + 1) + "<br/>"; continue; } associate(objectid, opportunityid, cells, objecttype); } document.getElementById('errorDIV').innerHTML += "Done.<br/>"; } function associate(objectid, opportunityid, cells, objecttype) { var rs; switch (objecttype) { case 30: rs = levelonecompany_find(false, '', document .getElementById("appid").value, opportunityid, 'opcid', 'opc.opccompanyid=' + objectid, '', 0, 1, ''); break; case 40: rs = levelonecontact_find(false, '', document .getElementById("appid").value, opportunityid, 'oprid', 'opr.contactid=' + objectid, '', 0, 1, ''); break; } if (!rs || rs.recordcount == 0) { var fields = ""; for (i = 2; i < cells.length && i < 10; i++) { var field = document.getElementById('cf' + (i + 1)).value; if (field.length > 0 && cells[i].length > 0) fields += "," + field; } if (fields.length == 0) { utarole_add(false, "", 23, opportunityid, objecttype, objectid, document.getElementById('rolecategoryid').value); return; } fields = fields.substring(1); rs = new ssRecordSet(null); rs.addfieldnames(fields); idx = rs.getnewrecnumber(); for (i = 2; i < cells.length && i < 10; i++) { var field = document.getElementById('cf' + (i + 1)).value; if (field.length > 0 && cells[i].length > 0) rs.addfieldvaluebyname(idx, field, cells[i]); } utarole_add(false, "", 23, opportunityid, objecttype, objectid, document.getElementById('rolecategoryid').value, rs); } else { var id = ""; switch (objecttype) { case 30: id = rs.getfieldbyname(1, "opcid"); break; case 40: id = rs.getfieldbyname(1, "oprid"); break; } if (id.length == 0) return; rs = new ssRecordSet(null); var fields = ""; for (i = 2; i < cells.length && i < 10; i++) { var field = document.getElementById('cf' + (i + 1)).value; if (field.length > 0 && cells[i].length > 0) fields += "," + field; } if (fields.length == 0) return; fields = fields.substring(1); rs = new ssRecordSet(null); rs.addfieldnames(fields); idx = rs.getnewrecnumber(); for (i = 2; i < cells.length && i < 10; i++) { var field = document.getElementById('cf' + (i + 1)).value; if (field.length > 0 && cells[i].length > 0) rs.addfieldvaluebyname(idx, field, cells[i]); } utarole_update(false, '', 23, id, objecttype, rs); } } function getObject(objecttype, identifiervalue, objectfieldname) { var rs; switch (objecttype) { case 30: rs = company_find(false, "", "companyid," + objectfieldname, objectfieldname + "=\"" + identifiervalue + "\"", "", 0, 1, ""); break; case 40: rs = contact_find(false, "", "userid," + objectfieldname, objectfieldname += "=\"" + identifiervalue + "\"", "", 0, 1, ""); break; case 23: rs = levelone_find(false, '', document.getElementById("appid").value, "opportunityid," + objectfieldname, objectfieldname + "=\"" + identifiervalue + "\"", '', 0, 1, '', '', ''); break; default: return 0; } if (rs == null || rs.recordcount == 0) return 0; switch (objecttype) { case 30: return parseInt(rs.getfieldbyname(1, "companyid")); case 40: return parseInt(rs.getfieldbyname(1, "userid")); default: return parseInt(rs.getfieldbyname(1, "opportunityid")); } } </script> </html>