function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alockremalockrem 

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.

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

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

JimRaeJimRae

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

 

 

 

 

This was selected as the best answer
alockremalockrem
Thank you very much.  Needless to say, I'm a force.com newbie and am still struggling through some of the basics.  I really appreciate your help.  Your response makes perfect sense.
TechSteveTechSteve

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

Jayesh Shewale12345Jayesh Shewale12345
String userId = UserInfo.getUserId();
        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