I have the following error for this code "Illegal assignment from Contact to String "

system.debug gives me email address, but its giving me error when I want to insert that value into that custom field inside the opportunity

List<Contact> allContacts = [Select Email From Contact where AccountId=: account.id Limit 1];
      for (Contact currentContact : allContacts) {
        System.debug('Current Contact Email' +  currentContact);
        Opportunity.Email_1__c = currentContact.toString();

      System.debug('contat' + allContacts);

1 Answer 1


You need to assign the Email field of Contact that you queried:

Opportunity.Email_1__c = currentContact.Email;

You can see a string version of an SObject such as Contact but that shows the names and values of all the populated fields rather than the single field value you require.


To find out what is going on for your "empty field problem":

System.debug('contact Email ' + currentContact.Email);
Opportunity.Email_1__c = currentContact.Email;
System.debug('opp Email ' + Opportunity.Email_1__c);
  • Oh yah, The ID was in the system.debug too, you are absolute right. Thank you
    – Metawaa
    Commented Nov 5, 2015 at 22:07
  • its not adding it to the field right now, do I need to make anything like .add();?
    – Metawaa
    Commented Nov 5, 2015 at 22:11
  • @bez Just assigning it is correct.
    – Keith C
    Commented Nov 5, 2015 at 22:13
  • hmmm it gives me an empty field
    – Metawaa
    Commented Nov 5, 2015 at 22:18
  • system debug gives met he correct information , its just not assigning it
    – Metawaa
    Commented Nov 5, 2015 at 22:22

You must log in to answer this question.

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