I'd like to create a trigger that automatically assign a value to a lookup field.
I've tried to emulate what I've read on these 3 posts (but without success)

https://developer.salesforce.com/forums?id=906F000000094NcIAI; How to Give Default value to a LookUp field in salesforce; Trigger based on Record Type + Default Value + Lookupfield;

Here is the situation. I have a Product_Sale__c object. Sale_Account__c is a lookup field in that object. That field is looking up for the Name (the Id?) of an account on Account__c. One of the account on Account__c is 'Sales_Revenue'. I'd like that account to be the default one on Sale_Account__c.

Here is what I wrote (after help from Peter Knolle ):

trigger ProductSale_Default_SaleAccount on Product_Sale__c (before insert, before update) {

    Account__c var = [
        SELECT Name 
        FROM Account__c 
        WHERE Name='Sales Revenue'

    for (Product_Sale__c productsale : trigger.New) {
                    productsale.Sale_Account__c = var.Id;

But nothing the field is still blank on the when I create a new "Product Sale", I was expecting it to already show the 'Sales Revenue' account.

Any advices?

3 Answers 3


You need to assign the ID (not the Name field) of the ‘Sales Revenue’ Account__c to the productsale.Account__c.

The error that you are getting is because your query queries the Product_Sale__c object but attempts to assign to the Account__c. The object that you query must be assigned to that type of object.

Your query should change to the following:

Account__c defaultSaleAcc = [
    Select Id
    From Account__c
    Where Name = ‘Sales Revenue’

Your loop body should change to assign the ID

productsale.Sale_Account__c = defaultSaleAcc.Id;

Lookup fields store the ID of the related record, not the name.

Like @JimRae commented, the trigger fires after the user hits the Save button. If you want the Lookup field pre-populated before the user hits the Save button the only option is a custom Visualforce page or implementing the LKID url hack.

Alternatively, you could check to see if the value has been not been set (i.e., == null) in the trigger and then set it to the default if it hasn't been set. Depending on your business rules you may only want to do that on the initial insert and you could add some help text about the defaulting behavior. Note that this alternative approach doesn't display any value until after the Save.

  • Hi Peter, Thanks again for correcting my mistakes. It does make sense that name is given by the Id. But it doesn't populate the field when I create a new instance of the object any reason why? Should I add something like update or something else at the end?
    – Franky
    Commented Jan 1, 2014 at 17:37
  • 2
    Your trigger won't fire until you save the record. If you want a prepopulated value that the user could change, a URL hack would be a better solution. The trigger will not give the user a chance to change the value.
    – JimRae
    Commented Jan 1, 2014 at 18:24
  • Also, your Account query should say "LIMIT 1" at the end so it doesn't return a list. I'm not sure why it's not failing without the limit clause, but it's a better idea in general to use that. Commented Apr 10, 2014 at 23:33
  • Thanks David. It will work if there is always exactly one record. I interpreted that the requirement was there would only be a single 'Sales Revenue' Account, so my query would never fail, because there would only ever be exactly one. A better approach for that might be to get back a List and generate an error if there is not exactly one vs. just letting it generate a system error. The downside to using LIMIT in this case would be that it would hide the invalid case when there were more than one 'Sales Revenue' Account. I may have completely misinterpreted the original question, though. :) Commented Apr 11, 2014 at 0:31

I am doing the exact thing but on standard Case and Account objects. The problem i am facing is that it gives Null Pointer exception with Attempt to de-reference a null object Trigger.DefaultAccName: line 13, column 1: []

Here is the code i am trying to execute

Account defaultAcc = [SELECT Id FROM Account WHERE Name = 'ABC Support'];

for (Case c: trigger.New) {


       c.account.name = defaultAcc.Id;

Not able to make out why?

Regards Deepak

  • welcome to salesforce.stackexchange deepak, you've posted this as an answer(=solution). You'll get more attention posting this as a new question ( salesforce.stackexchange.com/questions/ask ) Commented Apr 24, 2014 at 9:29
  • I think you did not have Any account with name ABC Support thats why defaultAcc is null and you try to assign null value in an id field thats why error is occurred.
    – Tepsi
    Commented Apr 24, 2014 at 9:40
  • HI Tepsi, i have the account with name ABC Support but still it is giving me this error Commented Apr 25, 2014 at 5:02

Deepak, I think you want to use

c.AccountId = defaultAcc.Id;

instead of trying to update the Name with an Id value....

  • Not really sure what you're getting at with this... he's not trying to use a name.
    – Matt Lacey
    Commented May 4, 2014 at 2:21
  • I chose my user name, "newbie", for a good reason. LaceySnr, I can't think of why someone would assign an Id into a Name field, which Deepak seemed to be doing in the statement: "c.account.name = defaultAcc.Id;". I guess that would work, but I'm not sure why one would do it (?)
    – newbie
    Commented May 4, 2014 at 12:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .