1

I have created a custom field in custom object, in that i am getting the value and when clicked custom button "convert" which is create a new contact. But when converting i am getting this error.

Error Message: Error: Compile Error: Illegal assignment from String to SOBJECT:Account at line 37 column 9

Code:

public class LeadConversion {
 public PageReference RedirecttoLead()
 {
    String currentLead = '/' + siteObj.Id;
    PageReference pageRef = new PageReference(currentLead);
    return pageRef;
 }

 private Site__c siteObj;

 // The extension constructor initializes the private member
 // variable acct by using the getRecord method from the standard
 // controller.
 public LeadConversion(ApexPages.StandardController stdController)
 {
  System.debug('******sai******');
         siteObj = (Site__c)stdController.getRecord();
 }

 public void convertLead(){    
    Contact cc = new Contact();
    cc.LastName = siteObj.LastName__c;
    **cc.Account = siteObj.Name;**

   try 
   { 
    insert cc; 
   }
    Catch (Exception ex2)
    {

     ex2.getmessage();
    }     
 }    
}

Kindly anyone tell how to solve this issue.

1
  • I assume cc.Account = siteObj.Name; is causing the error?
    – Mike Chale
    Apr 4, 2013 at 11:33

2 Answers 2

6

You cannot assign the standard field Name to the Account lookup. You will need an Account-ID. Something like this:

cc.AccountId = siteObj.Account__c;
1

Your code is trying to set siteObj.Name as an Account, but you need to set the Contact AccountId field to a valid Account Id instead.

Contact cc = new Contact();
cc.AccountId = siteObj.[field for Account Id]

Edit

You can try out SOQL queries easily in Workbench and it has a query builder. You can also execute anonymous APEX through the Developer Console. Try running

select AccountId from Contact

in Workbench and you'll get a list of AccountId assigned to various contacts.

6
  • Hi mike, thanks for the reply.I am not having AccountId field in contact. Account Name field is lookup field in contact.I am this this error after i have made these changes.Invalid id: Voltum Error is in expression '{!convertlead}' in component <apex:page> in page leadconversion Apr 4, 2013 at 11:47
  • 1
    Try Id accountId = [select AccountId from Contact where Id = {ID}][0].AccountId; System.Debug(accountId); in the Developer Console or Workbench. It's there.
    – Mike Chale
    Apr 4, 2013 at 11:59
  • Thanks Mike, I am new to developer kindly tell where to use this. Apr 4, 2013 at 12:03
  • Hi mike, I am getting these error after made thesee changes to apex class.Error: Compile Error: unexpected token: '{' at line 38 column 65 Apr 4, 2013 at 12:24
  • {ID} was meant to designate a test id for demonstration purposes, not for your class. If siteObj.Name is a lookup(Account) then you would probably use cc.AccountId = siteObj.Name.Id;
    – Mike Chale
    Apr 4, 2013 at 12:31

You must log in to answer this question.

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