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);

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 Nov 5 '15 at 22:07
  • its not adding it to the field right now, do I need to make anything like .add();? – Metawaa Nov 5 '15 at 22:11
  • @bez Just assigning it is correct. – Keith C Nov 5 '15 at 22:13
  • hmmm it gives me an empty field – Metawaa Nov 5 '15 at 22:18
  • system debug gives met he correct information , its just not assigning it – Metawaa Nov 5 '15 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.