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
Pooja NekkalapudiPooja Nekkalapudi 

Button to create a new case from the Opportunity object

How to create a button that opens a visual force page from the opportunity object ,which will prompt to fill fields like 
Case record type 
Subject
description
contact name ( look up field)
account name  ( pre populated from the respective opprtunity record)
opportunity ( pre populated from the respective opprtunity record)
task_c ( custom picklist data type )
save button ( this will create a new case record with the given input values for above records)
cancel button
All the above are fields from case object
Best Answer chosen by Pooja Nekkalapudi
Mahesh DMahesh D
Hi Pooja,

Please create a VF page with the StandardController='Opportunity' with extensions='OpportunityExtensionController'. 
something like below:

public class OpportunityExtensionController {
     public Case ca {get; set;}
     public OpportunityExtensionController(ApexPages.StandardController controller) {
           Opportunity opp = (Opportunity) controller.getRecord();
           opp = [Select Id, Name, ........ from Opportunity where Id =: opp.Id];
           ca = new Case();
           ca.OpportunityId = opp.Id;
           ca.AccountId = opp.AccountId;
     }
}

Above code will give an idea how to write the VF page and Controller.

All Answers

Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

Use URL hacking

http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html

Mahesh DMahesh D
Hi Pooja,

Please create a VF page with the StandardController='Opportunity' with extensions='OpportunityExtensionController'. 
something like below:

public class OpportunityExtensionController {
     public Case ca {get; set;}
     public OpportunityExtensionController(ApexPages.StandardController controller) {
           Opportunity opp = (Opportunity) controller.getRecord();
           opp = [Select Id, Name, ........ from Opportunity where Id =: opp.Id];
           ca = new Case();
           ca.OpportunityId = opp.Id;
           ca.AccountId = opp.AccountId;
     }
}

Above code will give an idea how to write the VF page and Controller.
This was selected as the best answer
Pooja NekkalapudiPooja Nekkalapudi
I tried the above @Mahesh 
my VF page
<apex:page standardcontroller="Opportunity" extensions="OpportunityExtensionController">
<apex:sectionHeader title="Title" subtitle="Case Helper"/>
<apex:form > <apex:pageMessages />
<apex:pageBlock title="Please fill in the details for creating a Case">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>
</apex:pageBlockButtons> <apex:pageBlockSection > <apex:inputField value="{!Opportunity.AccountId}" required="true"/>
<apex:inputField value="{!Opportunity.Name}" required="true"/>
<apex:inputField value="{!ca.RecordTypeId}" required="true"/> <apex:inputField value="{!ca.Subject}" required="true"/>
<apex:inputField value="{!ca.Description}" required="true"/> <apex:inputField value="{!ca.Task__c}" required="true"/>
<apex:inputField value="{!ca.Task_Details__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>  

my class
public class OpportunityExtensionController {
     public Case ca {get; set;}
     public OpportunityExtensionController(ApexPages.StandardController controller) {
           Opportunity opp = (Opportunity) controller.getRecord();
           opp = [Select Id, Name, AccountId from Opportunity where Id =: opp.Id];
           ca = new Case();
           ca.Pitch__c = opp.Id;
           ca.AccountId = opp.AccountId;
     }
}  
this did give me a visual force page with those fields but did not create a case , what am I doing wrong
Mahesh DMahesh D

Hi Pooja,

You have to implement save and cancel action methods.

public PageReference save() {
       insert ca;
       return new PageReference('/'+ca.Id);
}

public PageReference cancel() {
     return new PageReference('/'+opp.Id);
}

Declare the Opportunity opp outside the constructor to work with cancel button.

Regards,
Mahesh

Pooja NekkalapudiPooja Nekkalapudi
Awesome this worked :) thanks a lot
Could you giv eme one last advise how to pop or alert message saying " new case has been created with CaseNumber: xxxx or CaseID : 000000"
 
Mahesh DMahesh D
Hi Pooja,

The above code will automatically take you to the details page of newly created Case record.

If you don't want it and just to display a message with the case number then you can so it in the existing VF page orelse create a new VF page and achieve it.

If it is the same VF page then just return null from the save method so that it will go back to the same page. And if the VF page you can enable the outputPanel to display the success message whenever the boolean variable says true

something like below:

<apex:outputPanel rendered='{!isCaseCreated}'>
         New Case has beed created with CaseNumber: {!ca.CaseNumber} and Case Id: {!ca.Id} 
</apex:outputPanel>

This is just a sample code and you have to write the actual code accordingly.

Regards,
Mahesh