+ Start a Discussion

Visualforce - Custom Button Problem



I've recently overridden the standard "View" page for my custom object with a visualforce page.


On the Visualforce page, I have a custom button that executes the code below.  The button has worked in the past, but on the visualforce page, it gives me an error related to the "sforce.connection.update(records)" statement below.


Is there a way around this to continue using this custom button, or is it best to have some sort of new button functionality as part of the Visualforce page?





{!REQUIRESCRIPT('/soap/ajax/8.0/connection.js')} var x=window.confirm('Are you sure you want to do this now?') if (x) { var records = []; var d = new sforce.SObject('CustomObject__c'); d.id ='{!CustomObject__c.Id}'; d.Some_Flag__c = 'true'; d.Some_String__c = 'update'; records.push(d); result = sforce.connection.update(records); window.location.reload(); }





When your button worked in the past, was it within the context of a Visualforce page?  The reason I ask is that you are using javascript code that likely was within the context of a S-Control before, and now that it is within a VF page you may not have all the requisite elements available.  If this is just a snippet of the code that might explain, however if this is the entirety of the code, then a few areas to focus in on are:


1. Ensure that the field you are referencing in {!CustomObject__c.id} is in fact what you expect it to be (maybe insert an alert('CustomObject__c.id value=' + {!CustomObject__c.Id});  etc.


2. Or - I would suggest you change this code to a method within either a custom controller or a controller extension that would allow you to utilize a non javascript approach to solution the problem.  The controller action method would be along the same lines as the code you have.  I have included some pseudo code (not tested for validity but representative of the logic you would pursue).   There are other button alternatives that you could also look into that have different methods of passing information to a controller method.


public string selectedId;

public string selId
            return selectedId;
            selectedId = value;   
// Button action method within a custom controller or controller extension 

public PageReference myXXButton()
       CustomObject__c d = new CustomObject__c;

       d.id = selectedId;                 // passed in via apex:param to selId get/set

       d.Some_Flag__c = 'true';
       d.Some_String__c = 'update';
       upsert d;


VF code to reference the button:


 <apex:pageBlockButtons >
   <apex:commandButton onclick="if (!confirm('Are you sure you want to do this now?')) return false;" action="{!myXXButton}" value="Do some Action">
      <apex:param value="{!CustomObject__c.id}" name="id" assignTo="{!selId}" >



Hi wsyucott,

Thanks for the response.  The buttons were previously working on a standard page layout, and stopped working only when the Visualforce page was introduced.  They do execute some OnClick Javascript, so I suspect that is the conflict, but hoping there is a clean way around it.


Otherwise, I'll look closer at the suggested solution below (with the functions within my extension class).