You need to sign in to do that
Don't have an account?
abdul kalam 13
Initial term of field expression must be a concrete SObject: List<Horticulture__c>
Hai,
I have writter a trigger on custom obj called as Horticulture__c to avoid duplicate entries/names in the table. While executing my trigger it shows error like "Initial term of field expression must be a concrete SObject: List<Horticulture__c>". pls give solution.
trigger Duplicate_Farmer_Horti on Horticulture__c (before insert,before update)
{
for (Horticulture__c H:Trigger.New)
{
List<Horticulture__c> Horti = [select id from Horticulture__c where Applicant_Name__c=:H.Applicant_Name__c];
if(Horti.size()>0)
{
Horti.Applicant_Name__c.addError('You can not create duplicate name');
}
}
}
I have writter a trigger on custom obj called as Horticulture__c to avoid duplicate entries/names in the table. While executing my trigger it shows error like "Initial term of field expression must be a concrete SObject: List<Horticulture__c>". pls give solution.
trigger Duplicate_Farmer_Horti on Horticulture__c (before insert,before update)
{
for (Horticulture__c H:Trigger.New)
{
List<Horticulture__c> Horti = [select id from Horticulture__c where Applicant_Name__c=:H.Applicant_Name__c];
if(Horti.size()>0)
{
Horti.Applicant_Name__c.addError('You can not create duplicate name');
}
}
}
Hope the below code should solve your issue.
Thanks!!
All Answers
Salesforce best practices does not allow SOQL inside for loop.
Please update your code as mentioned below :
trigger Duplicate_Farmer_Horti on Horticulture__c (before insert,before update)
{
List<String> applicantNameList = new List<String>();
for(Horticulture__c H:Trigger.New){
applicantNameList.add(H.Applicant_Name__c);
}
List<Horticulture__c> allHorti = new List<Horticulture__c>([select id,Applicant_Name__c from Horticulture__c where Applicant_Name__c IN :applicantNameList ]);
Set<String> appNameSet = new Set<String>();
for(Horticulture__c horti : allHorti) {
appNameSet.add(horti.Applicant_Name__c);
}
for(Horticulture__c H:Trigger.New){
if(appNameSet.contains(H.Applicant_Name__c)){
H.addError('You can not create duplicate name');
}
}
}
Thanks,
Abhishek
Hope the below code should solve your issue.
Thanks!!