+ Start a Discussion
Manish Anand 10Manish Anand 10 

standardsetController- Giving Error message

Hi,

I am trying to implememnt a standardsetcontroller. I copy pasted the apex code from saslesforce document example-
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardsetcontroller.htm
Below is the code-
public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}

when I save this I get an error-
Error: Compile Error: Incompatible types since an instance of List<SObject> is never an instance of List<Opportunity> at line 17
column 16


Is something changed or if I am missing something?
 
Best Answer chosen by Manish Anand 10
apex sfdevapex sfdev
In my system it is working fine,
public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
       List<Opportunity> oplist=new List<Opportunity>();
       for(SObject op:setCon.getRecords())
          oplist.add((Opportunity)op);
        return oplist;
    }
}

and the visual force page is
<apex:page controller="opportunityList2Con" showHeader="false">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

it is displaying like this,
User-added image

All Answers

apex sfdevapex sfdev
Use the separate list for your getOpportunities() method and try the below code,
public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
       List<Opportunity> oplist=new List<Opportunity>();
       for(Opportunity op:setCon.getRecords())
          oplist.add(op);
        return oplist;
    }
}

 
Manish Anand 10Manish Anand 10
Thanks for your quick repsonse.
When I tried to save above code, I code below error message-
Error: Compile Error: Loop variable must be of type SObject at line 18 column 24
apex sfdevapex sfdev
Try the below change in your getOpportunities(),
 
public List<Opportunity> getOpportunities() {
       List<Opportunity> oplist=new List<Opportunity>();
       for(SObject op:setCon.getRecords())
          oplist.add((Opportunity)op);
        return oplist;
    }

 
Manish Anand 10Manish Anand 10
I tried this earlier as well, before writing my last comment.It again gives below error-

Error: Compile Error: Incompatible types since an instance of SObject is never an instance of Opportunity at line 19 column 22
apex sfdevapex sfdev
In my system it is working fine,
public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
       List<Opportunity> oplist=new List<Opportunity>();
       for(SObject op:setCon.getRecords())
          oplist.add((Opportunity)op);
        return oplist;
    }
}

and the visual force page is
<apex:page controller="opportunityList2Con" showHeader="false">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

it is displaying like this,
User-added image
This was selected as the best answer
Manish Anand 10Manish Anand 10
Yes. I see it working in my another dev org.But not in the current one.