+ Start a Discussion
Stefano AmentaStefano Amenta 

Illegal assignment from List<Contact> to Contact

Hi,

I'm working on this trigger but I get an error at this line:

Contact contacts = Trigger.new;

Illegal assignment from List<Contact> to Contact ​​​​​​​

Do you know why?
How should it be done correctly?

​​​​​​​Thanks.
trigger TriggerAmbassadorUpdateCall on Contact (before insert, after insert, before update, after update) {
 
        system.debug('trigger new ***' + Trigger.new);
        system.debug('trigger new ***' + Trigger.old);
        
        //define variable data
        Contact contacts = Trigger.new;
        
        //create a set of contact Ids
        
       List<Id> contactIds = new List<Id>();
       // Map<Id, String> contactMaps = new Map<Id, String>();
        
        //loop through data
        for ( Contact contact : contacts ) {
           if( contact.Teacher_recuritment_status__c == 'Active' && contact.Ambassador__Short_Code__c!=null) {
           
            contactIds.add(contact.Id);
            }
        }
        
        if( Trigger.isUpdate) {
          //instantiate the class or create an object 
           
          //fire method
          ambassador.fireCall(contactIds);  
        }
        
        return;
  
}

 
Best Answer chosen by Stefano Amenta
GovindarajGovindaraj
Hi Stefano,
Contact contacts = Trigger.new;
In above line, Trigger.new has 'list of Contact records' and not a 'single Contact record'. So, in that case, We can't assign Trigger.new to a Contact variable (as like above).

So it should be,
List<Contact> lstContacts = trigger.new
Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi Stefano,
Contact contacts = Trigger.new;
In above line, Trigger.new has 'list of Contact records' and not a 'single Contact record'. So, in that case, We can't assign Trigger.new to a Contact variable (as like above).

So it should be,
List<Contact> lstContacts = trigger.new
Thanks,
Govindaraj.S
This was selected as the best answer
Stefano AmentaStefano Amenta
Thanks Govindaraj

It works now.
Ajay K DubediAjay K Dubedi
Hi Stefano,

Firstly note that 'Trigger.new' returns a List of sObject and you use a single instance of Contact.That's why 'Illegal assignment from List<Contact> to Contact'​ error is occurred.So,try this 'use List<Contact> contacts=Trigger.new;'.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Stefano,

You are creating a variable of contact and using it in for-each loop but you have to create a list of contact for using in for-each loop

Replace this of your code
 
Contact contacts = Trigger.new;

By this,

List<Contact> contacts = new List<Contact>();
contacts =  Trigger.new;

and it will work.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha