[SalesForce] need help with onclick javascript button

I am working on a custom button on Account detail page which is an Onclick javascript button. I am using the following logic behind the button:

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

var acct = new sforce.SObject("Account"); 
acct.accountId = "{!Account.Id}"; 
acct.MCode = "{!Account.MCountry__c}"; 
acct.MNumber = "{!Account.MNumber__c}"; 


if (acct.MCode == null || acct.MNumber == null){ 
alert("Country and M Number fields are mandatory ") 
} else { 
var response = sforce.apex.execute("Mtest", 
"Mtestmethod", 
{accountId:accountId}); 
if (response == 'true'){ 
alert("Updated successfully.") 
window.location.reload(); 
} else { 
alert("Details could not be updated "); 
} 

The logic of this button is that when any of the MCountry__c and MNumber__c fields are empty , an alert message should be displayed "Country and M Number fields are mandatory". Else if these fields contain values, the control should pass to the apex class.

However, in my case when I click on this button from the Account detail page an error message appears stating that 'accountId' is not defined.

I have never worked on javascript before. Can someone please help what is missing or wrong here.

Best Answer

Something like that would be better:

var country = "{!Account.MCountry__c}"; 
var number = "{!Account.MNumber__c}"; 
if (country == null || number == null) {
    alert("Details could not be updated ");
}
Related Topic