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
Abhishek KedariAbhishek Kedari 

How to pass values to visualforce page

Hi,
I have create new 'abcd' button which is a standard button of 'abcd' object and when I clicked on that it launches my visualforce page. This object is added into opportunity page . Now what I want to do is whenever I click on create new 'abcd ' button, I want to pass opportunity ID to this visualforce page. How I can do that ?

Please let me know if my question is not clear.

Thanks,
Abhishek
Best Answer chosen by Abhishek Kedari
BalajiRanganathanBalajiRanganathan
nstead of shipment = new Shipments__c();  you have to use
this.acct = (Shipments__c)controller.getRecord();

also in the VF, you have to use your object name Shipments__c and not Shipment__c
<apex:inputField value="{!Shipments__c.Related_Opportunity__c}" required="true"/>


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

All Answers

BalajiRanganathanBalajiRanganathan
did you create the list button?

if yes, then change the content source as URL in the button definition and add below template as URL
/apex/<yourpage>?CF<relation_id>={!Opportunity.Name}&CF<relation_id>_lkid={!Opportunity.Id}&sfdc.override=1
<yourpage> - your VF Name
<relation_id> - go to your object 'abcd', click on the Opportunity RelationShip field, in the URL you can see the relation_id.

If you dont have relationship with Opportunity on 'abcd', then you can just use below url
/apex/<yourpage>?oppid={!Opportunity.Id}
 
Abhishek KedariAbhishek Kedari
Thanks Balaji for your reply.

User-added image

I do not see any URL option here.
This is like my 'abcd' button mentioned above for custom object.
I have added this object on opportunity page.
and whenever I click on this button it takes me to my visualforce page.
What I want is if I am clieck on this button from opportunity window, I should be getting opportunity id in my visualforce page.

Thanks,
Abhishek
BalajiRanganathanBalajiRanganathan
i thought you are creating a new button, but it looks like you are overriding the standard button.

if you override the new button, you dont need to do any thing. just add the opportunity relationship field in your VF. 

it will get the name when you click the overriden new button from Opportunity.

So all you have to do now is add your relationship field using apex:inputfield in the VF. thats it
Abhishek KedariAbhishek Kedari
Thanks Balaji. But I am new to salesforce. I tried searching for what you are saying but could not solve the problem.
Problem is - 
So there is a field in shipment object with name "Related_Opportunity" of type lookup and
there is nothing in opportunity which maps to Shipment.
Now I am not sure how I can write this relation in visualforce page.
My visualforce page already have this - 
​<apex:inputField value="{!shipment.Related_Opportunity__c}" required="true"/>
Can you explain what kind of relation I need and how I can implement it. (Steps would be much better)

I appreciate your help in this regards.


Thanks,
Abhishek

 
BalajiRanganathanBalajiRanganathan
Did you try creating new Shipment from Opportunity related list?

it should populate the Opportuniy into your {!shipment.Related_Opportunity__c} field in your VF page
Abhishek KedariAbhishek Kedari
Hi,

 I treied and I got 
System.NullPointerException: Attempt to de-reference a null object
and I think its logical, because there is no shipment object right now when I click on new. How I can say give me shipment.Related_Opportunity__c if shipment itself is null.


Thanks,
Abhishek

 
BalajiRanganathanBalajiRanganathan
are u using standard controller or controller extension?

in both case you should be using Shipment__c and not shipment.
​<apex:inputField value="{!Shipment__c.Related_Opportunity__c}" required="true"/>



 
Abhishek KedariAbhishek Kedari
​<apex:page standardController="Shipments__c" extensions="shipment" >
I am using this.
 where extension is my shipment apex class.
 
public class shipment {

   public Shipments__c shipment{get;set;}
   public String user{get;set;}
   public boolean editMode = false;
   ApexPages.standardController m_sc = null;
   String redirectURL = null;
   public String var {get;set;}
   

    public shipment(ApexPages.StandardController controller) {

        user = Userinfo.getName();
        m_sc = controller;
       
/*
        Here I want to get opportunity information
*/
               // Create new shipment
                shipment = new Shipments__c();  
            }
                 
    }

Alos wheb I used {!Shipment__c.Related_Opportunity__c}, it gave me error - 
                     
Error: Unknown property 'Shipments__cStandardController.Shipment__c'


Thanks,
Abhishek 
 
BalajiRanganathanBalajiRanganathan
nstead of shipment = new Shipments__c();  you have to use
this.acct = (Shipments__c)controller.getRecord();

also in the VF, you have to use your object name Shipments__c and not Shipment__c
<apex:inputField value="{!Shipments__c.Related_Opportunity__c}" required="true"/>


http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm
This was selected as the best answer