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
ktshannonktshannon 

Trouble working with database returns.. should be simple

Hey,

 

I have a small snippet of apex that is properly working, I am simply trying to subtract one date from the other (from the soql results) and am not sure as to why this would not work.

 

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Release_Cycle__c>

 

  private String soql {get;set;}
  public List<Release_Cycle__c> release {get;set;}
  public Integer spread {get;set;}

  public ReleaseCycleController() {
    soql = 'select Name, Start_Date__c, Expected_End_Date__c, Internal_Support__c, Business_Group__c, Liason__c, Priority__c, Project_Description__c, Status__c from Release_Cycle__c where Active__c = true';
    runQuery();
    spread = release.Name;
  }

  public void runQuery() {
 
    try {
      release = Database.query(soql);
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }

 

I am a bit unsure how to work with the values I am retreieving from the SOQL query. Ultimately I'd like to be able do this inside of runQuery I believe since I would like spread to be the value of Expected_End_Date__c - Start_Date__c for each line.

 

Thanks a lot!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

As you have retrieved a list of objects via your query, you'll need to pull elements from the list to use them.

 

For example,   the following sets the spread value to the name of the first element in the list.

 

 

spread = release[0].Name;

 

However, I don't think you'll be able to carry out the subtraction in the query.  You'd do better to put that into a formula field on the Release_Cycle__c object.

 

All Answers

bob_buzzardbob_buzzard

As you have retrieved a list of objects via your query, you'll need to pull elements from the list to use them.

 

For example,   the following sets the spread value to the name of the first element in the list.

 

 

spread = release[0].Name;

 

However, I don't think you'll be able to carry out the subtraction in the query.  You'd do better to put that into a formula field on the Release_Cycle__c object.

 

This was selected as the best answer
ktshannonktshannon

Very good point. I should be using a formula field for this.Thanks!

 

I assume it is not possible to append values into this List since it is of the <Release_Cycle__c> type.. would I have to use a map to accomplish this?

 

bob_buzzardbob_buzzard

I'm not quite understanding the question I'm afraid.

 

You can append Release_Cycle__c records to the list - I'm not sure what a map would add in this case.

 

Just to be clear, the formula field would be a custom field on the sobject itself, that you would just query as part of the Release_Cycle__c record.

 

Have I helped or made things worse ?

ktshannonktshannon

No this makes perfect sense, I was talking hypothetical if I were to calculate a value and try to push it into the list. I would have to create a separate list? Or I suppose I would do this via the query itself. You have helped very well ;) I'm just thinking out loud

bob_buzzardbob_buzzard

Gotcha.  No, you wouldn't be able to add your own calculated value to the list.  To tie together an sobject and other data, you'd usually use a custom wrapper class. 

ktshannonktshannon

Ahh very well, thank you again... do you have an example of a wrapper class?

bob_buzzardbob_buzzard
Here's a quick stab. Please excuse the formatting -sent from iPad.

Public class ContactWrapper
{
public Contact cont {get; set;}
public String mydata {get; set;}
}

Use as follows:

List<Contact> conts=[select Id, name from contact limit 10];

If (!conts.isEmpty())
{
ContactWrapper wrap=new ContactWrapper();
wrap.cont=conts[0];
wrap.mydata='this is my data';
}
ktshannonktshannon

Your the man Bob, I owe you a beer :)