hy! I have created new custom button named as "Readmit" on detail page of opportunity in lightning view.i want to make this functional, such as that when a user click on this button,a new opportunity should generate with "Opportunity Name","Phone" and "Mobile" of current opportunity.

please guide me how i can do this.

  • What kind of button have you created? Is it visualforce page button or Lightning Component button or Javascript or url? – sanket kumar Sep 7 '18 at 11:27
  • Thanks for reply, i have created visualforce page button, i don't know this is right or not, actually i am new to salesforce. please have look image. pasteboard.co/HCMdAHy.png – uzman Sep 7 '18 at 11:37
  • Is there a relationship between old opportunity and new opportunity? A lookup field? You can use standard quickactions with predefined fields. – Pranay Jaiswal Sep 7 '18 at 11:38
  • No there is not relationship between old and new opportunity. – uzman Sep 7 '18 at 11:40
  • Yopu might have to look into lightning data services component, – Pranay Jaiswal Sep 7 '18 at 11:43

@Uzman I am sharing a sample code which you can use to achieve your functionality:-

Here is your VF page where I am calling the action from Controller which will create the new Opportunity:-

<apex:page standardController="Opportunity" extensions="CLONEController" action="{!cloneOpp}">
    <apex:slds />
    <apex:pageMessages />

Now, In your apex:-

     public class CLONEController{
            private PageReference pr;
            public Opportunity oppy {get;set;}  

        public CLONEController(ApexPages.StandardController controller) {
                String oppId = ApexPages.currentPage().getParameters().get('Id');
                oppy =[ select Id,Name,StageName,Type,CloseDate,AccountId,Amount from opportunity where id = :oppId];
    public PageReference cloneOpp()
            Opportunity newOpp = oppy.clone(false, true, false, false);
            newOpp.AccountId = oppy.AccountId;
            newOpp.Name = oppy.Name;
            newOpp.StageName = 'Pre-Qualification'; 
            newOpp.Type = 'New Business';
            newOpp.CloseDate = Date.Today();
            insert newOpp;
            return new PageReference('/' + newOpp.Id + '/e' );    
            catch (DMLException e)
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Failed to create clone'));
            return null;

Now make use of it as a button and use it on Your Opportunity Layout. It will work in both Classic and Lightning.

Hope it helps you.

| improve this answer | |
  • thanks for code snippet, can you further explain that where i have to write the apex tag related code and where have to write the class and methods. – uzman Sep 7 '18 at 11:53
  • You don't need to add anything further in vf page. What you all need is setting the default value in apex controller like I have done in cloneOpp() method. I have set the default value of Name, stage name,accountid,Type, Closedate. Like that you can set any field you want. – sanket kumar Sep 7 '18 at 11:58
  • where does apex controller exist ? – uzman Sep 7 '18 at 12:01
  • I have given you two things in my snippet. The first one is a visualForce page and the second one is the apex controller of that page. – sanket kumar Sep 7 '18 at 12:03
  • i am trying to create new apex class, but there is no link to create new apex class, please see the image. pasteboard.co/HCMsI0h.png how i can create new apex class ? – uzman Sep 7 '18 at 12:14

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.