You need to sign in to do that
Don't have an account?

How do I pass a lookup field value to a Visualforce page
Hi Guys,
Any idea of how to pass a value to a loookup field on a visual force page. I tried passing it through URL and then pass it to the page using controller's getObject Method. Here is the code
My Url to Pass the Id of contact
apex/Fact_Find_Page_1?retURL=%2Fa0q%2Fo&save_new=1&sfdc.override=1?cid={!Contact.Id}
Here is my controller code:
Can anyone let me know what is wrong in my code?
Thanks so much
Any idea of how to pass a value to a loookup field on a visual force page. I tried passing it through URL and then pass it to the page using controller's getObject Method. Here is the code
My Url to Pass the Id of contact
apex/Fact_Find_Page_1?retURL=%2Fa0q%2Fo&save_new=1&sfdc.override=1?cid={!Contact.Id}
Here is my controller code:
public Fact_Find__c getFf() { if(ff == null) ff = new Fact_Find__c(); ff.Contact__c = ApexPages.currentPage().getParameters().get('cid'); return ff; }But this is not prefilling the contact lookup field on the visual force page.
Can anyone let me know what is wrong in my code?
Thanks so much
I have used newFF to show the new record. Therefore it should be inserted in the Save function.
Importantly, as I do not know the purpose of ff, therefore if it is not needed now (because we have newFF), remove it.
So basically you should modify the class code to the following (assuming that ff is not needed now) -
This code creates two new records when the VF Page is loaded - one for Fact_Find__c object and other for Life_Style_and_Financial_goals__c object.
When you click the Save button, it should save both these records.
Thanks
AR
If this solves your problem then please mark it as best answer.
All Answers
Did the issue resolve? Or is it that the value is not getting passed from the URL itself?
Thanks
AR
The issue isn't resolved. I have custmised the new button of the custom Object to include the URL with Contact's Id Value. When this new button is clicked from a contact page the URL is getting the Contact Id but it is not being passed to the Visualforce page. Not sure what is wrong with my code.
Share the VF Code that is sending the contact Id.
This is my URL /apex/Fact_Find_Page_1?retURL=%2Fa0q%2Fo&save_new=1&sfdc.override=1?cid={!Contact.Id}
and this is my Extension code. Sorry I wasn't clear. i am using standard controller and this is my xtension class
Because as I know when you try to customize an existing button like New you just have option to choose a VF Page. There is NO option to mention a URL over there.
I created a custom button with URL mnetioned above which calls my visualforce page and also sends contact id with the variable 'cid' and placed this custom button in the related list of Contact page as this Fact Find is the child object for a contact. Am I doing it in a wrong way? If yes, Can you please suggest me of how to proceed with this? I am new to Visual force pages and controllers
Thanks,
Bathy.
For ease, find below the configurations I have done as a test which are working.
1. Here I have a custom button on a Custom Object (Service Request) which is child of Contact.
2. On click of this button a URL is invoked that directs to a VF Page based on the Standard Controller 'Contact' having an extension class. This extension class can be used for any further logic implementation. And consider this class similar to what you must have built (in which you are having the 'public Fact_Find__c getFf()' function).
3. I am able to pass the contact Id to the VF Page and inturn to the class. You can check the Logs that the id is coming.
Custom Button Configuration -
URL Value = /apex/DisplayUserInfo?retURL={!Contact.Id}& (/apex/DisplayUserInfo?retURL=001U000001RpCTX&id={!Contact.Id})cId (/apex/DisplayUserInfo?retURL=001U000001RpCTX&id={!Contact.Id})={!Contact.Id} (/apex/DisplayUserInfo?retURL=001U000001RpCTX&id={!Contact.Id})
VF Page Sample -
Controller Extension Class Sample -
Thanks
AR
If this reply solves your problem then please mark it as best answer.
URL Value = /apex/DisplayUserInfo?retURL={!Contact.Id}&cId={!Contact.Id}
I changed the standard controller to Contact and added the line ff.Contact__c = ApexPages.currentPage().getParameters().get('cid'); in the public Fact_Find__c getFf() Function. It is still not showing up in the contact Lookup field. It is just empty. Not sure where I went wrong. I am pasting my extension class here for your reference. I tried to put this line of code in the public newFactFindController(ApexPages.StandardController controller) Function but it is giving the error System.NullPointerException: Attempt to de-reference a null object. SO I placed in getFf Function.
.
This is my Fact Find Visual Force page code. I am just pasting the two lines important Here is the resullt an empty lookup field
Thanks,
The controller code needs some modification. I have added the lines of code. You can use it to test.
The VF Page should remain on Contact itself because it is the parent. The URL behind the custom button should continue to send the Contact Id as a parameter.
The Controller class needs to be modified -
Thanks
AR
If this reply solves your problem then please mark it as best answer.
Controller Class
VF Page
Thanks
AR
I just copied the code. here is the result
apex/Fact_Find_Page_1?id={!Contact.Id}
It just worked. it is displaying the contact name in the lookup field
So now the issue should be resolved. In your original VF Page within the <apex:pageBlockSection> component, you can use the newFF function to get the values into the inputfield components.
<apex:inputfield value="{!newFF.Name}"/>
Something similar to the following -
Thanks
AR
If this solves your problem then please mark it as best answer.
that worked on the visualforce page but when i create the record and save it didnot have the contact in the detail page.
I have used newFF to show the new record. Therefore it should be inserted in the Save function.
Importantly, as I do not know the purpose of ff, therefore if it is not needed now (because we have newFF), remove it.
So basically you should modify the class code to the following (assuming that ff is not needed now) -
This code creates two new records when the VF Page is loaded - one for Fact_Find__c object and other for Life_Style_and_Financial_goals__c object.
When you click the Save button, it should save both these records.
Thanks
AR
If this solves your problem then please mark it as best answer.
I have used ff and lf in all my Visualforce pages for the input fields. If I remove them I have to change all my pages. Can I replace newFF with ff and newLF with lf ?
And when I save the above class I got an error message Error: Compile Error: The method Life_Style_and_Financial_goals__c getLf() is referenced by Visualforce Page (FactFindPage3) in salesforce.com. Remove the usage and try again. at line 52 column 50
FactFindPage3 is the last page in my wizard and it uses the Save() Function.
I used ff and lf in my visualforce pages in input fields as ff.Name and lf.Name etc. Should I replace ff and lf with newFF and newLF in all the input fields in my VF pages? I cannot remove them now. can you suggest how should I proceed with this?
And for the error you can put back the required method.
I just replaced them and recreated the pages. No issues now contact is prefilled and Fact find is saved with contact. Thank you so much for being patient with me and helping me out.
Regards,
Bathy