2

I am trying to create a Visualforce page that intercepts the "New Case" button on the account when billing status = cancelled and returns an error that a case can not be created. If billing status <> cancelled, then the New Case page should load like normal.

When I use the code below, I end up basically cycling through the New Case creation. I'm brought to the record type select page, then when I choose a record type it brings be back to the record type select page and then proceeds like normal. Ideally, I shouldn't be coming to the record type select page at all if the billing stats = cancelled.

I believe I've isolated the error down to the field criteria itself. If I flip the logic and use billing status <> cancelled, I'm brought to the error page that should be displayed when billing status = cancelled.

I am wondering if I am running into an issue with the Case Standard Controller trying to reference a custom field on the related account. I've created similar use cases using the Profile as the criteria in the IF statement without issue.

Any guidance on this would be very helpful! THanks!

<apex:page standardController="Case"
action="
  {!
  IF(case.Account.Billing_Status__c == 'Cancelled', 
       URLFOR($Page.CasePageMessenger, case.id,[id=case.id],FALSE),
       URLFOR($Action.Case.NewCase, case.id,[retURL="/001"], TRUE)
    )
  }">
<apex:pageMessage severity="ERROR" 
    strength="3" 
    title="Case Create" 
    summary="This Account is cancelled and can not receive support.">
</apex:pageMessage>
</apex:page>
3
  • Erik how are you accessing the VF page. You are basing it on standard controller="case" which means you need to provide a ?id="salesforce_15_digit_case_id" after the page name. /apex/your_page_name?id="salesforce_15_digit_case_id" to test this. Even if you do this you will end up with a soql error, let me know if you can get the first part taken care, I will help you on the SOQL part
    – Rao
    Commented Mar 31, 2016 at 20:58
  • The problem the OP has is that there is no ID to use here... since he is creating a new record.... Commented Mar 31, 2016 at 23:39
  • Rao - Thanks for the response. I am indeed using a standard controller for Case. I believe Sebastian is right though in that I can't reference an ID because this is happening before creation. No ID to reference.
    – Erik Eitel
    Commented Apr 1, 2016 at 13:21

2 Answers 2

1

I would do the following:

  1. Create the Page/Controller below
  2. Change your button to a URL button and point it to this page (either using URLFOR or /apex/CasePageMessenger), pass the account id as a parameter named accountid

Note: This will only stop you if you click this button first. If you want a sure-fire method to avoid cases being created for accounts with the "wrong" status, then use a trigger.... that will stop API created cases, or cases where the user did not click this button to create them

Code:

Controller:

public class CasePageMessengerController {
   private final string accountId;

   public CasePageMessengerController() {
        this.accountId = ApexPages.CurrentPage().getParameters('accountId');
   }

   public PageReference redirectOrNot() {

       Account acc = [select Billing_Status__c from Account where Id =:accountId limit 1;
       if (acc == null) {
         return null;
       }
     if (acc.Billing_Status__c == 'Cancelled') {
       return null;
     } else {
       Case c = new Case();

       PageReference pr = (new ApexPages.StandardController(c)).New();
       pr.getParamters().put('retURL', '/001');
       pr.setRedirect(true);
       return pr;
     }
   }

}

Page:

<apex:page standardController="Case" extension="CasePageMessengerController"
action="{!redirectOrNot}">

  <apex:pageMessage severity="ERROR" 
      strength="3" 
      title="Case Create" 
      summary="This Account is cancelled and can not receive support.">
  </apex:pageMessage>

</apex:page>
3
  • Thanks! I gave this a try an am getting an error on line 11. if (acc == null) { Error: Unexpected token: If at line 11 column 7
    – Erik Eitel
    Commented Apr 1, 2016 at 13:29
  • I forgot to close the square bracket at line 10. Commented Apr 1, 2016 at 14:17
  • Thanks, Sebastian. Looks like I have one more. Same error on line 19. for 'New'
    – Erik Eitel
    Commented Apr 1, 2016 at 15:15
0

I'm assuming that Billing_Status__c is a picklist; have you tried replacing your IF condition with the equivalent ISPICKVAL() formula?

1
  • I would say his code has more issues than that, and you can check the value of picklist as the OP did nothing wrong with that.
    – Rao
    Commented Mar 31, 2016 at 21:08

You must log in to answer this question.

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