0

Can you not query the same object more than once in a single class? I have a class that I grab information related to the Contract Member record to create a list of Contracts. I want to also create a function within the class that I use to grab the hourly rate of the Contract Member and use it to set a total cost field on the visual force page.

It seems that any time I want to query the same object (in this case the end where I am trying to assign a value to the variable hourly), I get an error that says

Error: Compile Error: Illegal assignment from List to Integer at line 24 column 8

Here is the Apex Code. I am new to Apex but it would be really ridiculous if you have to have multiple extensions to work with the same object.

public with sharing class addMultipleCostsExtension {

public list<Contract_Cost__c> listCost{get;set;}
public String selectedContractMember{get;set;}

public addMultipleCostsExtension(ApexPages.StandardController controller) {
    listCost= new list< Contract_Cost__c >();      
}
public List<selectOption> getContractMembers() {
    List<SelectOption> ContractMembers=new List<SelectOption>();
    ContractMembers.add(new SelectOption('0001', '--Select--'));
    List<Contract_Member__c> ContractMembersList=[SELECT Id, Name, CES_Contract__r.Name, CES_Contract__c FROM Contract_Member__c WHERE Internal_Employee__c =:UserInfo.getUserId()];
    for(Contract_Member__c r:ContractMembersList) {
        ContractMembers.add(new SelectOption(r.ID,r.CES_Contract__r.Name));
    }
    return ContractMembers;
}    

public integer hours{get;set;}
public integer totalCost{get;set;}
public integer hourlyRate{get;set;}

public void setCost(){
   hourlyRate = [SELECT Hourly_Rate__c FROM Contract_Member__c WHERE Internal_Employee__c =:UserInfo.getUserId()];       
   totalCost = hours*hourlyRate;
}  

It seems that the object name is being used in the List, which yeah I guess it is but (again new at this) it seems that you need to include the object name in the List definition <> in order for it to work. Maybe the List needs to just be renamed.

2

The problem doesn't come from the number of times you use this variable.

Like the error says, you're trying to assigning your Integer variable hourlyRate to a SOQL query returning a list of SObject type.

If you define a limit of 1 record returned, the query will return only one SObject. Then, just replace your query by the following code to assign your variable to the queried field.

hourlyRate = [SELECT Hourly_Rate__c FROM Contract_Member__c WHERE Internal_Employee__c =:UserInfo.getUserId() LIMIT 1].Hourly_Rate__c;

ps: Note that the LIMIT 1 will make the query return only one record. But if your query is supposed to return multiple records, the only record returned will be chosen randomly. Consider adding some filters to be sure to return only the contract member needed.

  • Thanks Martin! I will mark this when it allows me to. To narrow down my results, what I really need to do is base it off of the Contract (r.Id) they chose in the selectlist, which needs to be dynamically sent to the controller so it can process the setCost(). But that's a different stackexchange question. – Travis Johnston Jul 20 '17 at 15:02
0

You can't assign SObject to an integer, When you do SOQL it always return the fields with id. You may or May not be include Id field in SOQL , when You did query for Contract_Member__c its return with id and Hourly_Rate__c.

public integer hours{get;set;}
public integer totalCost{get;set;}
public integer hourlyRate{get;set;}

public void setCost(){
 Contract_Member__c  Contract_Member = [SELECT Hourly_Rate__c FROM Contract_Member__c WHERE Internal_Employee__c =:UserInfo.getUserId() Limit 1];   
   hourlyRate=Contract_Member.Hourly_Rate__c;
   totalCost = hours*hourlyRate;
} 

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.