2

I want to create a button in my VF page that when clicked on creates a new case with a certain record type or at least takes you to the new case page with the record type already filled in. How would I do this?

2 Answers 2

1

creating new case, just join button with method:

public class some controller{
   publis PageReference createCase(){
       Case case = new Case( RecordTypeId = [SELECT Id 
                                             FROM RecordType 
                                             WHERE SobjectType = 'Case' 
                                                 AND DeveloperName = 'Record Type Name'
                                             Limit 1].Id,
                             /** required fields **/
                           )
       try{
           insert case;
       }
       catch(Exception ex){
        /** some actions  **/
       }
       return new PageReference ('/' + case.Id);
   }
}

This will create new case with selected RecordType and redirect user to this case. Here I used query, but in my oppinion it is better to store it in some class as static varible.

1

Alternatively you can use a simple URLFOR tag together with an $Action global variable:

<apex:commandButton action="{!URLFOR($Action.Case.NewCase)}" value="Create a case"/>

This button redirects user to the record type selection page (only if you have any) and then to the new case page layout.

1
  • Great this approach works fine. But what if I want to do a "Save & New" on a child record. The new child record loses the information about his parent record, if you know what i mean. This would solve my problem here: salesforce.stackexchange.com/questions/41887/…
    – David Renz
    Jul 23, 2014 at 13:20

You must log in to answer this question.

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