1

I wrote the following trigger and I'm getting the error :

Illegal assignment from Schema.SObjectField to String

But I don't know how to fix it. I wrote a similar trigger before and didn't have any issues.

trigger AccountDuplicates on Account (before insert, before update) { 
for (Account myaccount : Trigger.new) {
        List <Account> dupes = [SELECT Name FROM Account WHERE Name = :(myaccount.Name)]; 
        if (dupes.size() > 0) { 
          myaccount.Duplicate_Account__c = Account.name;
        }
    }
}

The purpose of the trigger - when a new account is created that matches the name of an existing account, put the existing account's name into the "Duplicate Account" field.

1
  • Besides the issue you are having, you need to consider bulkifying you code. You currently have a SOQL statement inside a for loop, which will hit a governor limit if a lot of contacts are inserted/updated
    – BarCotter
    Sep 29, 2014 at 15:03

3 Answers 3

2

The root problem is the line

 myaccount.Duplicate_Account__c = Account.name;

This references Account (the Schema sobject), not myAccount, the loop variable. Thus you need the following

trigger AccountDuplicates on Account (before insert, before update) { 
  for (Account myaccount : Trigger.new) {
    List <Account> dupes = [SELECT Name FROM Account WHERE Name = :(myaccount.Name)]; 
    if (dupes.size() > 0) { 
      myaccount.Duplicate_Account__c = myAccount.name;  // myAccount - the trigger loop vbl
    }
  }
}

Now, as @barcotter, said, you need to bulkify the trigger as SOQL inside of for loops is a no-no -- when the batch size is >100 as it can easily be in dataloader use cases, the governor limit on SOQL (100) will be exceeded.

I'll leave that as an exercise for you - there are many many resources (Google) on bulkifying triggers

4
  • Thank you for your help! Teaching myself this stuff is not the easiest thing when I have no coding background.
    – JSmith
    Sep 29, 2014 at 15:15
  • you will also need to learn to write a test class - again --many many resources on how to do this, starting with the Apex Developer's Guide
    – cropredy
    Sep 29, 2014 at 15:16
  • Sorry @crop1645 I just voided your exercise by posting my answer :-)
    – BarCotter
    Sep 29, 2014 at 15:20
  • no worries -- i knew someone would do the bulkification
    – cropredy
    Sep 29, 2014 at 16:28
1

See @crop1645 for the answer as to why your current code isn't working. But there is a larger issue here with bulkified code. See the example below that will only do one SOQL query no matter how many Accounts are inserted or updated.

trigger AccountDuplicates on Account (before insert, before update) { 

    List<String> names = new List<String>();
    for (Account a : Trigger.new) {
            names.add(a.Name);
    }

    Map<String, Account> duplicateMap = new MapMap<String, Account>();
    for (Account dup : [SELECT Name, Id FROM Account WHERE Name in :names]) {
        duplicateMap.put(dup.Name, dup); // Last duplicate found is stored in the map
    }

    for (Account a : Trigger.new) {
        Account dup = duplicateMap.get(a.Name);
        if (dup != null) {
            a.Duplicate_Account__c = dup;
        }     
    }
}
0

List<Account> dupes should be there instead of just List dupes . And moreover is the field " Duplicate Account" a text field or a lookup? If its a lookup , name cannot be assigned to it?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .