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
James KinseyJames Kinsey 

Help! Error: Compile Error: Illegal assignment from LIST<Equipment__c> to LIST<Name>

Hello - I am writing a simple trigger to copy an auto-formula field text into the standard field of my custome object.  I have this error of...

Compile Error: Illegal assignment from LIST<Equipment__c> to LIST<Name> at line 2 column 9

 

My code is:

trigger AutoEName on Equipment__c (before insert, after update) {   
        List<name> NametoUpdate =
        [SELECT e.name
         FROM Equipment__c e
          For Update];
            
        For (Name n: NametoUpdate) {
             n == equipment__c.Auto_Name__c;               }
               
           update NametoUpdate;     
    }

 

Any help would be great.  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
jwhartfieldjwhartfield

The error is happening because your select statement returns full Equipment__c objects, even though you just asked for the name. But to do what you are trying to do, there are a couple of problems:

 

1.  The formula field won't have the right value  until after the record is updated/inserted.  So the trigger should be after insert, after update.

2. Your code right now tries to update every single equipment record in the database whenver the trigger is run.  You need a WHERE clause on your query to only get the records that are being updated/inserted.

3. You need to be sure to grab the Auto_Name__c field in your select query, since you want to access it

4.  Since this is an after update trigger, and you are calling update again, this will get stuck in an infinite loop and bomb.  You need to make some sort of flag in a static class that you can flip the first time through and on the second time through look for that flag and just exit.

 

To make this work, change your code to look something like this (warning: untested)

trigger AutoEName on Equipment__c (after insert, after update) {   
        List<Equipment__c> NametoUpdate =
        [SELECT e.name, e.Auto_Name__c
         FROM Equipment__c e
          WHERE ID in : trigger.newMap.keySet()];

     
        For (Equipment__c n: NametoUpdate) {
             n.name == n.Auto_Name__c;               }
               
           update NametoUpdate;     
    }

 

A better way to auto-set the name is to do forget about the Auto_Name__c field and build the name in the before triggers.  Then you don't need to call any update queries from the trigger. We do this in our product a lot:  (CAUTION: UNTESTED CODE)

 

     trigger AutoEName on Equipment__c (before insert, before update) {   
      For(Equipment__c e : trigger.new)

     {

          e.name = ''; // On this line, set your name to whatever Auto_Name__c would calculate to, if possible.

     }

     
 
    }

All Answers

James KinseyJames Kinsey

Well, I worked around the trigger by setting up a Workflow field update, but I would still like to know what my issue was above if anybody has a quick answer... Thanks!

jwhartfieldjwhartfield

The error is happening because your select statement returns full Equipment__c objects, even though you just asked for the name. But to do what you are trying to do, there are a couple of problems:

 

1.  The formula field won't have the right value  until after the record is updated/inserted.  So the trigger should be after insert, after update.

2. Your code right now tries to update every single equipment record in the database whenver the trigger is run.  You need a WHERE clause on your query to only get the records that are being updated/inserted.

3. You need to be sure to grab the Auto_Name__c field in your select query, since you want to access it

4.  Since this is an after update trigger, and you are calling update again, this will get stuck in an infinite loop and bomb.  You need to make some sort of flag in a static class that you can flip the first time through and on the second time through look for that flag and just exit.

 

To make this work, change your code to look something like this (warning: untested)

trigger AutoEName on Equipment__c (after insert, after update) {   
        List<Equipment__c> NametoUpdate =
        [SELECT e.name, e.Auto_Name__c
         FROM Equipment__c e
          WHERE ID in : trigger.newMap.keySet()];

     
        For (Equipment__c n: NametoUpdate) {
             n.name == n.Auto_Name__c;               }
               
           update NametoUpdate;     
    }

 

A better way to auto-set the name is to do forget about the Auto_Name__c field and build the name in the before triggers.  Then you don't need to call any update queries from the trigger. We do this in our product a lot:  (CAUTION: UNTESTED CODE)

 

     trigger AutoEName on Equipment__c (before insert, before update) {   
      For(Equipment__c e : trigger.new)

     {

          e.name = ''; // On this line, set your name to whatever Auto_Name__c would calculate to, if possible.

     }

     
 
    }

This was selected as the best answer