You need to sign in to do that
Don't have an account?
Nagesh Nagarajarao
Set default value for lookup field using trigger
Hi,
I have created the following trigger to set the default value for a lookup field.
But it doesn't seem to work. Need help.
I have created the following trigger to set the default value for a lookup field.
But it doesn't seem to work. Need help.
trigger DefaultCust on DOR__c (before insert, before update){ for(DOR__c InternalCust : Trigger.New) { InternalCust.Internal_Customer__c='ID Industries'; // Required default value is 'ID Industries' for Internal_Customer__c field } }
You are going in wrong direction. I have just put example of account object.You need to query "Internal_Customer__c" lookup object not the account object. :)
So Code Like : I have added comment's in code and replace them with object API Name.
Thanks
Hemant
All Answers
what is the type of Internal_Customer__c field ? and if it lookup field then use record ID instead of using lookup field record value name :
something link below :
trigger DefaultCust on DOR__c (before insert, before update){
yourLookupObjectName__c objRec = [select id,Name from yourLookupObjectName__c where Name = 'ID Industries'];
for(DOR__c InternalCust : Trigger.New) {
InternalCust.Internal_Customer__c = objRec.Id;
// Required default value is 'ID Industries' for Internal_Customer__c field
}
}
[PS: change the yourLookupObjectName__c with your object API name ]
Thanks
let us know if it helps you and kindly close your query by choosing best answer so it will make proper solution for others in future
First You should Understand that lookup does not take string value.If you need "ID Industries" value in "Internal_Customer__c" field then you need to put id of "ID Industries". Please mark this solved if this help you.
Thanks
Hemant
I inserted the query to obtain default value (account name) but it is still not working.
Is there any other way?
trigger DefaultCust on DOR__c (before insert, before update){
==Query Default value record==
Like :
List<Account> oAcc = [Select Id,Name from Account Where Name ='ID Industries'];
for(DOR__c InternalCust : Trigger.New) {
if(oAcc >0)
InternalCust.Internal_Customer__c= oAcc[0].ID;
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
Can You Please show me your latest updated code i will made necessary change.
Thanks
Hemant
Error: Compile Error: Comparison arguments must be compatible types: List<Account>, Integer at line 4 column 4
Do I need to change anything else?
trigger DefaultCust on DOR__c (before insert, before update){
List <Account> oAcc = [Select Id,Name from Account Where Name ='ID Industries'];
for(DOR__c InternalCust : Trigger.New) {
if(oAcc.size() >0)
InternalCust.Internal_Customer__c= oAcc[0].ID;
}
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
THanks for the updated record.
But it is still not working.
Thanks,
Raj
You are going in wrong direction. I have just put example of account object.You need to query "Internal_Customer__c" lookup object not the account object. :)
So Code Like : I have added comment's in code and replace them with object API Name.
Thanks
Hemant