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
santhosh konathala 17santhosh konathala 17 

Hi Community I am learner of Apex coding.I written a class avoids duplicate records but showing an error ''Loop variable must be of type SObject at line 11 column 13 ".please help me how to resolve this?Below is my class?

public class AvoidduplicateAccount{

public void AvoidDupliacteRecords(Account[] acc)
{
set<string> set1=new set<string>();
for(account a:[select id,name from Account])
{
set1.add(a.name);
}

for(Account d:Trigger.new)
{
if(set1.contains(d.name))
{
d.adderror('plz insert unique values');
}
}
}
}
Best Answer chosen by santhosh konathala 17
sfdcMonkey.comsfdcMonkey.com
you can not direct use of Trigger.new in trigger handler class trigger.new pass as parameter in method in your case  Trigger.new pass as Account[] acc so use acc insted of Trigger.new
I hop it helps you
Please mark it best ansert if it helps you so it make proper solution for others :)

All Answers

sfdcMonkey.comsfdcMonkey.com
hi santosh try this once :)

public class AvoidduplicateAccount{

public void AvoidDupliacteRecords(Account[] acc){
set<string> set1=new set<string>();
for(account a:[select id,name from Account]){
set1.add(a.name);
}

for(Account d:acc){
if(set1.contains(d.name)){
d.adderror('plz insert unique values');
  }
  }
  }
}

Thanks
let me inform if it work
sfdcMonkey.comsfdcMonkey.com
you can not direct use of Trigger.new in trigger handler class trigger.new pass as parameter in method in your case  Trigger.new pass as Account[] acc so use acc insted of Trigger.new
I hop it helps you
Please mark it best ansert if it helps you so it make proper solution for others :)
This was selected as the best answer
SHIRAJ ALAMSHIRAJ ALAM
getting error

Variable does n't exit 'Name'