You need to sign in to do that
Don't have an account?
James Kinsey
Custom Button to Create New Opportunity
Hello,
I am trying to create a custom button on a custom object to create an opportunity. I would like to merge many fields from the custom object into the new opportunity to reduce admin data entry.
Does anybody have a good example of their code that I can view and use as an example to base my developement around?
Thanks in advance!!!
- James
I don't have any sample code but you definitely can do that - quite easily.
1. Create a Visualforce page. It will be a page that uses both the standard controller of your custom object and an extension (ie custom apex class)
2. Create the apex class defined as your extension. In that class is where you will create a new Opportunity sObject, populate the fields from the custom object, and insert the opportunity. After you insert it, you can get the newly created id, and redirect the user to that newly created opportunity automatically.
3. Now that you have both the VF and Apex, create the custom button which will be associated to that VF page.
4. Put button on your page layout.
Once you have that stubbed out and run into issues, please post the code snippets and we'll take it from there.
Thank you. Working on it now.
As I am a noob, this will also be my first custom controller
I'll post when I have issues.
While technically a hack, you could also achieve this without visualforce or apex. There is a solution within Help & Training that explains how to go about it. I searched on "custom button" and it came up in the search results, but the specific name of the solution is "How do I create a Custom Button and Link to create a New Case from Cases related list on Opportunities?"
Ok what I have is below. But I'm not sure how to make the controller create a new opportunity and then make the VF page reference and open it...
public class VFControllerExtension {
private final Equipment__c e;
public VFControllerExtension (ApexPages.standardcontroller stdcontroller) {
this.e = (Equipment__c)stdController.getRecord();}
public PageReference getOpportunity() {
new Opportunity();
return null;
}
}
I'm all over the place here. What I have now is...
AND
With this error:
System.NullPointerException: Attempt to de-reference a null object
Class.OppClass2.NewOpp: line 18, column 48 External entry point
----
I understand I have moved away from the button to a VF page with a link. It was creating an opportunity correctly but not bringing in my equipment__c fields that I wanted.
Also, I don't know how to have the VF page open the opportunity instead of sending me to a link that then takes me to the opportunity...AHHH!!!
HELP :(