0

I cannot compare the value of two different list field(ie name)

Getting me error

Variable does not exist: Name

List<Account> actLst = new List<Account>();
Set<String> userNames = new Set<String>();

List<Account> accList = [SELECT Id,Name,duplicate__c FROM Account where 
            Id Not In (Select AccountId from Contact) And 
            Id Not In (Select AccountId from Opportunity)];

List<Account> accCont = [SELECT Id,Name,duplicate__c FROM Account where 
            Id In (Select AccountId from Contact)];

List<Account> accOpp = [SELECT Id,Name,duplicate__c FROM Account where 
            Id In (Select AccountId from Opportunity)];

for(Account account_i : accList)
{

  if( (accCont.Name != account_i.Name) ||  (account_i.Name != accOpp.Name)  ) 
   {    

     if(!userNames.contains(account_i.Name)) 
      {
        userNames.add(account_i.Name);
      }
     else
      {
        account_i.duplicate__c = true;
    actLst.add(account_i);
      } 
   }
 else {
        account_i.duplicate__c = true;
        actLst.add(account_i);
      }
    system.debug('actLst >> ' + actLst);
    update actLst;

}      
2

Your this piece of code is causing the error:-

if( (accCont.Name != account_i.Name) ||  (account_i.Name != accOpp.Name)  ) 

You are trying to access the value from list directly using accCont.Name or accOpp.Name but you cannot as Name exist on single instance of Account but List is group of accounts.

To access the value from a list, Use Get method like this:-

List<Integer> myList = new List<Integer>();
myList.add(47);
Integer myNumber = myList.get(0);
system.assertEquals(47, myNumber);

List<String> colors = new String[3];
colors[0] = 'Red';
colors[1] = 'Blue';
colors[2] = 'Green';

Read more here:- List Class

In your case, You can use Map instead of list like this:-

Map<Id,Account> accMap = new Map<Id,Account>([SELECT Id,Name,duplicate__c FROM Account where 
            Id In (Select AccountId from Contact) And 
            Id In (Select AccountId from Opportunity)]);

and get the account name using id as key.

  • Thanks sanket your solution is very helpful. – Palash Jun 26 at 10:59
  • You can accept this as answer. – sanket kumar Jun 26 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.