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

Assign/Retrieve value from lookup field

Hi. I am sure this is something simple but I have had no success in getting it to work. I have tried two different approaches and can get neither to work.

Basically, I have two custom objects - a parent and child. I have created my own page and have bound fields to the child custom object. I click on save and everything works fine. However, when I add a lookup field on the child object (populated with the parent object records), I can populate it correctly but I cannot preselect the  relevant parent object value (insert the parent object id into the lookup field for the child object) which I can then save as part of saving the child object.

So the first approach I took was to populate the lookup field with the name value but when I tried to save the record it didn't work and I now realise it needs the ID value to be inserted into the database and not the name value. I have not been able to work out how to pre-select the lookup field with the ID for the parent object. I am assuming that once I can do this then it should save normally as before.

The second approach I thought would be simpler but still couldn't get it to work. Basically as the parent object ID is passed to the child object in the query string parameter I thought I would use it to populate the lookup field in the database (and not have this field show on the page I created). I can get everything to save but for whatever reason it doesn't save the ID value and instead treats it as a null and not the actual ID value. Any assistance would be greatly appreciated.

If this doesn't make sense please let me know and I will provide more information.

Snippets of my code on this approach follows:

ChildObject__c cObj;
static string parentId = System.currentPageReference().getParameters().get('id');

public PageReference save() {

cObj.ParentObjID__c = parentId;
insert cObj;
PageReference cObjPage = new PageReference('/' +;
return cObjPage;

public ChildObject__c getChildObj() {
If(cObj == null) cObj = new ChildObject__c();
return cObj;

public void setChildObj(ChildObject__c f) {}


Message Edited by AussieBattler on 10-29-2007 02:01 PM

Hi. If anybody is interested. I managed to get the second approach working. All I had to do was change the declaration of the parentId from static to private. Grrr.