[SalesForce] OnClick Javascript Button Not Working

I built a custom button that should run some JavaScript to update a field value if certain conditions are met. However, when I test the button, the else clause always runs.

Any help on what is wrong with the javascript would be much appreciated:

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

var agmt = new sforce.SObject("Apttus__APTS_Agreement__c");
agmt.Id = '{!Apttus__APTS_Agreement__c.Id}';

if ('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Internal Review') {
    agmt.Apttus__Status__c = 'Other Party Review';
    result = sforce.connection.update([agmt]);
    window.location.reload();
} else if ('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Other Party Review') {
    alert('This agreement has already been sent for review');
} else if ('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Activated' || '{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Fully Signed') {
    alert('This agreement is fully signed');
} else {
    alert('This agreement must first be sent for legal review');
}

Best Answer

I was able to resolve the issue by querying for the Apttus__Status__c value of the record using the Id. I still have no idea why I would be able to retrieve the Id but not the Apttus__Status__c field, but oh well. Here's the full code:

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

var agmt = new sforce.SObject("Apttus__APTS_Agreement__c");
agmt.Id = '{!Apttus__APTS_Agreement__c.Id}'; 

var agmtStats = sforce.connection.query("SELECT Apttus__Status__c FROM Apttus__APTS_Agreement__c WHERE id = '{!Apttus__APTS_Agreement__c.Id}'");

records = agmtStats.getArray("records");
var agmtStat = records[0].Apttus__Status__c;

if(agmtStat == 'Internal Review'){
agmt.Apttus__Status__c = 'Other Party Review'; 
result = sforce.connection.update([agmt]); 
window.location.reload(); 
} 

else if(agmtStat == 'Other Party Review'){ 
alert('This agreement has already been sent for review'); 
} 

else if(agmtStat  == 'Activated' || agmtStat  == 'Fully Signed' ){ 
alert('This agreement is fully signed'); 
} 

else{ 
alert('This agreement must first be sent for legal review'); 
}
Related Topic