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
Rung41Rung41 

Save and Create Opportunity button?

I created a custom VF page called Renters, that is used to capture information similar to Contacts. When a user clicks on the "Save" button I want the record to save and then automatically create a new Opportunity with some of the information captured in the VF page. I can get the record to save but how do I also get it to create a new Opportunity?

 

I'm not sure where to start.

Best Answer chosen by Admin (Salesforce Developers) 
Darshan FarswanDarshan Farswan

I had a custom object Merchandise__c in my instance. I am providing you the class and page code below

 

<apex:page standardController="Merchandise__c" extensions="atest">
   <apex:form >
   <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!Save}"/>
            <apex:commandButton value="Save and New" action="{!News}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
       <apex:pageBlockSection columns="2">
            <apex:inputField value="{!mer.Name}"/>
            <apex:inputField value="{!mer.Description__c}"/>
            <apex:inputField value="{!mer.Price__c}"/>
            <apex:inputField value="{!mer.Total_Inventory__c}"/>
       </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>
             



 

public class atest{
    public Opportunity opp{get;set;}
    public Merchandise__c mer{get;set;}
    public atest(ApexPages.StandardController controller)
    {
        mer = (Merchandise__c)controller.getRecord();
    }
    public PageReference Save(){
        insert mer;
        opp = new Opportunity();
        opp.Name = mer.Name;
        opp.CloseDate = System.today()+1;
        opp.StageName = 'Prospecting';
        insert opp;
        PageReference ref = new PageReference('/'+opp.id);
        return ref;
    }
    public PageReference News(){
        return null;
    }
    public PageReference Cancel(){
        return null;
    }
}



when you try to save the merchandise object, the Save Action method runs and it helps in creating a opportunity as well.

It will also redirect you to the opportunity created.

All Answers

Darshan FarswanDarshan Farswan

Creating an Opportunity with the help of given information is farely simple. You must have defined the the Action method for the Save button. WIthin that Function you can define the new Opportunity and assign all the respective values. And simply insert that.

Rung41Rung41

Are able to provide an example?

Darshan FarswanDarshan Farswan

I had a custom object Merchandise__c in my instance. I am providing you the class and page code below

 

<apex:page standardController="Merchandise__c" extensions="atest">
   <apex:form >
   <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!Save}"/>
            <apex:commandButton value="Save and New" action="{!News}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
       <apex:pageBlockSection columns="2">
            <apex:inputField value="{!mer.Name}"/>
            <apex:inputField value="{!mer.Description__c}"/>
            <apex:inputField value="{!mer.Price__c}"/>
            <apex:inputField value="{!mer.Total_Inventory__c}"/>
       </apex:pageBlockSection>
       </apex:pageBlock>
    </apex:form>
</apex:page>
             



 

public class atest{
    public Opportunity opp{get;set;}
    public Merchandise__c mer{get;set;}
    public atest(ApexPages.StandardController controller)
    {
        mer = (Merchandise__c)controller.getRecord();
    }
    public PageReference Save(){
        insert mer;
        opp = new Opportunity();
        opp.Name = mer.Name;
        opp.CloseDate = System.today()+1;
        opp.StageName = 'Prospecting';
        insert opp;
        PageReference ref = new PageReference('/'+opp.id);
        return ref;
    }
    public PageReference News(){
        return null;
    }
    public PageReference Cancel(){
        return null;
    }
}



when you try to save the merchandise object, the Save Action method runs and it helps in creating a opportunity as well.

It will also redirect you to the opportunity created.

This was selected as the best answer
Rung41Rung41

Works great. One question though, is there any way to when the user is brought over to the Opportunity page, that its still in edit mode? Currently the user is brought over to the Opportunity page that has been saved.

Darshan FarswanDarshan Farswan

In the class definition, only change the page reference you want when you return from the Save action method. Here is what you need to replace.

 

REPLACE

 

PageReference ref = new PageReference('/'+opp.id);

WITH

 

PageReference ref = new PageReference('/'+opp.id+'/e?returl=%2F'+opp.id);

 

This will redirect you to Opportunity Edit page.

 



Rung41Rung41

I just want to Thank you for your help. Its been a great learning experience. I am starting to write the test class and was wondering if I am a doing this correctly.

This is what I have so far. I would continue to add the data to test each field.

 

public class testNewRentalOpportunity {
 static testMethod void myUnitTest(){
   ApexPages.standardController controller = new ApexPages.standardController(new Renter__c());
   
    test.startTest();
    
 Opportunity opp = new Opportunity();
 
 opp.name = 'Test Name';
 opp.Renters_Email__c = 'jsmith@aol.com';
 
 test.stopTest();
  }
  }

 

 

 

Darshan FarswanDarshan Farswan

As you are writing the test class for a particular Apex class, you must have used that class as a controller or extension. Therefore in the test class you must define the linkage to the particular Visualforce page. This is how it is done.

 

@isTest

 

private class <className>{

   static testMethod void <methodName>(){

       Test.setCurrentPage(Page.<yourPageName>);

        ApexPages.StandardController sc = new ApexPages.StandardController(new <sObjectName>);
        <sObjectName> sController = new <sObjectName>(sc);

        */

        the code can be written to access the functions defined within the class method. for e.g.  sController.Save();

        make sure to provide all the information that the particular function needs.

        /*

   }

}