You need to sign in to do that
Don't have an account?
Ryan 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.
Thanks for your help!
Ryan
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
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
All Answers
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
Let us know if this will help you
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
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;
}
****ERROR HERE**** acclist ='select id,name from account where id in:accid';
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!
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)