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
Missy LongshoreMissy Longshore 

Button for list view not working but passed syntax check

Hi, I created a custom button on Contacts to display on list views to pull out email addresses. I tried updating it to include Assistant Email addresses with the text in bold and it's no longer working. It passed the syntax check but when I click the button in Salesforce on a list view I get the error message A problem with the OnClick JavaScript... 'expected a ')'.' Any ideas where I missed a closing parentheses? Thanks for your help! 

Here's the code:

{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")} 

var records = {!GETRECORDIDS($ObjectType.Contact)}; 
var recipients = []; 

if (records[0] == null) { 
  alert("Please select at least one row."); 

else { 
  // Iterate through records 
  for (var n=0; n<records.length; n++) { 
    // retrieve Contact based on the ID in records 
    var contactLookup = 
sforce.connection.retrieve("Email,Assistant_Email__c","Contact",[records[n]]);
    if(contactLookup[0].Email)
    if(contactLookup[0].Assistant_Email__c) { 
      // email is not empty so add to list 
      recipients.push(contactLookup[0]); 
    } 
  } 

  // Build the string for a mass email 
  // Change the seperator between a semi-colon or comma, depending on 
//email client 
  var toEmailString = ""; 
  for (var n=0; n<recipients.length; n++) { 
    toEmailString = toEmailString + recipients[n].Email + "; "; 
  } 

  // Remove last comma or semi-colon from string 
  toEmailString = toEmailString.slice(0,toEmailString.length-2); 

   //alert("to:"+toEmailString); 
   prompt("Copy and paste this string into your email To: field", 
toEmailString); 

}
Best Answer chosen by Missy Longshore
Jeremy BergtholdtJeremy Bergtholdt
This version is even better -- it's super FAST compared to the code above.
 
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")} 

var records = {!GETRECORDIDS($ObjectType.Contact)}; 
var recipients = []; 
var ids = [];

if (records[0] == null) { 
	alert("Please select at least one row.");
} else {
	// Iterate through records 
	for (var n=0; n<records.length; n++) { 
		// add the contact ID to the ids variable
		ids.push(records[n]);
	}
	// retrieve Contact based on the ID in records 
	var contactLookup = 
		sforce.connection.retrieve("Email,Assistant_Email__c","Contact",ids); 
		
	for (var n=0; n<ids.length; n++) { 
		if(contactLookup[n].Email || contactLookup[n].Assistant_Email__c){ 
			// email is not empty so add to list 
			recipients.push(contactLookup[n]); 
		}
	}

	// Build the string for a mass email 
	// Change the separator between a semi-colon or comma, depending on
	// email client 
	var toEmailString = ""; 
	for (var n=0; n<recipients.length; n++){ 
		if(recipients[n].Email){
			toEmailString = toEmailString + recipients[n].Email + "; ";
		}
		if(recipients[n].Assistant_Email__c){
			toEmailString = toEmailString + recipients[n].Assistant_Email__c + "; "; 
		}
	}

	// Remove last comma or semi-colon from string 
	toEmailString = toEmailString.slice(0,toEmailString.length-2); 

	//alert("to:"+toEmailString); 
	prompt("Copy and paste this string into your email To: field", toEmailString); 

}

One limitation: the code only pulls around 85 email addresses. If you have more than that it seems that the rest are just ignored. I don't know why that limitation exists -- but I tested the old code (from a few weeks ago) too and it had the same limitation. Maybe it's a JavaScript string length limit?

Jeremy

All Answers

sandeep sankhlasandeep sankhla
Hi Missy,

Try to replace this line and check if it still gives the error..just to check 
toEmailString = toEmailString + recipients[n].Email +',';


Thanks,
Sandeep
Jeremy BergtholdtJeremy Bergtholdt
Hi Missy -- I've tested the code below and it works.
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")} 

var records = {!GETRECORDIDS($ObjectType.Contact)}; 
var recipients = []; 

if (records[0] == null) { 
	alert("Please select at least one row."); 
} else { 
	// Iterate through records 
	for (var n=0; n<records.length; n++) { 
		// retrieve Contact based on the ID in records 
		var contactLookup = 
		sforce.connection.retrieve("Email,Assistant_Email__c","Contact",[records[n]]); 
		if(contactLookup[0].Email || contactLookup[0].Assistant_Email__c){ 
			// email is not empty so add to list 
			recipients.push(contactLookup[0]); 
		}
	} 

	// Build the string for a mass email 
	// Change the separator between a semi-colon or comma, depending on
	// email client 
	var toEmailString = ""; 
	for (var n=0; n<recipients.length; n++){ 
		if(recipients[n].Email){
			toEmailString = toEmailString + recipients[n].Email + "; ";
		}
		if(recipients[n].Assistant_Email__c){
			toEmailString = toEmailString + recipients[n].Assistant_Email__c + "; "; 
		}
	}

	// Remove last comma or semi-colon from string 
	toEmailString = toEmailString.slice(0,toEmailString.length-2); 

	//alert("to:"+toEmailString); 
	prompt("Copy and paste this string into your email To: field", toEmailString); 

}
The code runs slowly -- it took about 10 seconds to run on 50 records -- so be patient after clicking the button that runs the code.

Jeremy
I help people use Salesforce.
 
Jeremy BergtholdtJeremy Bergtholdt
This version is even better -- it's super FAST compared to the code above.
 
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")} 

var records = {!GETRECORDIDS($ObjectType.Contact)}; 
var recipients = []; 
var ids = [];

if (records[0] == null) { 
	alert("Please select at least one row.");
} else {
	// Iterate through records 
	for (var n=0; n<records.length; n++) { 
		// add the contact ID to the ids variable
		ids.push(records[n]);
	}
	// retrieve Contact based on the ID in records 
	var contactLookup = 
		sforce.connection.retrieve("Email,Assistant_Email__c","Contact",ids); 
		
	for (var n=0; n<ids.length; n++) { 
		if(contactLookup[n].Email || contactLookup[n].Assistant_Email__c){ 
			// email is not empty so add to list 
			recipients.push(contactLookup[n]); 
		}
	}

	// Build the string for a mass email 
	// Change the separator between a semi-colon or comma, depending on
	// email client 
	var toEmailString = ""; 
	for (var n=0; n<recipients.length; n++){ 
		if(recipients[n].Email){
			toEmailString = toEmailString + recipients[n].Email + "; ";
		}
		if(recipients[n].Assistant_Email__c){
			toEmailString = toEmailString + recipients[n].Assistant_Email__c + "; "; 
		}
	}

	// Remove last comma or semi-colon from string 
	toEmailString = toEmailString.slice(0,toEmailString.length-2); 

	//alert("to:"+toEmailString); 
	prompt("Copy and paste this string into your email To: field", toEmailString); 

}

One limitation: the code only pulls around 85 email addresses. If you have more than that it seems that the rest are just ignored. I don't know why that limitation exists -- but I tested the old code (from a few weeks ago) too and it had the same limitation. Maybe it's a JavaScript string length limit?

Jeremy
This was selected as the best answer
Jeremy BergtholdtJeremy Bergtholdt
Due to the limitation I note above, I'd limit list views to displaying 50 records (or 25 records, if most records will have an Email AND Assistant_Email), and click the button that runs this code for each page of records separately.
Jeremy BergtholdtJeremy Bergtholdt
GOOD NEWS -- Further testing revealed that the limitation I note above is browser-specific. Avoid Google Chrome -- it has the issues noted above. Internet Explorer works for at least 200 email addresses, but it becomes unreliable above 200.

Mozilla Firefox is the most capable browser. Salesforce only allows me to select 200 records at a time in a Contact list view, and when I tested 200 records, each with 2 email addresses, the code worked beautifully -- FAST (almost instantaneous), and 400 email addresses accounted for.

So... I recommend ONLY using Mozilla Firefox with the functionality in this code.
Missy LongshoreMissy Longshore
Thank you @Jeremy Bergtholdt - you are awesome!!! It's working great!