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 

Custom button not updating field values.

Hi,

I have created a custom button with the intent that it be used to update a field value.  I have built buttons like this before that have worked successfully, but for some reason this button is not updating any field values.  The alert I setup works, and the page reloads as the code tells it to do.  The only thing that doesn't occur is the field value updating.  Can anyone please help me figure out what is going wrong?

Here are two versions of the code that I built:

VERSION 1:
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}
{
var agmt = new sforce.SObject("Apttus__APTS_Agreement__c");
var agmtId = "{!Apttus__APTS_Agreement__c.Id}";
var status = "Submitted Request";

agmt.Id = agmtId;

if('{!Apttus__APTS_Agreement__c.Apttus__Status__c}' != 'Request'){
alert('This agreement has already been sent to Tax.');
}

else{
agmt.Apttus__Status__c  = status;
result = sforce.connection.update([agmt]);
window.location.reload();
}}

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

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

if(agmt.Apttus__Status__c != 'Request'){
alert('This agreement has already been sent to Tax.');
}

else{
agmt.Apttus__Status__c  = 'Submitted Request';
result = sforce.connection.update([agmt]);
window.location.reload();
}

Thanks!
Best Answer chosen by Matt Cooper 7
Matt Cooper 7Matt Cooper 7
I ended up adding code to show me whether there were errors being caused while trying to update the value.  It ended up being that there was a workflow rule that was causing errors when trying to save the record.  I fixed the workflow rule and then the button ended up working.  Here is the code I added to view the error:

result = sforce.connection.update([agmt]);
if (result[0].getBoolean("success")) {
    alert("account with id " + result[0].id + " updated");
  } else {
    alert("failed to update account " + result[0]);
  }

All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
It seems that you are using a relationship field to update the field via your custom button code which is Apttus__APTS_Agreement__c.Apttus__Status__c
as it's like a refrence field so it should be like Apttus__APTS_Agreement__r.Apttus__Status__c and please make sure you are using proper case in field Api name with the Nampespace as javascript is case sensitive.
Hope it will help you
Matt Cooper 7Matt Cooper 7
Apttus__APTS_Agreement__c is the API name for the custom object that the field Apttus__Status__c is located on.  I thought that __r should be utilized when accessing a custom relationship (i.e. field on detail record) where you first have to reference the master record's object, then the detail record's object, and then the field.  As I am just attempting to update the value of a field on the object where the button is located, I was pretty sure that the object's API name should end in __c.  Is this not correct?
Matt Cooper 7Matt Cooper 7
What's also strange is that when I take a button that works fine on another custom object and clone it (changing values to reflect object change) the button fails to work.  Is there some reason to explain why this would be happening?
Matt Cooper 7Matt Cooper 7
I added an alert to the above code in the else section to make sure that it was processing that.  The alert pops up fine, but the field does not update.  Does anyone have any idea why that functionality in particular would not be working?
Matt Cooper 7Matt Cooper 7
I ended up adding code to show me whether there were errors being caused while trying to update the value.  It ended up being that there was a workflow rule that was causing errors when trying to save the record.  I fixed the workflow rule and then the button ended up working.  Here is the code I added to view the error:

result = sforce.connection.update([agmt]);
if (result[0].getBoolean("success")) {
    alert("account with id " + result[0].id + " updated");
  } else {
    alert("failed to update account " + result[0]);
  }
This was selected as the best answer