You need to sign in to do that
Don't have an account?
AL
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];
}
}
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];
}
}
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:
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
Now once I save the record this is what happens:
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,
Now once I hit the save button then Account Name is changed ! As expected!
Hope it helps & if it solves the query then please mark it as BEST ANSWER!
All Answers
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?
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"?
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".
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?
Yes that is correct.
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:
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
Now once I save the record this is what happens:
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,
Now once I hit the save button then Account Name is changed ! As expected!
Hope it helps & if it solves the query then please mark it as BEST ANSWER!