+ Start a Discussion
cmtgolf05cmtgolf05 

AggregateResult issues

 I am trying for the first time to use the group by function in SOQL. However I am getting the following error with the code I have:

 

"Sobject constructor must use name=value pairs"

 

Here is my code:

 

List<OpportunityLineItem> myOCR;

public List<OpportunityLineItem> getOpportunity() {
if (myOCR == null) { AggregateResult[] myOCR = [Select SUM(ProductTotal__c), SUM(Supplier_Price__c), Opportunity.Sales_Initials__c from                     OpportunityLineItem where opportunity.closedate = YESTERDAY group by Opportunity.Sales_Initials__c ];
for (AggregateResult ar : myOCR) {
myOCR.add(new OpportunityLineItem(string.valueof(myOCR.get('Opportunity.Sales_Initials__c')),
String.valueOf(myOCR.get('ProductTotal__c')),String.valueOf(myOCR.get('Supplier_Price__c'))));

}

}

return myOCR;
}

 

It is indicating the error is coming from "myOCR.add(new ......." Line

 

Any Help?

 

Thank you,

Satish_SFDCSatish_SFDC

Yes the error is in this line.

 

myOCR.add(
  new OpportunityLineItem(
string.valueof(myOCR.get('Opportunity.Sales_Initials__c')),String.valueOf(myOCR.get('ProductTotal__c')),String.valueOf(myOCR.get('Supplier_Price__c'))
)
);

 

I see that you are creating new OpportunityLineItems by passing the values directly in the Constructor of the OpportunityLineItem.

 

Instead of that you should pass name value pairs.

For instance, if i want to create a new account and then pass the values for the new account in the constructor, i will have to use the syntax:

 

Account a = new Account(name = 'Acme', billingcity = 'San Francisco');

 

So while creating your new OpportunityLineItem, specify the field name to which the value you passed belongs.

 

 

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.