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
Matthew BirckbichlerMatthew Birckbichler 

VisualForce Page to create new custom object from button in Opportunity related list

I will start of by saying, I am pretty familiar with APEX, but brand new toVisualForce.
I have a custom objects called Opportunity_Contact__c, in the related list for this on Opportunity, I need to replace the current 'new' Button with a custom button to a VF page.
First issue I am having is, if I use the standard controller 'Opportunity_Contact__c', when I click the button, I recieve this...
'Id value 00655000004MBYc is not valid for the Opportunity_Contact__c standard controller'.
I realize that this is because it is passing the Opportunity ID into the VP page, and I am not using the Opportunity standard controller, but I am unsure how to move forward.

My code thus far :
<apex:page standardController="Opportunity_Contact__c" extensions="createOpportunityContactExtension">
<apex:form >
<apex:pageBlock title="Create New Opportunity Contact" mode="new">

    <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:pageBlockButtons>
    
    <apex:pageBlockSection columns="1">
        <apex:inputField value="{!Opportunity_Contact__c.Opportunity__c}"/>
    </apex:pageBlockSection>
    
</apex:pageBlock>
</apex:form>
</apex:page>
public class createOpportunityContactExtension {
    private final Opportunity_Contact__c oppCon;    
    public createOpportunityContactExtension(ApexPages.StandardController stdController){
        this.oppCon = (Opportunity_Contact__c)stdController.getRecord();
    }
       
}


 
Best Answer chosen by Matthew Birckbichler
Deepak GulianDeepak Gulian
public class createOpportunityContactExtension {
    private final Opportunity_Contact__c oppCon;    
    public createOpportunityContactExtension(ApexPages.StandardController stdController){
        this.oppCon = (Opportunity_Contact__c)stdController.getRecord();
        oppCon.Opportunity__c = apexpages.currentpage().getparameters().get('oppId');
    }
       
}
Button or Link URL :  /apex/VFPAgeName?oppId={!Opportunity.Id}
 

All Answers

Deepak GulianDeepak Gulian
Opportunity Id (006)55000004MBYc Opportunity Id thats why its giving you an error. Its not an Opportunity_Contact__c Id.
R Z KhanR Z Khan
U need to create a button on the child object
Matthew BirckbichlerMatthew Birckbichler
R Z Khan, I did create the button on the child object(Opportunity_Contact__c)
R Z KhanR Z Khan
are you passing opportunity Id form the button? where is it coming form?
Deepak GulianDeepak Gulian
public class createOpportunityContactExtension {
    private final Opportunity_Contact__c oppCon;    
    public createOpportunityContactExtension(ApexPages.StandardController stdController){
        this.oppCon = (Opportunity_Contact__c)stdController.getRecord();
        oppCon.Opportunity__c = apexpages.currentpage().getparameters().get('oppId');
    }
       
}
Button or Link URL :  /apex/VFPAgeName?oppId={!Opportunity.Id}
 
This was selected as the best answer
Deepak GulianDeepak Gulian
Also include all the required fields of "Opportunity_Contact__c" in VF page then it will work!