0

I have a custom object

     @AuraEnabled 
   public static List<Donation__c> onLoad(string recordId){  
   List<Donation__c> donList = new List<Donation__c>();
     Contact con = [select id, Name, (select id, Name, Email1__c from  Donations__r) from Contact where id=:recordId];  
     for(Contact c: con.Donations__r){  
       donList.add(new Donation__c(c,false)); 

   }   
    return donList;  
   }  

but in this line:

 donList.add(new Donation__c(c,false)); 

Compile Error:

Type requires name=value pair construction: Donation__c

Any advice? Thanx

1
  • What is it you expect this constructor to do? What is the Donation__c supposed to do with the Contact record, or with this Boolean?
    – Adrian Larson
    Nov 10, 2019 at 21:06

2 Answers 2

3

It looks like what you are trying to accomplish is to return all child records for the specified Contact.Id. Just query on and return Donation__c records directly. If your lookup field has an API Name of Contact__c, your entire method body would look like:

return [SELECT Name, Email1__c FROM Donation__c WHERE Contact__c = :recordId];
1
  • Thank you Adrian.
    – Greg Jason
    Nov 19, 2019 at 21:35
1

The compiler error is telling you that you're not using the correct syntax to create an instance of a custom object record. You need to explicitly call out a field that you want to set. So something like the below:

donList.add(new Donation__c(Contact__c = c.Id , My_Boolean_Field__c = false)); 

However, Adrian Larson's answer is a better example of how to do what it seems you're trying to accomplish.

1
  • Thank you Nicholas.
    – Greg Jason
    Nov 19, 2019 at 21:36

You must log in to answer this question.

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