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
Nagesh NagarajaraoNagesh 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.
 
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
}
}

 
Best Answer chosen by Nagesh Nagarajarao
Hemant_SoniHemant_Soni
Hi Nagesh Nagarajarao,

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 :
trigger DefaultCust on DOR__c (before insert, before update){
List <"Internal_Customer__c lookup Object Api Name"> oAcc = [Select Id,Name from "Internal_Customer__c lookup Object Api Name" Where Name ='ID Industries'];
for(DOR__c InternalCust : Trigger.New) {
if(oAcc >0)
InternalCust.Internal_Customer__c= oAcc[0].ID;
// Required default value is 'ID Industries' for Internal_Customer__c field
}
}
I have added comment's in code and replace them with object API Name.

Thanks
Hemant
 

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi Nagesh,
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
Hemant_SoniHemant_Soni
Hi Nagesh Nagarajarao,
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".
trigger DefaultCust on DOR__c (before insert, before update){

==Query Default value record==
Like :
Account oAcc = [Select Id,Name from Account Where Name ='ID Industries'];

for(DOR__c InternalCust : Trigger.New) {
InternalCust.Internal_Customer__c= oAcc.ID;
// Required default value is 'ID Industries' for Internal_Customer__c field
}
}
Please mark this solved if this help you.

Thanks
Hemant 
Nagesh NagarajaraoNagesh Nagarajarao
Hi Hemant,
I inserted the query to obtain default value (account name) but it is still not working.

Is there any other way?
Gururaj BGururaj B
oAcc object should be defined as list object like list<Account>
Maharajan CMaharajan C
oAcc.ID  ==> oAcc[0].ID

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
Hemant_SoniHemant_Soni
Hi Nagesh Nagarajarao,

Can You Please show me your latest updated code i will made necessary change.

Thanks
Hemant
Nagesh NagarajaraoNagesh Nagarajarao
@ Mahajan, my updated code is as below. But I am getting error message as;
Error: Compile Error: Comparison arguments must be compatible types: List<Account>, Integer at line 4 column 4
 
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 >0)
InternalCust.Internal_Customer__c= oAcc[0].ID;
// Required default value is 'ID Industries' for Internal_Customer__c field
}
}
Do I need to change anything else?
Maharajan CMaharajan C
Sry its my mistake!!!

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
Nagesh NagarajaraoNagesh Nagarajarao
Hi Mahajan,

THanks for the updated record.
But it is still not working.
  1. Not sure whether a lookup field can contain a default value?
  2. With the same code I tried to set the default value to a normal text field, but it is still not working.
Maharajan CMaharajan C
Check did you have 'ID Industries' Account record in your org because the code works for both the text field and lookup fields. to me

Thanks,
Raj
Hemant_SoniHemant_Soni
Hi Nagesh Nagarajarao,

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 :
trigger DefaultCust on DOR__c (before insert, before update){
List <"Internal_Customer__c lookup Object Api Name"> oAcc = [Select Id,Name from "Internal_Customer__c lookup Object Api Name" Where Name ='ID Industries'];
for(DOR__c InternalCust : Trigger.New) {
if(oAcc >0)
InternalCust.Internal_Customer__c= oAcc[0].ID;
// Required default value is 'ID Industries' for Internal_Customer__c field
}
}
I have added comment's in code and replace them with object API Name.

Thanks
Hemant
 
This was selected as the best answer
Maharajan CMaharajan C
Yes Hement you are correct i did't see the previous comments properly i just gave the example to query the Account Record instead of Nagarajarao orginal parent object for DOR__c record
Nagesh NagarajaraoNagesh Nagarajarao
THanks Hemant the updated trigger worked