0

I have a requirement that when User clicks on cancel button on a detail page the status field should be updated to "cancelled".I have implemented the code using visualforce page as suggested but when I add it detail page and click on cancel button visualforce page is opening instead of updating the field.

<apex:page StandardController="Invoice__c" extensions="CS_CancelInvoiceCtrExt" >
 <apex:form >
  <apex:pageBlock >
     <apex:pageBlockButtons >

         <apex:commandButton value="Cancel" onclick="Cancel();" id="button"/>

      </apex:pageBlockButtons>
   </apex:pageBlock>
 </apex:form>
</apex:page>

controller:

public with sharing class CS_CancelInvoiceCtrExt {

    public Invoice__c inv;

    public CS_CancelInvoiceCtrExt(ApexPages.StandardController controller) {
      //this.inv = (Invoice__c)controller.getRecord();
      this.inv = inv;
   }

    public void Cancel() {
        inv.Status__c = 'Cancellation Requested';
        update inv;

    }



}
1

Try Below piece of code instead of Onclick you need to put the action of commandButton

<apex:page StandardController="Invoice__c" extensions="CS_CancelInvoiceCtrExt" >
 <apex:form id="theForm">
  <apex:pageBlock >
     <apex:pageBlockButtons >

         <apex:commandButton value="Cancel" action="{!Cancel}" rerender="theForm" id="button"/>

      </apex:pageBlockButtons>
   </apex:pageBlock>
 </apex:form>
</apex:page>


public PageReference Cancel() {
   inv.Status__c = 'Cancellation Requested';
   update inv;
   return null; \\IF YOU WANT TO REDIRECT THEN SEND AN URL
}
| improve this answer | |
  • Its not working..... – anjali Jan 17 '17 at 9:58
  • Check the updated answer – RCS Jan 17 '17 at 10:05
1

You don't need the VF page here. Javascript will work for you.

{!requireScript("/soap/ajax/36.0/connection.js")}
var Invoice = new sforce.SObject("Invoice__c");
Invoice.id = //set Record Id here, potentially get it from the URL ?id= param

Invoice.field__c = '{!Invoice__c.id}';
Invoice.Status__c = 'Cancellation Requested';
sforce.connection.update([Invoice]); 
window.location.reload();

Use the Javascript button and use this code

If you want VF approach then

<apex:page StandardController="Invoice__c" extensions="CS_CancelInvoiceCtrExt" >
 <apex:form >
  <apex:pageBlock >
     <apex:pageBlockButtons >

         <apex:commandButton value="Cancel" action="{!Cancel}" id="button"/>

      </apex:pageBlockButtons>
   </apex:pageBlock>
 </apex:form>
</apex:page>

and Controller

public with sharing class CS_CancelInvoiceCtrExt {

    public Invoice__c inv;

    public CS_CancelInvoiceCtrExt(ApexPages.StandardController controller) {
      //this.inv = (Invoice__c)controller.getRecord();
      this.inv = inv;
   }

    public Pagereference Cancel() {
        inv.Status__c = 'Cancellation Requested';
        update inv;
        return new pagereference('\'+inv.Id);
    }
}

You need to use action attribute to call controller method. If you don't want your user to click the button then remove the button from page and call the action from your page action attribute.

| improve this answer | |
  • According to design ,strictly I need to build it using visualforce page. – anjali Jan 17 '17 at 9:57
  • @anjali check my updated answer. – Tushar Sharma Jan 17 '17 at 10:14
  • 1
    you need to make the method as PageReference – RCS Jan 17 '17 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.