+ Start a Discussion
Simon WhightSimon Whight 

Javascript - error handling an undefined substring error

I know why it is happening, my array is empty. What I want to be able to do is provide a logica error for the end user, not a browser parped javascript technical error bubble.

Here is the code:

result1 = sforce.connection.query("SELECT ContractNumber FROM Contract where Id = \'" + idToUse + "\'");

records1 = result1.getArray("records");

for (var i = 0; i < records1.length; i++) {
    var record1 = records1[i];
    contrId1 = record1.ContractNumber.substring(0, 15);
}
The SoQL will occasionally be empty, so I need to flag some sort of "please make sure Contract Number has been entered" and exit the code. Can anyone help?

RadnipRadnip
You could just add a Salesforce validation rule? But assuming this is part of other code and you cant? but thats the easiest way. Otherwise just add an 

if (record1.ContractNumber == ''){
   alert('please make sure Contract Number has been entered');
}


is that what you mean?
Naveen Rahul 3Naveen Rahul 3

The below code will work like what you expecting

 

result1 = sforce.connection.query("SELECT ContractNumber FROM Contract where Id = \'" + idToUse + "\'");

records1 = result1.getArray("records");

if(records1.length > 0){
	for (var i = 0; i < records1.length; i++) {
		var record1 = records1[i];
		contrId1 = record1.ContractNumber.substring(0, 15);
	}	
}
i have highlighted them.
Simon WhightSimon Whight
How odd. Even though the result of the query is null, the array still has a length of 1. As such, the if statement still executes.
Naveen Rahul 3Naveen Rahul 3
Simon your result is returning atleast 1, i checked here its working corectly.i mean when no data is retured,it populates the count as 0 and not 1.
please re investigate,might be missing something.