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
EzmoEzmo 

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

Best Answer chosen by Admin (Salesforce Developers) 
CLKCLK

correction -

if(logbook.size() > 0)

   objective.Log_Book__c = logbook[0].id;

 


All Answers

kritinkritin

you can hadle it through two ways...one by trigger and another one VF page override new button.

 

CLKCLK

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.

EzmoEzmo

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

CLKCLK

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.

EzmoEzmo

Ok I'm trying to write a trigger to this now so far my code is

 

 

trigger logbooklookup on Objective__c (before insert) {
    for(Objective__c objective: Trigger.new){
    
    Log_Book__c[] logbook;
    
    logbook = [SELECT Id FROM Log_Book__c WHERE Id = :UserInfo.getUserId() LIMIT 1];
    
    objective.Log_Book__c = logbook;
    //objective.Log_Book__c refers to the lookup field.
    }
}

 

I now get the following error:

 Illegal assignment from LIST<Log_Book__c> to Id at line 8 column 5

 

Thanks

 

CLKCLK

correction -

if(logbook.size() > 0)

   objective.Log_Book__c = logbook[0].id;

 


This was selected as the best answer
EzmoEzmo

That worked perfectly thank you all very much for your input it is MUCH appreciated!

 

Many many thanks,

 

Ezmo