0

How can I save list values in my controller to fields in my object? I keep getting the error "Initial term of field expression must be a concrete SObject: List". I can get the individual values in a visualforce page, so I can't understand why I can't get them in my controller when I try to save. Thank you in advance for any help/ guidance.

Please see the following for some background on the problem...

In my controller I have a JSON string which is parsed, and it's values are added into a list (please see below).

JSON String "this.studyJson"

{"study":{"moleculeDescription":"RONTfdasZUMAB","protocolTitle":"Ifw","protocolNumber":"GA0fda6","studyName":"n alpha ie II LCM Option","therapeuticArea":"INFRuafnda&BONE","startDate":"2009-03-25","endDate":"2013-08-22"}}

Controller, Study list

List<StudyWrapper> Study = new List<StudyWrapper>();

public List<StudyWrapper> getStudy(){
    JSONParser parser = JSON.createParser(this.studyJson);
    while(parser.nextToken() != null){
            while(parser.nextToken()!= null){
                if(parser.getCurrentToken()==JSONToken.START_OBJECT){
                    StudyWrapper s = (StudyWrapper)parser.readValueAs(StudyWrapper.class);
                    Study.add(s);
                }
            }
        }
    return Study;
}

class StudyWrapper{
    public String moleculeDescription {get; set;}
    public String protocolTitle {get; set;}
    public String protocolNumber {get; set;}
    public String studyName {get; set;}
    public String therapeuticArea {get; set;}
    public String startDate {get; set;}
    public String endDate {get; set;}
}

On the visualforce page I can call the list values and show them (please see below)

   <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockTable value="{!Study}" var="s">
                <apex:column headerValue="Protocol Title" value="{!s.protocolTitle}"/>
                <apex:column headerValue="Molecule Description" value="{!s.moleculeDescription}"/>
                <apex:column headerValue="Protocol Nickname" value="{!s.studyName}"/>
                <apex:column headerValue="Therapeutic Area" value="{!s.therapeuticArea}"/>
                <apex:column headerValue="Start Date" value="{!s.startDate}"/>
                <apex:column headerValue="End Date" value="{!s.endDate}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>

Just like on the visualforce page, what I want to do is get the list values and save them to the object's record fields when I hit the save button. Something like...

    public PageReference save(){
    Object__c.field__c = Study.studyname;
...
}

However, when I do this I get the inevitable error "Initial term of field expression must be a concrete SObject: List". In my controller, how can I get the values from the list and save them to a field on my object?

1
  • Study is a LIST of StudyWrapper. You need to get the specific list item to save it (i.e. Study[0].studyName) Which item to get is up to your logic and needs
    – Eric
    Feb 13, 2015 at 17:32

1 Answer 1

4

In this method you are trying referencing a variable of a List type which ois wrong.

public PageReference save(){
    Object__c.field__c = Study.studyname;
...
}

This method would be like that

public PageReference save() {
    // Get elemnt form list first
    if(study.size()>0)
       Object__c.field__c = study[0].studyname; // Getting instance of sobject at index 0
...
}

Hope it makes sense.

1
  • Thank you @Ashwani , this method works great and is exactly what I needed to know and understand.
    – rwegner7
    Feb 13, 2015 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .