1

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');
}
1
  • Are you sure that {!Apttus__APTS_Agreement__c.Apttus__Status__c} is evaluated as Internal Review? Check in JS console.
    – Saroj Bera
    Commented Aug 20, 2015 at 16:26

2 Answers 2

1

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'); 
}
0

The easiest way to troubleshoot this would be to throw a couple of alert() statements in the script so that you can see what the values are that are being evaluated.

For instance:

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

// debug the values
alert('Apttus Status is: ' + {!Apttus__APTS_Agreement__c.Apttus__Status__c});
alert('if statement result: ' + ('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Internal Review'));

if ('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Internal Review') {
4
  • When I set the last else alert to: alert('This agreement must first be sent for legal review agmtid=' + agmt.id + ' status= ' + '{!Apttus__APTS_Agreement__c.Apttus__Status__c}' + ' if statement result: ' + ('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' == 'Internal Review')); It came back with agmtid = undefined, status = blank, if statement result = false. I feel like this is all dependent on the agmt.id not setting, but not sure why it wouldn't.
    – Matt
    Commented Aug 20, 2015 at 17:39
  • is this on a create page where the record hasn't been saved yet or are you on an edit page of an existing record?
    – Mark Pond
    Commented Aug 20, 2015 at 18:22
  • I'm just on the record page after the record has been saved. It's a detail page button.
    – Matt
    Commented Aug 20, 2015 at 19:09
  • Also, I tried ncreasing the version didn't seem to help. What's also interesting is if I replace agmt.id with '{!Apttus__APTS_Agreement__c.Id}' in the alert, it shows the record's id. So for some reason it's not setting the agmt.id to the record's id with the agmt.Id = '{!Apttus__APTS_Agreement__c.Id}'; line. Do you have any idea why it would be able to read the id, but not the Apttus__Status__c field?
    – Matt
    Commented Aug 20, 2015 at 19:10

You must log in to answer this question.

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