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
3C3C 

Custom "New Case" Button w/ VF Page

We have a visualforce page replacing the "new" button on cases. It references a controller extension. The case will only save if the "new case" button is clicked from the account record. If it is clicked from the cases tab or another object, nothing happens. Any ideas on how to make the case record savable regardless of where its created?

public with sharing class CaseContExt
{
    public AccountModel am{get; private set;}
    public Casemodel cm {get; private set;}
    private final Account a;
    private Case c;
    private final String caseID;
    
    public CaseComment caseComment {get; set;}
    
    public Boolean isPortal{get; private set;}
    public Boolean isChangeAsset {get; private set;}
    public Boolean useDefaultAssignmentRules {get; set;}
    
    //public String entitlementAlert {get; private set;}
    public Boolean customization {get; set;}
    
    private Boolean initialChangeAssetLoad {get; set;}
    
    public CaseContExt(ApexPages.StandardController std)
    {
     
        caseID = ApexPages.currentPage().getParameters().get(CONSTANTS.QRY_STRING_CASE_ID);
        System.debug('\n\ncaseId = '+caseID+'\n\n');
        useDefaultAssignmentRules = false;
        if(caseID == null)
        {
            User u = [Select Id, Profile.UserLicense.LicenseDefinitionKey, ContactID, Contact.AccountID from User where id=: UserInfo.getUserId()];
            isPortal = u.Profile.UserLicense.LicenseDefinitionKey == Constants.CUSTOMER_PORTAL_LICENSE_KEY;
            
            this.c = (Case)std.getRecord(); 
            this.isChangeAsset = false;
            
            if(isPortal)
            {
                c.AccountID = u.Contact.AccountID;
                c.ContactID = u.ContactID;
            }
            else
            {
                c.AccountId = ApexPages.currentPage().getParameters().get(Constants.QRY_STRING_ACCOUNT_ID);
                c.ContactId = ApexPages.currentPage().getParameters().get(Constants.QRY_STRING_CONTACT_ID);
            }
               
            caseComment = new CaseComment();
            initialChangeAssetLoad = false;
        }
        else
        {
            
            this.c = (Case)std.getRecord(); 
            this.isChangeAsset = true;
            isPortal = false;
            useDefaultAssignmentRules = true;
            initialChangeAssetLoad = GenericServices.isNullOrEmpty(this.c.Customer_Asset__c)?false:true;
        }
        
        if(c.AccountID == null)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.No_Account_provided));
            return;
        }
        try
        {
            a = [Select ID, Name from Account where id =: c.AccountID];
        }
        catch(QueryException qexc)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.Invalid_Account_ID_URL_param));
            return ;
        }
        am = new AccountModel(a);
        cm = new CaseModel(c);
        if(cm.isContent)
          c.Issue_Type__c = CONSTANTS.ESCALATED_CASE_ISSUE_TYPE;
        loadData();
        
    }
    
    private void loadData()
    {
        try
      {
          am.getContacts(c.AccountID, c.ContactID);
      }
      catch(CaseException exc)
      {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, exc.getMessage()));
      return;
      }
        contactOptions = am.contactOptions;
        if(c.ContactID == null)
            makeDependentPicklistSelections(contactOptions,'ContactID');
        else
            makeDependentPicklistSelections(c.ContactID,'ContactID');
        
        am.getAssets();
        productLineOptions = am.productLineOptions;
        makeDependentPicklistSelections(productLineOptions, 'Product_Line__c');
        makeDependentPicklistSelectionsForProductLine();
    }
    
    public void makeDependentPicklistSelectionsForProductLine()
    {
      assetOptions = new List<SelectOption>();
      assetOptions.add(new SelectOption('', 'None'));
      assetOptions.addAll(am.getAssetOptionsByLine(c.Product_Line__c));
      if(assetOptions.size() == 1)
          assetOptions = new List<SelectOption>{new SelectOption('', Constants.NO_ASSETS_AVAILABLE)};
        
        makeDependentPicklistSelections(assetOptions, 'Customer_Asset__c');
        makeDependentPicklistSelectionsForAsset();
        
        
    }
    
    public void makeDependentPicklistSelectionsForAsset()
    {
      componentOptions = am.getComponentOptionsByAsset(c.Customer_Asset__c);
      makeDependentPicklistSelections(componentOptions, 'Component__c');
      
      initialChangeAssetLoad = false;
    }
    
    private void makeDependentPicklistSelections(List<SelectOption> options, String fieldName)
    {
        if(options != null && options.size()> 0 && !initialChangeAssetLoad)
        {
            SYstem.debug('\n\noptions[0].getValue() = '+options[0].getValue()+'\n\n');
            c.put(fieldName, options[0].getValue());
            
        }
    }
    
    private void makeDependentPicklistSelections(String value, String fieldName)
    {
        if(value != null  && !initialChangeAssetLoad)
        {
            c.put(fieldName, value);
            
        }
    }
    
    public List<SelectOption> contactOptions {get; private set;}
    public List<SelectOption> productLineOptions{get; private set;}
    public List<SelectOption> assetOptions{get; private set;}
    public List<SelectOption> componentOptions{get; private set;}
    
    
    public Pagereference saveOnly()
    {
        SavePoint sp = Database.setSavePoint();
        try
        {
            return am.saveCase(cm, caseComment, Constants.REDIRECT_PARENT, useDefaultAssignmentRules, isChangeAsset,false);
        }
        catch(DMLException dmle)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.General_Error_On_Case+': '+dmle));
            Database.rollBack(sp);
            return null;
        }
        catch(Exception exc)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.General_Error_On_Case+': '+exc));
            Database.rollBack(sp);
            return null;
        }
    }
    
    public Pagereference saveAndAttach()
    {
        SavePoint sp = Database.setSavePoint();
        try
        {
            return am.saveCase(cm, caseComment, Constants.REDIRECT_ATTACH, useDefaultAssignmentRules, false,false);
        }
        catch(DMLException dmle)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.General_Error_On_Case+': '+dmle));
            Database.rollBack(sp);
            return null;
        }
        catch(Exception exc)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.General_Error_On_Case+': '+exc));
            Database.rollBack(sp);
            return null;
        }
    }
    
    public Pagereference saveAndClose()
    {
        SavePoint sp = Database.setSavePoint();
        try
        {
            return am.saveCase(cm, caseComment, Constants.REDIRECT_CLOSED, useDefaultAssignmentRules, false,true);
        }
        catch(DMLException dmle)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.General_Error_On_Case+': '+dmle));
            Database.rollBack(sp);
            return null;
        }
        catch(Exception exc)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.General_Error_On_Case+': '+exc));
            Database.rollBack(sp);
            return null;
        }
    }
    
    
    public void refreshAccount()
    {
        if(c != null )
        {
            if( !GenericServices.isNullOrEmpty(c.AccountID))
            {
                am = new AccountModel(new Account(id = c.AccountID));
                c.ContactID = null;
                loadData();
            }
            else
            {
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.No_Account_provided+' - Please choose an Account before clicking Go'));
            }
        }
    }
}


ShashForceShashForce
Hi,

This code is exiting if account ID is not passed.

if(c.AccountID == null)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, System.label.No_Account_provided));
            return;
        }

You can modify these lines, remove the line "return;", and query for the Account ID if the button is clicked from a case, and pass the acoount ID to the new case being created.

Thanks,
Shashank