0

I am trying to use the wrapper class example from https://developer.salesforce.com/page/Wrapper_Class Instead of using the contact object I am trying to use my own custom object LGFProduct__c. I am getting the error "SObject constructor must use name=value pairs" from line 12.

Here is my code:

public class wrapperClassExample1 {

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

    //This method uses a simple SOQL query to return a List of Contacts
    public List<LGFProduct__c> getProducts() {
        if(contactList == null) {
            contactList = new List<LGFProduct__c>();
            for(LGFProduct__c  c: [select Id, Name from LGFProduct__c limit 10]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new LGFProduct__c(c)); //line 12
            }
        }
        return contactList;
    }

public PageReference processSelected() {

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

        //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(LGFProduct__c cCon: getProducts()) {
            if(cCon.selected == true) {
                selectedProducts.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(LGFProduct__c con: selectedProducts) {
            system.debug(con);
        }
        contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
        return null;
    }


}
2
  • 2
    Just do contactList.add(c)?
    – Adrian Larson
    Commented Aug 17, 2016 at 19:10
  • That did the trick! Though now I have a new error to fix. XP Commented Aug 17, 2016 at 19:18

1 Answer 1

3

You can't construct an sObject with itself as a parameter. But then, you shouldn't have to. You can add it to the list simply using:

contactList.add(c);

You can even just declare the whole list using the query.

 contactList = [/*query*/];

You must log in to answer this question.

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