function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matt Cooper 7Matt Cooper 7 

OnClick Javascript button to update status not working

I built out 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 chosen by Matt Cooper 7
Matt Cooper 7Matt Cooper 7
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'); 
}

 

All Answers

Neetu_BansalNeetu_Bansal
Hi Matt,

Have you create the custom button on the object 'Apttus__APTS_Agreement__c'. If yes, it should work as I cannot find any issue in the code.
If no, You need to query Apttus__APTS_Agreement__c fields to check in the if-else.

Let me know, if I can help you more.

Thanks,
Neetu
Matt Cooper 7Matt Cooper 7
Thanks Bansal, the button is on the Apttus__APTS_Agreement__c object.  Also, to help troubleshoot,  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 isn't linking to the record's id.
Neetu_BansalNeetu_Bansal
Hi Matt,

Just try by increasing the version and adding script for Apex. May be Apttus__APTS_Agreement__c object does not supported in previous versions.
{!REQUIRESCRIPT("/soap/ajax/34.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/34.0/apex.js")}
If you still not able to resolve, you can share your salesforce org login credentials, I will be able to look into it and help you.

Thanks,
Neetu
Matt Cooper 7Matt Cooper 7

Hi Neetu,

Increasing the version didn't seem to help.  What's 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.  Also, do you have any idea why it would be able to read the id, but not the Apttus__Status__c field?

Sadly, our org has some very sensitive information in it and I can't give you access.

Thanks,

Matt

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

 
This was selected as the best answer