+ Start a Discussion
Girbson Bijou 7Girbson Bijou 7 

ist has more than 1 row for assignment to SObject error

Help please, I test the SOQL query in developer Console, and work as expected But, i get error message  in the VF

My Controller is: 

public class InventoryReport {

     public List<Articles_Containers__c> allproduct{get;set;}

     public InventoryReport() {

    AggregateResult allproduct = [
      SELECT Product_Hiden_Name__c, UM__c ,SUM(On_Hand__c)onHand,  SUM(Pending__c)pending,  SUM(Available__c)avail 
      FROM Articles_Containers__c 
         GROUP BY Product_Hiden_Name__c , UM__c 
         HAVING SUM(On_Hand__c)>0
          ORDER BY Product_Hiden_Name__c, UM__c
      limit 1000];

      }

}

VF Page is:

<apex:page controller="InventoryReport" showHeader="true" RenderAs="PDF">
<div class="table">
       <div class="tableHeader">
              <th>Item</th>
              <th>Unit Of Measure </th>
              <th>On Hand </th>
              <th>Pendig </th>
              <th>Available </th>
      </div>
<apex:repeat value="{!allproduct}" var="a"> <!-- <div class="tablebody"> -->
<tr>
   <td>{!a.Product_Hiden_Name__c }</td>
    <td>{!a.UM__c}</td>
    <td>{!a.On_Hand__c}</td>
    <td>{!a.Pending__c}</td>
    <td>{!a.Available__c}</td>
</tr>
  </apex:repeat> </div>
</apex:page>
Zhen Yueh LeanZhen Yueh Lean
Hi Girbson, AggregateResult is a little different from normal object. You have to access the value using {!a['onHand']}. See this thread for the full example: https://salesforce.stackexchange.com/a/84770
Girbson Bijou 7Girbson Bijou 7
Hi Zhen , thank you for replying, I Change it but i still get the same error mesage.
Zhen Yueh LeanZhen Yueh Lean
Hi Girbson, do you mind to paste your latest code here?
Girbson Bijou 7Girbson Bijou 7
VF is :
<apex:page controller="InventoryReport" renderAs="pdf" showHeader="false" sidebar="false"  standardStylesheets="false"    applyBodyTag="false" >

   <div class="table">
        
        <div class="tableHeader">       
            <th>ITEM</th> 
            <th>UNIT OF MEASURE </th> 
            <th>ON HAND </th> 
            <th>PENDING </th> 
            <th>AVAILABLE </th>
            
        </div>

        <apex:repeat value="{!allproduct}" var="a"> 
         <!--  <div class="tablebody"> -->
               
  <tr>
     
     <td>{!a['Product_Hiden_Name__c'] }</td>
     <td>{!a['UM__c']}</td>
     <td>{!a['OnHand']}</td> 
     <td>{!a['Pending']}</td>
     <td>{!a['avail']}</td>
  </tr> 
           
        </apex:repeat>
    
     </div>
</apex:page>

Controller IS:

public class InventoryReport {

      public List<AggregateResult> allproduct{get;set;}

     public InventoryReport() {

   allproduct = [
      
      
      
      SELECT Product_Hiden_Name__c, UM__c ,SUM(On_Hand__c)onHand,  SUM(Pending__c)pending,  SUM(Available__c)avail 
      FROM Articles_Containers__c 
      WHERE IsOpened__c = 1
      GROUP BY Product_Hiden_Name__c , UM__c 
      HAVING SUM(On_Hand__c) >0 
     ORDER BY Product_Hiden_Name__c, UM__c limit 1000];
      
      }
}
Zhen Yueh LeanZhen Yueh Lean
Hi Girbson, your code looks ok. What is the error that you got? I tried something similar and it works for me:
 
public class AggregateResultController{
    
    public List<AggregateResult> accs {set; get;}
    
    public AggregateResultController(){
        accs = [select count(id) total, Active__c from account where Active__c != '' group by active__c];
    }    
    
}

<apex:page controller="AggregateResultController">
    <apex:repeat value="{!accs}" var="a">
        
        <p>{!a['Total']}</p>
        <p>{!a['Active__c']}</p>
    </apex:repeat>
</apex:page>