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
Cristian TrifCristian Trif 

button that creates new Quote

Hi,

I need to create a custom button on Opportunity Layout then when this button is pressed I need to create a new Quote and redirect to a VF page that I will create afterward. Someone can help me with this, please? I wrote some code but I don't know if it's good or bad.

Just need to create a new Quote on the current Opportunity which I'm in.

 

public class MyController {

    public String currentRecordId {get;set;}
    public String parameterValue {get;set;}
    public Opportunity opp {get;set;}
    
    public MyController(ApexPages.StandardController controller){
        currentRecordId = ApexPages.currentPage().getparameters().get('id');
        opp = [Select id From Opportunity where id =: currentRecordId];
		parameterValue = ApexPages.currentPage().getparameters().get('nameParam');
        
        Quote qo = new Quote();
        insert qo;
    }
    
}
Best Answer chosen by Cristian Trif
Newbie__SalesforceNewbie__Salesforce
you need to update the instance 

copy paste below code - 
public class quoteController {

    public String currentRecordId {get;set;}
    public Opportunity opp {get;set;}
    
    public quoteController(ApexPages.StandardController controller){
        currentRecordId = ApexPages.currentPage().getparameters().get('id');
        opp = [Select id, Accountid From Opportunity where id =: currentRecordId];
        
    }
    public PageReference createquote(){
        
        opp.StageName = 'Qualification';

        Quote qo = new Quote();
        qo.Opportunityid=opp.id;
        qo.Name='Test';
        qo.Status='Accepted';
        insert qo;
        
        Contract ct = new Contract();
        ct.Accountid = opp.AccountId;
        ct.Opportunity__C = currentRecordId;
        ct.Status = 'Draft';
        ct.StartDate = System.today();
        ct.ContractTerm = 4;

        update opp;    // this will update your instance
        insert ct;
        PageReference pr = new PageReference('/apex/VehicleVF');
                 pr.setRedirect(true);
        return pr;
    }
}

 

All Answers

Newbie__SalesforceNewbie__Salesforce
Hello Cristian,

Step 1 : Create a visualforce page 
<apex:page action="{!doInsert}" standardController="Oppportunity" extensions="OpportunityExtension" >

<!-- Your logic goes here -->

</apex:page>
Step 2 : Create a controller extension
public with sharing class OppportunityExtension {
	private final Opportuity oppObj;
    public Id oppId;

  /*
    Constructor to initialize 
  */
    public OppportunityExtension (ApexPages.StandardController stdController) {
        oppObj = (Oppportunity)stdController.getRecord();
        oppId = ApexPages.currentPage().getParameters().get('id');
    }

  /*
    doInsert is called on page load
  */
    public void doInsert() {
       // Include you req.
    }    
}

Step 3 : Create custom button of the type detail page button and include the same vf 


Let me know if you face any difficulties
Cristian TrifCristian Trif

so basically in the doInsert method I will include my logic something like:

//create new Quote and add the required fields
Quote qt = new Quote();
qt.ContracTNumber='0001';
....
....

//after this insert the object
insert qt;

Similar to this i should write in this method?

Newbie__SalesforceNewbie__Salesforce
Yes, it will be the action you want to perform on page load.
Cristian TrifCristian Trif
HI again, I managed to create the Quote.
 
public class quoteController {

    public String currentRecordId {get;set;}
    public Opportunity opp {get;set;}
    
    public quoteController(ApexPages.StandardController controller){
        currentRecordId = ApexPages.currentPage().getparameters().get('id');
        opp = [Select id, Accountid From Opportunity where id =: currentRecordId];
    }
    public PageReference createquote(){
         Quote qo = new Quote();
        qo.Opportunityid=opp.id;
        qo.Name='Test';
        insert qo;
        
        Contract ct = new Contract();
        ct.Accountid = opp.id;
        ct.Opportunity__C = opp.id;
        ct.Status = 'Draft';
        ct.StartDate = System.today();
        ct.ContractTerm = 4;
        PageReference pr = new PageReference('/apex/VehicleVF');
                 pr.setRedirect(true);
        return pr;
    }
}

As you can see I'm trying to also create a Contract as well related to that Opportunity but it does not create anything... I think the problem is at ct.AccountId = opp.id; I'm not sure how to make a relation between the Account name... do you know how? 
Newbie__SalesforceNewbie__Salesforce
opp.id will return opprtunity id, we need account id which is a lookup in opportunity.

So use - 
ct.Accountid = opp.AccountId 
Cristian TrifCristian Trif

It works man. Thanks! And one final thing, I need to change the StageName of the opportunity when I'm pressing this button.

I added:   opp.StageName = 'Qualification'; but it doesn't change. You know why?

public class quoteController {

    public String currentRecordId {get;set;}
    public Opportunity opp {get;set;}
    
    public quoteController(ApexPages.StandardController controller){
        currentRecordId = ApexPages.currentPage().getparameters().get('id');
        opp = [Select id, Accountid From Opportunity where id =: currentRecordId];
        
    }
    public PageReference createquote(){
        
        opp.StageName = 'Qualification';

        Quote qo = new Quote();
        qo.Opportunityid=opp.id;
        qo.Name='Test';
        qo.Status='Accepted';
        insert qo;
        
        Contract ct = new Contract();
        ct.Accountid = opp.AccountId;
        ct.Opportunity__C = currentRecordId;
        ct.Status = 'Draft';
        ct.StartDate = System.today();
        ct.ContractTerm = 4;
        insert ct;
        PageReference pr = new PageReference('/apex/VehicleVF');
                 pr.setRedirect(true);
        return pr;
    }
}
Newbie__SalesforceNewbie__Salesforce
you need to update the instance 

copy paste below code - 
public class quoteController {

    public String currentRecordId {get;set;}
    public Opportunity opp {get;set;}
    
    public quoteController(ApexPages.StandardController controller){
        currentRecordId = ApexPages.currentPage().getparameters().get('id');
        opp = [Select id, Accountid From Opportunity where id =: currentRecordId];
        
    }
    public PageReference createquote(){
        
        opp.StageName = 'Qualification';

        Quote qo = new Quote();
        qo.Opportunityid=opp.id;
        qo.Name='Test';
        qo.Status='Accepted';
        insert qo;
        
        Contract ct = new Contract();
        ct.Accountid = opp.AccountId;
        ct.Opportunity__C = currentRecordId;
        ct.Status = 'Draft';
        ct.StartDate = System.today();
        ct.ContractTerm = 4;

        update opp;    // this will update your instance
        insert ct;
        PageReference pr = new PageReference('/apex/VehicleVF');
                 pr.setRedirect(true);
        return pr;
    }
}

 
This was selected as the best answer
Cristian TrifCristian Trif
Thanks a lot... missed that update...
v varaprasadv varaprasad
Hi Cristian,

add opp.accountid.
 
public class quoteController {

    public String currentRecordId {get;set;}
    public Opportunity opp {get;set;}
    
    public quoteController(ApexPages.StandardController controller){
        currentRecordId = ApexPages.currentPage().getparameters().get('id');
        opp = [Select id, Accountid From Opportunity where id =: currentRecordId];
    }
    public PageReference createquote(){
         Quote qo = new Quote();
        qo.Opportunityid=opp.id;
        qo.Name='Test';
        insert qo;
        
        Contract ct = new Contract();
        ct.Accountid = opp.Accountid;  // Added account id
        ct.Opportunity__C = opp.id;
        ct.Status = 'Draft';
        ct.StartDate = System.today();
        ct.ContractTerm = 4;
        PageReference pr = new PageReference('/apex/VehicleVF');
                 pr.setRedirect(true);
        return pr;
    }
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1