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
ALAL 

Create default value for lookup field

I have a lookup field and would like to create a default value. There are only two values that I have for the lookup. Here's the code I have so far

trigger AccountName on Opportunity (before insert) {
    
    List<Account> accLu = new List<Account>(); 
    
    for(Opportunity fieldLU: Trigger.new){
             
   Opportunity Account_Name__c = [SELECT ID (NAME FROM ACCOUNT) from Opportunity where Id =:accLu];
        
    }

}
Best Answer chosen by AL
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Albert,

As I have mentioned earlier that this possible with an Apex Trigger. Here we go :)

First of all, this is what I assume you have it already.
1) An Account record whoes names is "Government Account 1". Second Account record whoes name is "Government Account 2"
2) A field named Account_Name_Custom__c with twp picklist values in it.  Government Account 1 & Government Account 2
3) A checkbox field named Is_Government_Account_PreSale__c

Once you have them ready I would invite you to try followig trigger.
I have tested into my org and it is working per requirements.

Trigger Code:
 
Trigger AccountAutoAssign On Opportunity(Before Insert, Before Update, After UnDelete){
    
    List<Account> fetchingGovAccounts = [Select Id, Name FROM Account WHERE Name =: 'Government Account 1' OR Name =:'Government Account 2'];
    
    Map<String, Id> accountIdWithNameMap = New Map<String, Id>();
    
    If(!fetchingGovAccounts.IsEmpty())
    {
        For(Account act : fetchingGovAccounts )
        {
            accountIdWithNameMap.put(act.Name, act.Id);
        }
    }
    
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        
        For(Opportunity Opp : Trigger.New){
            If(Opp.Account_Name_Custom__c == 'Government Account 2' && Opp.Is_Government_Account_PreSale__c == TRUE)
            {
                Opp.AccountId = accountIdWithNameMap.get('Government Account 2');
            }
            
            else{
                Opp.AccountId = accountIdWithNameMap.get('Government Account 1');
            }
        }
    }
}

Output of the trigger:

1) Let's say I am entering an Opportunity named "Test Opp - 1" with "Government Account 1" as a value in Account_Name_Custom__c picklist field then by default "Account Name" field should be Government Account 1

User-added image
Now once I save the record this is what happens:

User-added image

2) Let's say I were to edit this record and make that checkbox checked and make Account_Name_Custom__c as Government Account 2.

This is what happens,

User-added image

Now once I hit the save button then Account Name is changed ! As expected!

User-added image


Hope it helps & if it solves the query then please mark it as BEST ANSWER!
 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH

This is possible. What are the two values you have for the lookup field?

If an Opportunity comes with name let's say "New Opp-1" and there is no value in Account field. Now, what deterimines which account (out of 2) would go in there?

Hansel Pena 4Hansel Pena 4
Just FYI @Albert White, it is a bad practice to have a SOQL query inside a loop, also it will affect the SOQL Limit number for the process you are creating.
ALAL
Thanks Govind. The two values are: Government Account 1 and Government Account 2 and the field name is just Account Name__c. 
HARSHIL U PARIKHHARSHIL U PARIKH
I see! But what determines which account it should be?
I mean let's say I enter 10 Opportunities from standard UI page, Is the first 5 would have "Government Account 1" and remaining 5 would have "Government Account 2"?
ALAL
The default account would be "Government Account 1". The "Government Account 2" could display depending on one of two conditions:

1. Another custom field with data type picklist has "Government Account 2" as its value. 
2. Workflow rule (don't know if it would work for a lookup field). If the picklist field "Is Government Account PreSale" is checked to yes, then the Account Name__c field would be "Government Account 2".  
HARSHIL U PARIKHHARSHIL U PARIKH

So you have two fields that determines Account info on Opportunity?
1) Account (This is a standard Lookup field for an Account)
2) Account_Name__c as a picklist field with "Government Account 1" and "Government Account 2" as picklist values?

If yes, then
Now, this is what I am getting so far..

- Whenever Opportunity comes by default it will have "Government Account 1" as a default value in Standard Account lookup field. But if it has "Government Account 2" as a value on Account_Name__c picklist field AND "Is Government Account PreSale" checkbox is checked then it will have "Government Account 2" as a value in standard Account field.

Is this correct?
 

ALAL
Hi Govind, 

Yes that is correct. 
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Albert,

As I have mentioned earlier that this possible with an Apex Trigger. Here we go :)

First of all, this is what I assume you have it already.
1) An Account record whoes names is "Government Account 1". Second Account record whoes name is "Government Account 2"
2) A field named Account_Name_Custom__c with twp picklist values in it.  Government Account 1 & Government Account 2
3) A checkbox field named Is_Government_Account_PreSale__c

Once you have them ready I would invite you to try followig trigger.
I have tested into my org and it is working per requirements.

Trigger Code:
 
Trigger AccountAutoAssign On Opportunity(Before Insert, Before Update, After UnDelete){
    
    List<Account> fetchingGovAccounts = [Select Id, Name FROM Account WHERE Name =: 'Government Account 1' OR Name =:'Government Account 2'];
    
    Map<String, Id> accountIdWithNameMap = New Map<String, Id>();
    
    If(!fetchingGovAccounts.IsEmpty())
    {
        For(Account act : fetchingGovAccounts )
        {
            accountIdWithNameMap.put(act.Name, act.Id);
        }
    }
    
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        
        For(Opportunity Opp : Trigger.New){
            If(Opp.Account_Name_Custom__c == 'Government Account 2' && Opp.Is_Government_Account_PreSale__c == TRUE)
            {
                Opp.AccountId = accountIdWithNameMap.get('Government Account 2');
            }
            
            else{
                Opp.AccountId = accountIdWithNameMap.get('Government Account 1');
            }
        }
    }
}

Output of the trigger:

1) Let's say I am entering an Opportunity named "Test Opp - 1" with "Government Account 1" as a value in Account_Name_Custom__c picklist field then by default "Account Name" field should be Government Account 1

User-added image
Now once I save the record this is what happens:

User-added image

2) Let's say I were to edit this record and make that checkbox checked and make Account_Name_Custom__c as Government Account 2.

This is what happens,

User-added image

Now once I hit the save button then Account Name is changed ! As expected!

User-added image


Hope it helps & if it solves the query then please mark it as BEST ANSWER!
 
This was selected as the best answer