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

Default Value for lookup field
Hi there, I'm creating an apex page to override the normal layout and I want a lookup field to have a default value which I am coding into the custom controller. The code I have so far is
Log_Book__c[] logbook; public Void<Log_Book__c> getlogbook(){ logbook = [SELECT Id FROM Log_Book__c WHERE Owner.Id = :UserInfo.getUserId() LIMIT 1]; } public List<Objective__c> objective = new List<Objective__c>(); public List<Objective__c> getObjective(){ return objective; } public void addObjective(){ objective.add(new Objective__c(Log_Book__c = logbook.Id)); }
when I try to save it I get this error
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Log_Book__c>
I have tried just putting logbook instead of logbook.Id but then I get the following error:
Error: Compile Error: Invalid initial expression type for field Objective__c.Log_Book__c, expecting
Any ideas on this would be great.
Thank you for your time
Ezmo
correction -
if(logbook.size() > 0)
objective.Log_Book__c = logbook[0].id;
All Answers
you can hadle it through two ways...one by trigger and another one VF page override new button.
why you are overriding hole sf page for this small change.
As per kritin, just write before trigger on that object for assigning default value.
This isn't the whole of the code just the part I am stuck on. I am creating a complete custom page so that users can fill in information for two objects at the same time. What I need to do is get the lookups to automatically link to the appropriate parent record, hence the default value.
Whether it is done by the custom controller or a trigger I am still unsure of the coding.
Thanks
definately u can write trigger for assigning lot's of default value to that fields of that object.
it can be anything like lookup,master-detail relashanship's fields & all.
Ok I'm trying to write a trigger to this now so far my code is
I now get the following error:
Thanks
correction -
if(logbook.size() > 0)
objective.Log_Book__c = logbook[0].id;
That worked perfectly thank you all very much for your input it is MUCH appreciated!
Many many thanks,
Ezmo