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
James KinseyJames 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

aalbertaalbert

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. 

James KinseyJames Kinsey

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.

flewellsflewells

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?"

James KinseyJames Kinsey

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;

}
  
    }

James KinseyJames Kinsey

I'm all over the place here.  What I have now is...

 

public without sharing class OppClass2
{

    private final equipment__c equipment;
    
      public OppClass2(ApexPages.StandardController controller) {
            }
      public oppclass2() {
        equipment= [select id, name from Equipment__c
        where id = :ApexPages.currentPage().getParameters().get('id')];
          }
          
      public Equipment__c getquipment() {
          return equipment;  
          }

    public PageReference NewOpp(){
        Opportunity o = new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today());
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = 'name',StageName='New Opportunity',CloseDate=system.today())},false);
        for(Database.SaveResult sr: lsr){
            if(!sr.isSuccess())
                Database.Error err = sr.getErrors()[0];
        }
                return (new ApexPages.StandardController(o)).edit();

        }

}

 AND

<apex:page standardController="Equipment__c" extensions="OppClass2">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Q1 2011 Upsell</h1>
Create new opportunity with this campaign:
<!-- My Stufff -->
    <apex:form >
    <apex:commandLink action="{!NewOpp}" value="Create New Opportunity" id="comlink1"/>
    </apex:form>
<!-- END MY STUFF-->
  <!-- End Default Content REMOVE THIS -->
  
  
</apex:page>

 

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 :(