0

Users receive an error when clicking a custom button when using Internet Explorer web browser of;

"A problem with the ONClick JavaScript for this button or link was encountered:

Object doesn't support property or method 'startsWith'"

Note that this error only occurs when using Internet explorer.

The code for my custom button is below. Is there anything I can do to get this working?

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

var r = confirm("Warning: this will make the R&D Project or Service  available for Salesforce Opportunities, once they are added to a price list. Do  you wish to proceed?"); 
if(r==true) { 

var result = sforce.apex.execute("availableForOpportunities","convertToOpportunity", 

{product2:"{!Product2.Id}"}); 
location.reload = '/'+result[0].Id+'/e'; 

// Check result in console window 
   console.log(result[0]); 

// If this result starts with a string equal to the id key for the object type you are accessing, relocate the browser to that page 

if (result[0].startsWith('001')) { 
location.reload = '/' + result[0] + '/e'; 
} else { 
// Otherwise, show the error message to the viewer 
alert(result[0]); 
location.reload(true) 
}
}

1 Answer 1

2

Internet Explorer doesn't support the Javascript function startsWith yet. You can replace it with:

if (result[0].lastIndexOf('001', 0) === 0) {

The trick here is that the function lastIndexOf returns the last position of the substring found. But if you specify the index 0 as second parameter, it will start searching at position 0 but searching backwards. So it will return 0 if the string starts with the substring.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .