+ Start a Discussion
Mike Parks 5Mike Parks 5 

Compile Error: Illegal assignment from List

I keep getting this error when I try to save the following code...any idea what I've got wrong? 

Error: Compile Error: Illegal assignment from List<Proposal__c> to Id at line 13 column 3


public with sharing class AnotherController
{
 private ApexPages.StandardController stdCtrl;
  
 public AnotherController(ApexPages.StandardController std)
 {
  stdCtrl=std;
 }
  
 public void PopulateProject()
 {
  Project__c proj=(Project__c) stdCtrl.getRecord();
  proj.Proposal__c=[select Scope_of_Work__c from Proposal__c where Id=:proj.Proposal__c];
  proj.Scope_of_Work__c=proj.Proposal__c.Scope_of_Work__c;
 }
}
karthikeyan perumalkarthikeyan perumal
 Hello Mike, 

you are trying to assign list of  Proj.Proposal__c to id. try some other Operator "IN" insted of  " =: "

Hope it will help you. 

Thanks
Karthik
 
Dilip_VDilip_V
Hi Mike,
 
This line contains error
proj.Proposal__c=[select Scope_of_Work__c from Proposal__c where Id=:proj.Proposal__c];

 Above query returns list and Proj.Proposal__C is of id type

So try something like this
proj.Proposal__c=[select Scope_of_Work__c,id  from Proposal__c where Id=:proj.Proposal__c limit 1].id;
If it helps make it as best answer.
Thanks.


 
Manish Anand 10Manish Anand 10
Mike,
SOQL statement, returns list of records.So, you need a list of datatype Proposal__c to store the list of records returned by the SOQL statement.
Eg: List<Proposal__c> prop = new List<Proposal__c> ();
prop=[select Scope_of_Work__c from Proposal__c where Id=:proj.Proposal__c]; 

By the way, what's your requirement?What's the relation between Project__c and Proposal__c ?
Mike Parks 5Mike Parks 5
There is a lookup relationship from Project__c to Proposal__c

When I'm entering a new Project__c, and select the related Proposal__c, I'm trying to auto complete several fields on the new Project__c screen before Saving. And I need the auto completed fields to be editable as well. 

I found an example in this forum for Contacts and Accounts that works exactly like I need. Here's the code for the example that is working:

public with sharing class RelatedController 
{
 private ApexPages.StandardController stdCtrl;
  
 public RelatedController(ApexPages.StandardController std)
 {
  stdCtrl=std;
 }
  
 public void AccountPopulated()
 {
  Contact cont=(Contact) stdCtrl.getRecord();
  cont.Account=[select NAICS__c, DUNS_Number__c from Account where id=:cont.AccountId];
  cont.NAICS__c=cont.Account.NAICS__c;
 }
}
karthikeyan perumalkarthikeyan perumal
Hello mike

juat look at the where condition they check with id=:cont.accountid. 

But in your case you are check id with entire object insted if this  kinldy check like below

id=:proj.Proposal__c.id 

hope it will work for you 
makr best ans if it's work.

Thanks
karthik
 
Mike Parks 5Mike Parks 5
Karthik thanks for the response. But when I make that change I just get a different error:
Error: Compile Error: Invalid foreign key relationship: Project__c.Proposal__c at line 13 column 72

I guess I don't understand why this same line from the working example:
 cont.Account=[select NAICS__c, DUNS_Number__c from Account where id=:cont.AccountId];

is working without problem. 

One thing I see is cont.Account is named differently from cont.AccountId whereas in my code
proj.Proposal__c=[select Scope_of_Work__c from Proposal__c where Id=:proj.Proposal__c];

proj.Proposal__c is named exactly the same as proj.Proposal__c 

So it's like for the std object something is different from my custom object. 
karthikeyan perumalkarthikeyan perumal
Hello Mike, 

Hello mike

account is the standard object.your trying to use custom object so you have to use like below

id=:proj.Proposal__r.id 

Note: also make sure the right relationship with those 2 objects. 

Coz Account and Contact Document wise lookup relationship. but while working with this object like is there is any account  is assiciated with contact it would act as master details relationship like delete account will associated contact also delete. 

thanks
karthik
 
Mike Parks 5Mike Parks 5
Thanks everyone for trying to help me with this. Here's what finally worked....

public with sharing class RelatedController1
{
public Proposal__c prop {get;set;}
 private ApexPages.StandardController stdCtrl;
  
 public RelatedController1(ApexPages.StandardController std)
 {
  stdCtrl=std;
 }
  
 public void PopulateProject()
 {
  Project__c proj=(Project__c) stdCtrl.getRecord();
  prop=[select Name, SOW__c, Scope_of_Work__c, Contact__c from Proposal__c where Id=:proj.Proposal__c];


  proj.SOW__c=prop.SOW__c;
  proj.Name=prop.Name;
  proj.Scope_of_Work__c=prop.Scope_of_Work__c;
  proj.Project_Contact__c=prop.Contact__c;
 }
}

Adding the line: public Proposal__c prop {get;set;}

And then I was able to say....
 prop=[select Name, SOW__c, Scope_of_Work__c, Contact__c from Proposal__c where Id=:proj.Proposal__c];

is working perfectly now. 

So if you ever need to auto populate editable fields based on selecting a related object lookup value...this is how you do it!