You need to sign in to do that
Don't have an account?

How to cast SOQL result as a string
I am attempting to pull the state__c value from the site__c object where the site__c.id matches the selected value from a selectList. I have tried 2 different ways and am unable to avoid errors.
-----------------
Attempt #1
public String siteState
{
get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1]; }
set;
}
The error I receive is:
Error: Compile Error: Return value must be of type: String at line 17 column 14
-----------------
Attempt #2
public String siteState
{
get
{
String tmpState = [SELECT state__c FROM site__c WHERE id = :site LIMIT 1];
return tmpState;
}
set;
}
The error I receive is:
Error: Compile Error: Illegal assignment from LIST:SOBJECT:Site__c to String at line 19 column 13
-----------------
Any help is greatly appreciated.
Remember that your SOQL is returning an object, in your case a site__c object.
what you need to do is get just the field member you are interested in, assuming it is already the correct data type for your need.
Attempt #1 public String siteState { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
or
public String siteState { get { String tmpState = [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; return tmpState; } set; }
All Answers
Remember that your SOQL is returning an object, in your case a site__c object.
what you need to do is get just the field member you are interested in, assuming it is already the correct data type for your need.
Attempt #1 public String siteState { get {return [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; } set; }
or
public String siteState { get { String tmpState = [SELECT state__c FROM site__c WHERE id = :site LIMIT 1].state__c; return tmpState; } set; }
Can someone tell me how to test this in unit testing.It solved my problem but created another!
I keep getting an error message 'System.QueryException: List has no rows for assignment to SObject' and I'm not sure how to tackle this problem.
Thanks
Steve
User u = [Select Id, userPosition From User Where Id =:userId limit 1];
String usrPos = u.userPosition;
Please tell me what is wrong into this I am getting error