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
jsacpt24jsacpt24 

create opportunity from button

I am trying to create a visualforce button that then inserts an opportunity and then sends an email to anyone if the running user is listed as the marketing_development_rep__c. Not sure if I am doing something wrong but it isn't creating the opportunity when I go and hit save.

VF: 
<apex:page controller="mdrHandOffOppInsert" sidebar="false" showHeader="false">
    <apex:form id="frm">
        <apex:pageBlock >
            <strong>Please provide any reference notes for this deal.</strong>
            <apex:pageBlockSection >
               <apex:inputField value="{!Opportunity.Hand_Off_Notes__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:actionFunction name="quickSaveJavascript" action="{!quicksave}" oncomplete="window.top.close();"/>
               <apex:commandButton value="Submit" onclick="quickSaveJavascript();"/>
               <!--<apex:commandButton action="{!cancel}" value="Cancel"/>-->
           </apex:pageBlockButtons>
       </apex:pageBlock>
    </apex:form>
</apex:page>

Apex:
 
public class mdrHandOffOppInsert {
    public Account acc{get; set;}
    public opportunity oppstring{get; set;}
    public mdrHandOffOppInsert(){oppstring= new opportunity();}
    public void Saveto(){
        opportunity opp= new opportunity();
        opp.Accountid= oppstring.Accountid;
		opp.StageName= 'F- Awareness';
        opp.CloseDate=date.today()+90;
        opp.Type='New Business';
        opp.Name= oppstring.name + ' SaaS Deal';
        opp.Marketing_Opp__c = true;
        opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('SaaS - Marketer').getRecordTypeId();		             
        insert opp;
		}
    
}

public static sendEmailToAccDir {
    
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
                List <User> sadList= SELECT Account_Director__r.email
               	  				 		FROM team__c
               	  						WHERE Marketing_Development_Rep__r.id = UserInfo.getUserId()]; 
                mail.setTargetObjectId(Con[0].id); 
                mail.setSenderDisplayName('Salesforce Support'); 
                mail.setUseSignature(false); 
                mail.setBccSender(false); 
                mail.setSaveAsActivity(false); 
                EmailTemplate et=[Select id from EmailTemplate where Name=:'MDR_Sales_Handoff_Initial_Alert']; 
                mail.setTemplateId(et.id); 
                Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

​​​​​​​
Rajesh3699Rajesh3699
Hey,

Is this the updated code..?

In the SaveTo() method, you have written logic to  create opportunity, but I don't see this function is being called from any button in the VF page.

Am I right..?

Thank You,
Rajesh Adiga P.
jsacpt24jsacpt24
I did some work on the apex class but I was under the impression that using the controller to pull in the apex class and having it call to the save feature would allow the record to be created? 

VF: 
<apex:page controller="mdrHandOffOppInsert" sidebar="false" showHeader="false">
    <apex:form id="frm">
        <apex:pageBlock >
            <strong>Please provide any reference notes for this deal.</strong>
            <apex:pageBlockSection >
               <apex:inputField value="{!Opportunity.Hand_Off_Notes__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
               <apex:commandButton action="{!save}" value="Submit"/>
               <apex:commandButton action="{!cancel}" value="Cancel"/>
           </apex:pageBlockButtons>
       </apex:pageBlock>
    </apex:form> 
</apex:page>


Apex: 
public class mdrHandOffOppInsert {

    public opportunity opp = new opportunity();
    public Account acc{get; set;}
    Public opportunity oppstring{get;set;}
    public mdrHandOffOppInsert(){
    
            opp.Accountid= oppstring.Accountid;
            opp.StageName= 'F- Awareness';
            opp.CloseDate=date.today()+90;
            opp.Type='New Business';
            opp.Name= oppstring.name + ' SaaS Deal';
            opp.Marketing_Opp__c = true;
            opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('SaaS - Marketer').getRecordTypeId();	
                                  
        }

    // Method for saving new opportunity
    public PageReference save(){ 
    //Saving new opportunity
      try{
         insert opp;
         PageReference pg = new PageReference('/'+opportunity.Id);
         pg.setRedirect(true);
         return pg;
         }
     Catch(Exception e){
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Something Went Wrong !Please Try Again'));
         return null;
          }    
      }

   // Method  to Cancel
     public pagereference cancel(){
         
         PageReference pg = new PageReference('/'+apexpages.currentpage().getparameters().get('id'));
         pg.setRedirect(true);
         return pg; 
       }
    
}

 
Rajesh3699Rajesh3699
Yes, this will work. 
Also, update the VF page line 6 with the below line
<apex:inputField value="{!Opp.Hand_Off_Notes__c}" required="true"/>
 
jsacpt24jsacpt24
Still getting an error. "Unknown constructor 'mdrHandOffOppInsert.mdrHandOffOppInsert()'". I had a couple other things that caused issues so I added the standardsetcontroller into the apex class and added a standard controller with extension. 

VF:
 
<apex:page standardController="Account" extensions="mdrHandOffOppInsert" sidebar="false" showHeader="false">
    <apex:form id="frm">
        <apex:pageBlock >
            <strong>Please provide any reference notes for this deal.</strong>
            <apex:pageBlockSection >
               <apex:inputField value="{!Opp.Hand_Off_Notes__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
               <apex:commandButton action="{!submit}" value="Submit"/>
               <apex:commandButton action="{!cancel}" value="Cancel"/>
           </apex:pageBlockButtons>
       </apex:pageBlock>
    </apex:form> 
</apex:page>



APEX: 
 
public class mdrHandOffOppInsert {

    public opportunity opp = new opportunity();
    public Account acc{get; set;}
    public ApexPages.StandardSetController stdCntrlr {get; set;}
    public opportunity oppstring{get;set;}
    public mdrHandOffOppInsert(ApexPages.StandardSetController controller){
        stdCntrlr = controller;
        opp.Accountid= oppstring.Accountid;
        opp.StageName= 'F- Awareness';
        opp.CloseDate=date.today()+90; 
        opp.Type='New Business';
        opp.Name= oppstring.name + ' SaaS Deal';
        opp.Marketing_Opp__c = true;
        opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('SaaS - Marketer').getRecordTypeId();	
                                  
        }

    // Method for saving new opportunity
    public PageReference save(){ 
    //Saving new opportunity
      try{
         insert opp;
         PageReference pg = new PageReference('/'+opportunity.Id);
         pg.setRedirect(true);
         return pg;
         }
     Catch(Exception e){
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Something Went Wrong !Please Try Again'));
         return null;
          }    
      }

   // Method  to Cancel
     public pagereference cancel(){
         
         PageReference pg = new PageReference('/'+apexpages.currentpage().getparameters().get('id'));
         pg.setRedirect(true);
         return pg; 
       }
    
}