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
James KinseyJames Kinsey 

Controller Extension to create SObject from custom object

Is this the correct extension to get the custom object "equipment__c" data and create a new opportunity with fields already populated with equipment__c values? Either this isn't right or I'm referencing it wrong from the visualforce page.

 

public without sharing class OppClass2
{

    private final equipment__c equipment;
    
      public OppClass2(ApexPages.StandardController opportunity) {
            }
      public oppclass2() {
        equipment = [select id, name from Equipment__c
        where id = :ApexPages.currentPage().getParameters().get('id')];
          }
          
      public Equipment__c getequipment() {
          return equipment;  
          }

    public PageReference NewOpp(){
        Opportunity o = new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today());
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today())},false);
        for(Database.SaveResult sr: lsr){
            if(!sr.isSuccess())
                Database.Error err = sr.getErrors()[0];
        }
                return (new ApexPages.StandardController(o)).edit();

        }

}

 

ReidCReidC

looks to me like you're using it as a standalone app and not an extension.  An extenion would typically have a different constructor: 

 

    public  ApexPages.StandardController con { get; set;}  
    public Milestone1_TimelineCon ( ApexPages.StandardController stc){
        con = stc;
    }

 

And rather than querying for your Equipment__c using SOQL you would simply get it from the standard controller. 

 

Likewise in your VF page you would have something like

 

<apex:page sidebar="false" showheader="true" standardController="Milestone1_Milestone__c" extensions="Milestone1_TimelineCon" >

 

Note the "extensions" property.

HTH

sebcossebcos

Hi, 

normally you should be able to get the info from the extended object with standardcontroller's method getRecord like as shown here:

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm

 

Here is the sanitized code:

 

 

public class myControllerExtension {
    private final Account acct;
    
   public myControllerExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
}
Then you can use the data in the sobject to create your logic (e.g. generate an opportunity).
    public String getGreeting() {
        return 'Hello ' + acct.name + ' (' + acct.id + ')';
    }
}
Beware that only the data that is bound to the controller in the visualforce page is available to your controller.
Hence the visualforce page should contain the name field:
<apex:page standardController="Account" extensions="myControllerExtension">
    {!greeting} <p/>
    <apex:form>
        <apex:inputField value="{!account.name}"/> <p/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>
 

 

can you share your visualforce markup or part of it? It might be easier to see what is wrong with it.

 

James KinseyJames Kinsey

Ok, I removed and some others:

public oppclass2() {
        equipment = [select id, name from Equipment__c
        where id = :ApexPages.currentPage().getParameters().get('id')];

 

And now my extension looks like:

public class OppClass2
{
    
    private final equipment__c equipment;
    
               
     public OppClass2(ApexPages.StandardController stdController) {
        this.equipment= (equipment__c)stdController.getRecord();
     }

      public Equipment__c getequipment() {
          return equipment;  
          }

    public PageReference NewOpp(){
        Opportunity o = new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today());
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today())},false);
        for(Database.SaveResult sr: lsr){
            if(!sr.isSuccess())
                Database.Error err = sr.getErrors()[0];
        }
                return (new ApexPages.StandardController(o)).edit();

        }

}

 

 

My VF page looks like this but I am not able to insert this page as a button on the Equipment__c custom object.

<apex:page standardcontroller="opportuntiy" extensions="OppClass2">
<apex:form >
 <apex:pageBlock >
 <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!save}"/>
 <apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
   <apex:pageBlockSection >
       <apex:pageBlockSectionItem >
           <apex:inputField value="{!opportunity.name}" />
       </apex:pageBlockSectionItem>
            <apex:inputfield Value="{!opportunity.amount}"/>
            <apex:outputfield Value="{!equipment.name)"/>

  </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

I thank you for your inputs

sebcossebcos

Hi,

the problem is that the visualforce page is using an opportunity standard controller while it should be using the equipment__c standarcontroller.

 

Here is the revised code:

 

<apex:page standardcontroller="Equipment__c" extensions="OppClass2">
<apex:form >
 <apex:pageBlock >
 <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!save}"/>
 <apex:commandButton value="Cancel" action="{!cancel}"/>
 <apex:commandButton value="New opp" action="{!NewOpp}"/>
</apex:pageBlockButtons>
   <apex:pageBlockSection >
           <apex:pageBlockSectionItem >
                   <apex:outputLabel value="Opportunity Name"/>
                   <apex:inputText value="{!opportunityName}" />
           </apex:pageBlockSectionItem>
            
           <apex:pageBlockSectionItem >
                   <apex:outputLabel value="Opportunity Amount"/>
                   <apex:inputText value="{!opportunityAmount}" />
           </apex:pageBlockSectionItem>
           
            <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Equipment Name"/>
                    <apex:outputField Value="{!Equipment__c.name}"/>
            </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


public class OppClass2
{
    
    private final equipment__c equipment;
    public String OpportunityName {get;set;}
    public String OpportunityAmount {get;set;}
    
               
     public OppClass2(ApexPages.StandardController stdController) {
        this.equipment= (equipment__c)stdController.getRecord();
     }

      public Equipment__c getequipment() {
          return equipment;  
          }

    public PageReference NewOpp(){
        Opportunity o = new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today());
        Database.SaveResult[] lsr = Database.insert(new Opportunity[]{o, new Opportunity(name = equipment.name,StageName='New Opportunity',CloseDate=system.today())},false);
        for(Database.SaveResult sr: lsr){
            if(!sr.isSuccess())
                Database.Error err = sr.getErrors()[0];
        }
                return (new ApexPages.StandardController(o)).edit();

        }

}

 

 

I would recommend going through the visualforce workbook which will give you the basics to understand Controllers and Visualforce pages:

http://www.salesforce.com/us/developer/docs/workbook_vf/index.htm