+ Start a Discussion
Ryan GreeneRyan Greene 

Illegal Assignment from String to List

Hello,
I have been going through this code for an hour now and cannot figure out why it is an "Illegal Assignment from String to List". Isn't an Email field a string?

The error is shown at the line I flagged, however the error might be somewhere else in my code too.
public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }
 
    private final <Object> WebInquiry;
 
    // Create a constructor that populates the Account object
    public sendEmail() {
        WebInquiry = [SELECT Email__c FROM <Object> WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    }
 
    public <Object> getWebInq() {
        return WebInquiry;
    }
 

    public PageReference send(){
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 
String address;
        address = WebInquiry.Email__c;

****ERROR HERE****        String[] toAddresses = address;
 
        // Sets the paramaters of the email
        email.setSubject( subject );
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( body );
   
        // Sends the email
        Messaging.SendEmailResult [] r =
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
        
        return null;
    }

}

Thanks for your help!
Ryan
Best Answer chosen by Ryan Greene
Ryan GreeneRyan Greene
Unfortunately your suggestions did not work. I went back to where I took this code and added the ".split(':', 0)" at the end of line 34 and it now works.

If anyone is interested I grabbed the code here and modified it to fit my custom object: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_custom_controller.htm

Thank you for your input!
Ryan
 
public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }
 
    private final Web_Links__c WebInquiry;
 
    // Create a constructor that populates the Account object
    public sendEmail() {
        WebInquiry = [SELECT Email__c, First_Name__c FROM Web_Links__c WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    }
 
    public Web_Links__c getWebInquiry() {
        return WebInquiry;
    }
    
    public PageReference send(){
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 
  String addresses;
//    if (WebInquiry.size() > 0)
//    {
        addresses = WebInquiry.Email__c;
        // Loop through the whole list of contacts and their emails
//        for (Integer i = 1; i < WebInquiry.size(); i++) 
//        {
//            if (WebInquiry[i].Email__c != null)
//            {
//                addresses += ':' + WebInquiry[i].Email__c;
//            }
//        }
//    }

        String[] toAddresses = addresses.split(':', 0);

        
        // Sets the paramaters of the email
        email.setSubject(subject);
        email.setToAddresses(toAddresses);
        email.setPlainTextBody(body);
   
        // Sends the email
        Messaging.SendEmailResult [] r =
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
        
        return null;
    }
}

 

All Answers

Shiva RajendranShiva Rajendran
Hi Ryan,
This error happens at time in start of apex .It's that you assigned list to a string variable . At line 22 WebInquiry is a list , not a string ,  WebInquiry.get(index).Email__c will work. Cool 
Thanks and Regards,
Shiva RV
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }
 
    private final <Object> WebInquiry;
 
    // Create a constructor that populates the Account object
    public sendEmail() {
        WebInquiry = [SELECT Email__c FROM <Object> WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    }
 
    public <Object> getWebInq() {
        return WebInquiry;
    }
 

    public PageReference send(){
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 
		String address;
		if(WebInquiry.size() > 0 )
		{
			address = WebInquiry.get(0).Email__c ;

			List<String> toAddresses = new List<String>();
			toAddresses.add(address);

			// Sets the paramaters of the email
			email.setSubject( subject );
			email.setToAddresses( toAddresses );
			email.setPlainTextBody( body );

			// Sends the email
			Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
			
		}
        
        return null;
    }

}

Let us know if this will help you
 
Ryan GreeneRyan Greene
Unfortunately your suggestions did not work. I went back to where I took this code and added the ".split(':', 0)" at the end of line 34 and it now works.

If anyone is interested I grabbed the code here and modified it to fit my custom object: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_custom_controller.htm

Thank you for your input!
Ryan
 
public class sendEmail {
    public String subject { get; set; }
    public String body { get; set; }
 
    private final Web_Links__c WebInquiry;
 
    // Create a constructor that populates the Account object
    public sendEmail() {
        WebInquiry = [SELECT Email__c, First_Name__c FROM Web_Links__c WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    }
 
    public Web_Links__c getWebInquiry() {
        return WebInquiry;
    }
    
    public PageReference send(){
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 
  String addresses;
//    if (WebInquiry.size() > 0)
//    {
        addresses = WebInquiry.Email__c;
        // Loop through the whole list of contacts and their emails
//        for (Integer i = 1; i < WebInquiry.size(); i++) 
//        {
//            if (WebInquiry[i].Email__c != null)
//            {
//                addresses += ':' + WebInquiry[i].Email__c;
//            }
//        }
//    }

        String[] toAddresses = addresses.split(':', 0);

        
        // Sets the paramaters of the email
        email.setSubject(subject);
        email.setToAddresses(toAddresses);
        email.setPlainTextBody(body);
   
        // Sends the email
        Messaging.SendEmailResult [] r =
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
        
        return null;
    }
}

 
This was selected as the best answer
achyuth dasamachyuth dasam
Hi All,
 I have an error (Illegal assignment from String to List<Account>)  , plz help me to resolve the below code


trigger CountOfContacts on Contact (after insert,after delete)
{
 set<id> accid=new set<id>();

 list<contact> contactlist =new list<contact>();

 list<contact> listcon=new list<contact>();

 list<account> acclist=new list<account>();

 list<account> listAcc=new list<account>();

map<id,integer> mapCount=new map<id,integer>();

 if(trigger.isinsert){

 for(contact con:trigger.new){

 accid.add(con.accountid);

 }

 }
     if(trigger.isdelete){

 for(contact con:trigger.old){

 accid.add(con.accountid);

 }

 }

 acclist ='select id,name from account where id in:accid';

 contactlist = 'select id,name,accountid from contact where accountid in:accid';

 for(account acc:acclist){

 listcon.clear();

 for(contact c:contactlist){

 if(c.accountid==acc.id){

 listcon.add(c);

 mapCount.put(c.accountid,listcon.size());

 }

 }

 }

 if(acclist.size()>0){

 for(Account a:acclist){

 if(mapCount.get(a.id)==null)

 a.contacts_count__c=0;

else

 a.contacts_count__c=mapCount.get(a.id);

 listAcc.add(a);

 }

 }

 if(listAcc.size()>0)

 update listAcc;

 }
 
achyuth dasamachyuth dasam
Attached error for above code
****ERROR HERE****  acclist ='select id,name from account where id in:accid';
 
Troy CenterTroy Center

Ryan Greene

I'm replying because of achyuth dasam. Syntax error: String[] toAddresses = address would not work because String[] is an Apex Array (same as a list). String toAddresses = address;  would have worked.  There really aren't arrays in Apex, you can use the array notation to declare lists however. In the original case, you needed a string variable, not an Array of Strings. I think Amit Chaudhary 8 may have also had a relevant working answer. 

achyuth dasam

You created a string...  acclist ='select id,name from account where id in:accid';
You need a query... acclist = [ select id,name from account where id in:accid ];

If these answers help, please vote... :-) Thanks. Troy ~ Happy Paths! 

Troy CenterTroy Center

Additionally Ryan Green: (opposite prior post)

If you were attempting to make an array.... String[] toAddresses = address would not work because you cannot add to an array (list) like this. String[] toAddresses.add(address); would have worked. See more about lists here: List Class | Apex Reference Guide | Salesforce Developers (https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm)