I created a custom button with the send email features, so if the margin<0.4) show alert, than can't send the email. I created the button but it doesn't work.

Somebody can help me?


var criteria1 = "SELECT id FROM OpportunityLineItem WHERE margin__c < 0.4 limit 1";

 records = sforce.connection.query(criteria1);

 records1 = records.getArray("records");

if(records1.length > 0){  
  alert("Before send email need approva process");
} else {
  location.replace('/email/author/emailauthor.jsp?retURL=/{!Opportunity.Id}&p3_lkid={!Opportunity.Id}&rtype=003&&[email protected]&template_id=0000000000000&new_template=true ');

  alert('An Error Occured' +e)
  • 3
    See the information about using the JavaScript console in How do I start to debug my own Visualforce/JavaScript?. If you get rid of the blank lines and indent the code you'll see that the last 3 lines shouldn't be there.
    – Keith C
    Oct 20, 2014 at 17:57
  • Thanks for the help Keith C. I try that and show me this warning. 'Attr.nodeValue' is deprecated. Please use 'value' instead. And this error. Refused to set unsafe header "User-Agent" connection.js:598send connection.js:598sforce.SoapTransport.send connection.js:1017sforce.Connection._invoke connection.js:1630sforce.Connection.invoke connection.js:1569sforce.Connection.query connection.js:1257(anonymous function) VM8329:4element.invokeAction 00611000006uOxS:172window.invokeOnClickJS_00b11000000IERF 00611000006uOxS:175onclick I can't understand how can i fix that.
    – lisyy
    Oct 21, 2014 at 9:00
  • 3
    Those errors appear to be in the Salesforce code that you pull in with the first line of your script. Try replacing the 22.0 with 32.0 so that you get the latest version of the Salesforce code that may produce less complaints.
    – Keith C
    Oct 21, 2014 at 9:54

2 Answers 2


I think you could be missing a file {!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}. You add this file in your javascript, It will work.

  • It looks like the op is just using the standard query call and isn't using any custom defined webservices. That script shouldn't be necessary.
    – martin
    Oct 20, 2016 at 2:47
  • I see, You're right.
    – Peter Dong
    Oct 20, 2016 at 2:55

Can you change the url like below.


And if you will not gonna add template you can remove "template_id"

/_ui/core/email/author/EmailAuthor?p3_lkid={!Opportunity.Id}&retURL={!Opportunity.Id}&rtype=003&&[email protected]&new_template=true

You must log in to answer this question.

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