+ Start a Discussion
abdul kalam 13abdul 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');
        }            
        
    }

}
Best Answer chosen by abdul kalam 13
SaranSaran
Hi Abdul,

Hope the below code should solve your issue.
 
trigger Duplicate_Farmer_Horti on Horticulture__c (before insert,before update)
{
    for (Horticulture__c newH:Trigger.New)
    {
        if(nameSet.ContainsKey(newH.Name))
        	newH.addError('Duplicate Entry');
        else
        	nameSet.add(newH.name);
    }

    for(Horticulture__c dbH : [select id, name from Horticulture__c where name in : nameSet])
    	dbNameSet.add(dbH.Name);

    for(Horticulture__c newH : trigger.new)
    {
    	if(dbNameSet.contains(newH.Name))
    		newH.addError('Duplicate Entry');
    }
}

Thanks!!

All Answers

Abhishek BansalAbhishek Bansal
Hi Abdul,

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
SaranSaran
Hi Abdul,

Hope the below code should solve your issue.
 
trigger Duplicate_Farmer_Horti on Horticulture__c (before insert,before update)
{
    for (Horticulture__c newH:Trigger.New)
    {
        if(nameSet.ContainsKey(newH.Name))
        	newH.addError('Duplicate Entry');
        else
        	nameSet.add(newH.name);
    }

    for(Horticulture__c dbH : [select id, name from Horticulture__c where name in : nameSet])
    	dbNameSet.add(dbH.Name);

    for(Horticulture__c newH : trigger.new)
    {
    	if(dbNameSet.contains(newH.Name))
    		newH.addError('Duplicate Entry');
    }
}

Thanks!!
This was selected as the best answer
abdul kalam 13abdul kalam 13
Oh! Thanks Abhishek. Both the codes you have sent is executing. Thanks once agian.