I have a custom button in a layout created for a custom object. Upon clicking, if some conditions are fulfilled, I would like to execute some code and otherwise I want to display a popup. I use the OnClick functionality provided by Salesforce and I inserted the following code:
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
if(!("{!Invoice__c.RecordType}" != "2 – Invoice" || "{!Invoice__c.Invoice_Status__c}" != "Credited"))
{
var newInvoiceId = sforce.apex.execute("InvoiceCloning","cloneInvoice", {invoiceId:"{!Invoice__c.Id}"});
window.location.href = "/"+newInvoiceId;
}
else
{
alert("Cannot create credit note");
}
The conditions look pretty straightforward and also the if - else statement is nothing transcendental. However just the code inside the else is executed. I tried to debug with Chrome and I found out that the values are correctly populated and I do not understand why the expression in the if statement evaluates to false:
<input value="Create Credit" class="btn" name="create_credit" title="Create Credit" type="button" onclick="if (window.invokeOnClickJS_00b8A000000Dhcm) window.invokeOnClickJS_00b8A000000Dhcm(this); else if (parent.window.invokeOnClickJS_00b8A000000Dhcm) parent.window.invokeOnClickJS_00b8A000000Dhcm(this); return false">
Code that exectues Onclick:
window.invokeOnClickJS_00b8A000000Dhcm = function(element) {
function getRecordIds(keyPrefix) { return Scontrol.prototype.getSelectedRecordIdsFromForm(element.form, keyPrefix); }
element.invokeAction = function() {
var scriptLibrary = [];
scriptLibrary.push("/soap/ajax/26.0/connection.js");
scriptLibrary.push("/soap/ajax/29.0/apex.js");
Util.loadScripts(scriptLibrary, 0, function() {
if (typeof(sforce) != 'undefined' && typeof(sforce.connection) != 'undefined') { sforce.connection.sessionId = '00D8A0000000POh!AQkAQGNGOdzEJLjjmvJrMdCB62tq9Z_jczpq6u4UKrCGycXIZfTjxYxFzk7fBUaMjB8ZVFDK.jOuKQ6UG6JGQyC_5chaaC_.'; }
try { Util.stripCustomFunctionFromObjectPrototype(Array);eval(' \r\n \r\n\r\nif((\"2 - Invoice\" == \"2 – Invoice\" && \"Credited\" == \"Credited\"))\r\n{\r\n\r\nvar newInvoiceId = sforce.apex.execute(\"InvoiceCloning\",\"cloneInvoice\", {invoiceId:\"a7q33000000L7zl\"});\r\nwindow.location.href = \"/\"+newInvoiceId;\r\n\r\n}\r\n\r\nelse\r\n{\r\nalert(\"Cannot create credit note\" + \"2 - Invoice\" + \"Credited\");\r\n}') }
catch (e) { alert('A problem with the OnClick JavaScript for this button or link was encountered:\n\n' + (e.message ? e.message : e)) }
finally { Util.restoreCustomFunctionsToObjectPrototype(Array);}
});
};
element.invokeAction();
}
What am I doind wrong? Thanks