?
Solved

Error: Array element at 12 is null

Posted on 2009-12-22
2
Medium Priority
?
216 Views
Last Modified: 2012-05-08
I am getting the following error within the javascript shown below in the try/catch section:

Array element at 12 is null.

I've examined the records and I don't see why the array element 12 is null.  It is the last element in the array and from what I see it is not null.

Can anyone point out the error in my code or what I might be missing?

Thanks in advance for any help.
<html> 
<head> 
<script type="text/javascript" src="/js/functions.js"></script> 
<script src="/soap/ajax/12.0/connection.js"></script> 
<script src="https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js" type="text/javascript"></script> 
<script language="javascript1.2" type="text/javascript"> 

// Update Related Records 

function updateRelatedRecords() { 
//Get Records - Contact Roles for Account 
var AccountId = "{!Account.Id}"; 
var ParentId="{!Account.ParentId}"; 
var childAccounts; 
var parentContactRoles; 
var childContactRoles; 
var hasParentContactRoles = false; 
var hasChildAccounts = false; 
var strSQL; 
var result; 

//If the Account is a child Account which means it has ParentId then 
//nothing should happen 

if(ParentId != ''){ 
alert("This is a child Account"); 
} else { 

strSQL="Select Id, AccountId,Role,ContactID From AccountContactRole where AccountId='{!Account.Id}'"; 
result = sforce.connection.query(strSQL); 
parentContactRoles = result.getArray("records"); 

//If the Parent Account has no Contact Roles then it should 
//not do anything No update or insert to anything not even Child records 

if(parentContactRoles.length==0){ 
alert("There are no Contact Roles for this Account '{!Account.Name}'"); 
} else { 
hasParentContactRoles = true; 
} 


//If the Parent Account has no child Accounts then 
//do not do anything either 

strSQL="Select Id,Name from Account where ParentId='{!Account.Id}'"; 
result = sforce.connection.query(strSQL); 
childAccounts = result.getArray("records"); 
if(childAccounts.length==0){ 
alert("There are no child Accounts for '{!Account.Name}'"); 
} else { 
hasChildAccounts = true; 
} 

if(hasParentContactRoles && hasChildAccounts){ 
var newContactRoles = []; 
var contactRoleExists = false; 
var i,j,k; 
for (i in childAccounts){ 
//Get Contacts Roles for each child account 
strSQL="Select Id, AccountId, Role, ContactId From AccountContactRole where AccountId=" + "'" + childAccounts[i].Id + "'"; 
result = sforce.connection.query(strSQL); 
childContactRoles = result.getArray("records"); 

for(j in parentContactRoles){ 
contactRoleExists = false; 
for(k in childContactRoles){ 
if(parentContactRoles[j].Role == childContactRoles[k].Role) {contactRoleExists = true;} 
} 
if(!contactRoleExists){ 
var newContactRole = new sforce.SObject("AccountContactRole"); 
newContactRole.AccountId = childAccounts[i].Id; 
newContactRole.ContactId = parentContactRoles[j].ContactId; 
newContactRole.Role = parentContactRoles[j].Role; 
newContactRoles.push(newContactRole); 
} 
} 
} 
if(newContactRoles.length > 0){ 
try 
{ 
var index = 0; 
while(index < newContactRoles.length) { 
var tmpArray = new Array(); 
var count = 0; 
for(count = 0; count < 200; count++) { 
tmpArray[count] = newContactRoles[index]; 
index++; 
} 
result = sforce.connection.create(tmpArray); 
} 
result = sforce.connection.create(newContactRoles); 
} catch(e) 
{ 
alert(e.faultCode?e.faultCode:e.message?e.message:e) 
} 
document.write(newContactRoles.length + " new Contact Roles were created in child accounts."); 
} else { 
document.write("No Contact Roles assigned. All child accounts already have all roles defined."); 
} 
} 
} 
} 
</script> 
</head> 
<body onload="updateRelatedRecords()";> 
</body> 
</html>

Open in new window

0
Comment
Question by:-Dman100-
2 Comments
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26105839
try this

(swap line 88 & 89)

I could not get what the inner for loop is for... looks like there is a logic problem here...
if(newContactRoles.length > 0){ 
try { 
	var index = 0; 
	while(index < newContactRoles.length) { 
		var tmpArray = new Array(); 
		var count = 0; 
	
		for(count = 0; count < 200; count++) { 
			tmpArray[count] = newContactRoles[index]; 
		} 

		index++; 
		result = sforce.connection.create(tmpArray); 
	} 
	result = sforce.connection.create(newContactRoles); 
} catch(e) { 
	alert(e.faultCode?e.faultCode:e.message?e.message:e) 
} 

Open in new window

0
 

Author Comment

by:-Dman100-
ID: 26105997
Hi HainKurt,

Thanks for your help.  I moved the index++ increment line outside of the the inner loop and that solved the problem.

The reason for the inner loop is because In some cases the code processes more than 200 records, but the limit for AJAX calls is 200, so I had to create an inner loop that limited the call to 200 records at a time until newContactRoles length is reached.

Again, thank you for your help.  I sincerely appreciate it.
Regards,
-D
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question