0

enter image description here

i am getting an unknown error in class. don't know how to clear. code and logic is right. i have mentioned code below please do needful. thanks in advance.

    public class wrapperClassController {

    //Our collection of the class/wrapper objects cContact 
    public List<cContact> contactList {get; set;}

    //This method uses a simple SOQL query to return a List of Contacts
    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            **for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {**
                //  **ERROR:Error: Compile Error: Loop variable must be an SObject //or list of Contact at line 10 column 25**  

                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }


    public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Contact> selectedContacts = new List<Contact>();

        //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        for(cContact cCon: getContacts()) {
            if(cCon.selected == true) {
                selectedContacts.add(cCon.con);
            }
        }

        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        System.debug('These are the selected Contacts...');
        for(Contact con: selectedContacts) {
            system.debug(con);
        }
        contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        return null;
    }


    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }
}
  • i did not get any error , i just copied your code. – C0DEPirate May 6 '16 at 6:59
  • to me it looks perfectly fine. Try navigating to the other page and then come back and try to save it again? – Novarg May 6 '16 at 7:10
  • As @shanu Suggested, delete the class and create a new class. – C0DEPirate May 6 '16 at 8:13
2

To clear this error. Please follow below steps.

1) Go to Setup > Develop > Apex Classes.

2) Find the apex lass named "wrapperClassController".

4) Delete the apex class.

5) Create a new Apex class using same code.

   public class wrapperClassController {

//Our collection of the class/wrapper objects cContact 
public List<cContact> contactList {get; set;}

//This method uses a simple SOQL query to return a List of Contacts
public List<cContact> getContacts() {
    if(contactList == null) {
        contactList = new List<cContact>();
        for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
            contactList.add(new cContact(c));
        }
    }
    return contactList;
}


public PageReference processSelected() {

            //We create a new list of Contacts that we be populated only with Contacts if they are selected
    List<Contact> selectedContacts = new List<Contact>();

    //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
    for(cContact cCon: getContacts()) {
        if(cCon.selected == true) {
            selectedContacts.add(cCon.con);
        }
    }

    // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
    System.debug('These are the selected Contacts...');
    for(Contact con: selectedContacts) {
        system.debug(con);
    }
    contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
    return null;
}


// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
public class cContact {
    public Contact con {get; set;}
    public Boolean selected {get; set;}

    //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
    public cContact(Contact c) {
        con = c;
        selected = false;
    }
}}
  • I have deleted the class. deleted the browsing history of chrome. again i created a new class in the apex code page. again showing like this only. – muralimano May 6 '16 at 9:17
  • Are you able to save the same code in different org ? – iamsfdc May 6 '16 at 10:06
0

Try to modify the API version of the apex class and save. If this resolves your issue. I have also copied the same code and save it in my org. Perfectly saved!!!

  • he tried creating a new class (new version) but didn't work. :) – iamsfdc May 6 '16 at 10:22
0

That is strange

 public List<Contact> getContacts() {
    if(contactList == null) {
        contactList = [select Id, Name, Email, Phone from Contact limit 10];
    }
    return contactList;
}

I have sometimes tried that Salesforce would not allow me to save eventhough the code was fine. Then I tried it in a different way and then it worked, even if I copied in my old code.

Does you code say:

list<cContact>  ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.