0

I need to create a custom button on my Opportunity Page Layout and when it is pressed this button needs to create automatically a Quote. Basically, the point of this is to avoid clicking so much. I wrote some code but I don't know if it's good or bad and I don't know how to continue to this.

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;
  }
}
2
  • Is this requirement specific to lightning or classic as well? – Abhishek Daware Aug 31 '18 at 9:15
  • we work in classic, hence only classic. – Sarah Paulson Aug 31 '18 at 9:34
0

All you need is:-

Create a Controller with a method createrecord with return type pagerefernce. Here I am creating a quote related to that Opportunity and redirect to newly created 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;
        PageReference pr = new PageReference('/' +qo.id);
                 pr.setRedirect(true);
        return pr;
    }
}

Now, create a visualforce page and call that method in this page as follows:-

<apex:page extensions="quoteController" standardController="Opportunity" action="{!createquote}">
</apex:page>

Now, go to setup--> opportunity-->button links and action--> create a new button---> content source-- visualforce page-->fill other details-->save it-->add it to opportunity layout Remember to add your button display behaviour as Display in existing window without sidebar or header

Your custom quote button is ready. Hope it helps You.

6
  • humar I create a new button just how you said it, so I went to etup--> opportunity-->button links and action--> new button or link --> selected Detail Page button --> Display in new window --> URL --> in body i added /apex/quoteVF --> saved it. I added the button to Opportunity page layout and when i click it it says: List has no rows for assignment to SObject An unexpected error has occurred. Your development organization has been notified. What I'm doing wrong here? – Sarah Paulson Aug 31 '18 at 10:54
  • Use display behavior Display in existing window without sidebar or header – sanket kumar Aug 31 '18 at 10:57
  • damn, i still have the same error with last has no rows. – Sarah Paulson Aug 31 '18 at 11:01
  • @Franken you should not use the content source as url. use Content Source as Visualforce Page. – sanket kumar Aug 31 '18 at 11:04
  • yep, it works:D thank you very much! And if i want to redirect to a custom visualforce i can change this: partPageReference pr = new PageReference('/' +qo.id); pr.setRedirect(true); return pr; I have a custom VF page that needs to be shown after I click the button with couple of fields. – Sarah Paulson Aug 31 '18 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.