function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
StefanStefan 

Improve Mass Update Contact Address

I am looking to improve the below S-Control taken from the AppExchange and am a bit out of my depth.
 
Looking to achieve the following when mass updating contact addresses from the Account:
 
1. Populate null values on account with null values on contact. Currently if I have Account state = null and Contact State = London. I end up with City = London, State = London which is incorrect.
 
2. Add a custom field to the update. I have a custom lookup field on Account and Contact (Country ISO Code).
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
<!--  
 AJAX  scontrol to update one or many contact records to match the parent account

 jan 25 2007 updated to new ajax toolkit, fix skin, test with more locales
 
-->
<title>Mass Change Contact Addresses</title>
<link  href="/dCSS/Theme2/default/common.css" type="text/css" rel="stylesheet" >
<link  href="/dCSS/Theme2/default/custom.css" type="text/css" rel="stylesheet" >
<script type="text/javascript" src="/js/functions.js"></script>
<script type="text/javascript" src="/js/setup.js"></script>

  <script type="text/javascript" src="/soap/ajax/8.0/connection.js"></script>

<script language="javascript"> 
<!--
function pageInit() {
 loadAccountInfo();
 setupForm();
}

var accId = "{!Account.Id}"; 
var accName = "{!Account.Name}"; 
var addStreet = ""; // this link is filled in by loadAccountInfo(), because it may be a multi line...
var retUrl = "{!Account.Link}"; 
var addZip = "{!Account.BillingPostalCode}"; 
var addCity = "{!Account.BillingCity}"; 
var addCountry = "{!Account.BillingCountry}"; 
var addState = "{!Account.BillingState}";

function loadAccountInfo() {
 var account = sforce.connection.retrieve(
  "Id, Name, BillingStreet, BillingPostalCode, BillingCity, BillingCountry, BillingState", "Account", ["{!Account.Id}"])[0];
 addStreet = account.get("BillingStreet"); // should not be a multi line

 var html = "<table width=70%><tr>";
 html += "<td width=35% nowrap valign=top class='dataLabel'>Account Name:</td>";
 html += "<td class='dataField'><a color='#FFFFFF' href='" + 
  retUrl + "' target=_blank>" + "{!Account.Name}" + "</a></td></tr><tr>";
 html += "<td class='bodyBold' colspan=5 nowrap>Address Information:</td>";
 html += "</tr><tr><td class='blackLine' colspan=5><img src='/s.gif'></td></tr><tr>";
 html += "<td width=35%  valign=top class='dataLabel'>Apply Address to Checked Contacts</td>";
 html += "<td valign=top colspan=4 class='dataField'>" + addStreet + "<br>";
 html += "{!Account.BillingCity}, ";
 html += "{!Account.BillingState} ";
 html += "{!Account.BillingPostalCode} <BR>";
 html += "{!Account.BillingCountry}</td>";
 html += "</tr></table>";
 document.getElementById("divAccountInfo").innerHTML = html;
} 

function cancelUpdate() { window.parent.parent.location.href = retUrl; }

function trim(string) { return string.replace(/(^\xA0*)|(^\s*)|(\s*$)/g,''); } 

function SelectChecked(form, element_name, value) { 
 var i = 0; for (i = 0; i < form.elements.length; i++) { 
  if (form.elements[i].name == element_name) { 
   form.elements[i].checked = value; 
  } 
 } 
} 

function IsChecked(form, element_name, value) { 
 var updateObjects = new Array();
 for (i = 0; i < form.elements.length; i++) { 
  if ((form.elements[i].name == element_name) && (form.elements[i].checked)) { 
   var contact = new sforce.SObject("Contact");
   contact.set("Id",form.elements[i].value);
   contact.set("MailingCity", addCity);
   contact.set("MailingStreet", addStreet);
   contact.set("MailingCountry", addCountry);
   contact.set("MailingPostalCode", addZip);
   contact.set("MailingState", addState);
   updateObjects.push(contact);
  } 
 } 

 try { 
  if (updateObjects.length > 0) { 
   var x = window.confirm("Are you sure you want to update "+ updateObjects.length +" record(s) —"); 
   if (x) { 
    var sr = sforce.connection.update(updateObjects); 
    window.parent.parent.location.href = retUrl; 
   } 
  } else { 
   alert("No Records will be updated"); 
   window.parent.parent.location.href = retUrl; 
  } 
 } catch (e) { alert('could not update contacts '+e); }
} 

//--> 
 </script>
<script language="javascript"> 
<!-- 
function setupForm() { //  Query to get Contact informaiont at this accont
 var qr = sforce.connection.query("Select Id, FirstName, LastName, MailingStreet, MailingCity, MailingPostalCode, MailingState, MailingCountry from Contact Where AccountId = '" + accId + "'");

 var docContents ="<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"list\" >";  
 docContents += "<tr class=\"headerRow\">"; 
 docContents += "<th class=\"booleanColumn\" scope=\"col\"><input name=\"\" type=\"checkbox\" id=\"\" onClick=\"javascript:SelectChecked(document.forms['Multi_Contact'],'ids',this.checked)\" value=\"\"></td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;First Name</td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;Last Name</td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;Street</td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;City</td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;Zip Code</td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;State</td>"; 
 docContents += "<th nowrap class=\"dataCell\"> &nbsp;Country</td>"; 
 docContents += "</tr>";  

 if (qr.size > 0) {
  var records = qr.getArray('records');
  for (var i=0;i<records.length;i++) {
   var row;
   if (i%2 == 0) {
    docContents += '<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even ">';  // TODO how to make this look good on both skins
   } else { 
    docContents += '<tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow odd ">';  // TODO how to make this look good on both skins
   } 
   contact = records[i];

var tabCol = [contact.get("Id"), contact.get("FirstName"), contact.get("LastName"), contact.get("MailingStreet"), contact.get("MailingCity"), contact.get("MailingPostalCode"), contact.get("MailingState"), contact.get("MailingCountry")];
   docContents += "<th class=\"booleanColumn dataCell\" scope=\"row\" ><input type=\"checkbox\" name=\"ids\" value=\"" + tabCol[0] + "\" id=\"ids\"> </td>"; 
   for (var j=1;j<tabCol.length;j++) {
    docContents += "<td class=\"dataCell\" >&nbsp;" + tabCol[j] + "</td>"; 
   } 
   docContents += "</tr>";
   
  }
  docContents += "</table>";
 }     
 document.getElementById("divTableHeader").innerHTML = docContents;
 
 docContents = ""; 
 docContents += '<INPUT class="button" id="button1" onclick="javascript: IsChecked(document.forms[\'Multi_Contact\'],\'ids\',this.checked)" type="button" value="Update" name="Button1">';
 docContents += '&nbsp;&nbsp;<INPUT class="button" id="button2" onclick="javascript: cancelUpdate()" type="button" value="Cancel" name="Button2">';
 document.getElementById("divButtons").innerHTML = docContents;
}
//--> 
</script>
</head>
<body vLink="#000000" aLink="#99cc00" link="#000000" onload="pageInit()" class="contact" >
 <FORM id="Multi_Contact">  
<div class="bPageTitle">
<div class="ptBody secondaryPalette">
<div class="content">
<img title="Contact" class="pageTitleIcon" alt="Contact" src="/s.gif"/>
<h1 class="pageType noSecondHeader">Update Contact Addresses</h1>
<div class="blank"> </div>
</div>
<div class="links">
<a title="Help for this Page (New Window)" href="javascript:openPopupFocusEscapePounds('/help/doc/user_ed.jsp–loc=help&target=contacts_merge.htm&section=Contacts', 'Help', 700, 600, 'width=700,height=600,resizable=yes,toolbar=yes,status=no,scrollbars=yes,menubar=yes,directories=no,location=no,dependant=no', false, false);">
</a>
</div>
</div>
<div class="ptBreadcrumb" >

</div>
</div>

<div class="bNext">
 <div class="withFilter">
  <div class="clearingBox"></div>
 </div>
</div>

 <div id="divAccountInfo"></div>

<div class="bPageBlock secondaryPalette">
 <div class="pbHeader">
  <table cellspacing="0" cellpadding="0" border="0">
  <tbody>
  <tr>
  <td class="pbTitle">
  </td>
  <td class="pbButton" id="divButtons" >
  </td>
  <td class="pbHelp">
  </td>
  </tr>
  </tbody>
  </table>
 </div>

 <div class="pbBody" id="divTableHeader" ></div>
 <div class="pbFooter secondaryPalette"><div class="bg"></div></div>
</div>


</FORM> </body>
</html>