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
IWyattIWyatt 

[Help Requested] Create new Opp from visualforce page hyperlink

Hello, I am trying to build a simple proof of concept whereby a hyperlink on an in-line visualforace page on the Task creates a new Opportunity when clicked.

 

I have what I think should be workable code. However, the inline visual force page that I want to show up on the task is displaying "Content cannot be displayed: DML currently not allowed".

 

What I would expect to be displayed is a hyperlink on the text "Create New Opp".

 

This is my visualforce page code:

 

<apex:page standardController="Task" extensions="OppClass">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Q1 2011 Upsell</h1>
Create new opportunity with this campaign:
<!-- My Stufff -->
    <apex:form >
    <apex:commandLink action="!Ops" value="Create New Opp" id="comlink1"/>
    </apex:form>
<!-- END MY STUFF-->
  <!-- End Default Content REMOVE THIS -->
</apex:page>

 

 

My apex code is this (intended to execute when one clicks on the above link):

 

public without sharing class OppClass
{

    public OppClass(ApexPages.StandardController controller) {
    NewOpp();
    }

    public void NewOpp(){
        Opportunity o = new Opportunity(name = 'Acme');
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = 'Acme')},false);
    }
}

 

This is intended to be a simplified proof of concept, but I can't see where I am failing. Does anyone have any suggestions?

Could it be permission settings somewhere in my developer environment? 

 

 

Thanks,

IW

 

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

Hi,

You are inserting the required fields for an opportunity that is stage name and close date. And in the action method you are not calling the method to perform the action when the commandLink is clicked.

 

Try the following code

 

Visualforce Page

 

<apex:page standardController="Task" extensions="OppClass">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Q1 2011 Upsell</h1>
Create new opportunity with this campaign:
<!-- My Stufff -->
    <apex:form >
    <apex:commandLink action="{!NewOpp}" value="Create New Opp" id="comlink1"/>
    </apex:form>
<!-- END MY STUFF-->
  <!-- End Default Content REMOVE THIS -->
</apex:page>

 

 

Controller

 

public without sharing class OppClass
{

    public OppClass(ApexPages.StandardController controller) {
   
    }

    public PageReference NewOpp(){
        Opportunity o = new Opportunity(name = 'Acme',StageName='Test',CloseDate=system.today());
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = 'Acme',StageName='Test',CloseDate=system.today())},false);

        for(Database.SaveResult sr: lsr){
            if(!sr.isSuccess())
                Database.Error err = sr.getErrors()[0];
        }
    return null;
    }
}

 

Let me know if you face any difficulty......

Hope this helps you.

All Answers

sravusravu

Hi,

You are inserting the required fields for an opportunity that is stage name and close date. And in the action method you are not calling the method to perform the action when the commandLink is clicked.

 

Try the following code

 

Visualforce Page

 

<apex:page standardController="Task" extensions="OppClass">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Q1 2011 Upsell</h1>
Create new opportunity with this campaign:
<!-- My Stufff -->
    <apex:form >
    <apex:commandLink action="{!NewOpp}" value="Create New Opp" id="comlink1"/>
    </apex:form>
<!-- END MY STUFF-->
  <!-- End Default Content REMOVE THIS -->
</apex:page>

 

 

Controller

 

public without sharing class OppClass
{

    public OppClass(ApexPages.StandardController controller) {
   
    }

    public PageReference NewOpp(){
        Opportunity o = new Opportunity(name = 'Acme',StageName='Test',CloseDate=system.today());
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = 'Acme',StageName='Test',CloseDate=system.today())},false);

        for(Database.SaveResult sr: lsr){
            if(!sr.isSuccess())
                Database.Error err = sr.getErrors()[0];
        }
    return null;
    }
}

 

Let me know if you face any difficulty......

Hope this helps you.

This was selected as the best answer
IWyattIWyatt

Thank you!

 

Your example was able to get me to a functional proof of concept which is what I needed.

 

I sincerely appreciate your help!

 

Best,

IW