My full error message is as follows

Error: Compile Error: Method does not exist or incorrect signature: void add(AggregateResult) from the type List at line 21 column 41

I am working on building a page to represent items in a shipment, that page must show the items picked for the shipment and list their locations in our inventory below each product picked. I am grabbing the information I need from an object called Shipment_Line__c that holds a relation what products have been picked for shipment and the other object is GMBLASERP__Inventory_Movement_Cube__c and it represents where a product is in our inventory. I am needing to match up the two objects based on Project_Task__r.Product_Code__c from Shipment_Line__c and GMBLASERP__Product__c from GMBLASERP__Inventory_Movement_Cube__c.

My controller is as follows:

public class ShipmentInventoryMovements {
    public List<Shipment_Line__c> allShipmentLines { get; set; }
    public List<Shipment_Line__c> displayShipmentLines { get; set; }
    public Map<Id, List<GMBLASERP__Inventory_Movement_Cube__c>> inventoryMap { get; set; }

    public ShipmentInventoryMovements ( ) {
        allShipmentLines = new List<Shipment_Line__c>();
        allShipmentLines = [SELECT Id, Product_ID__c, Shipment__r.Warehouse__r.Name, Quantity__c, Quantity_Filled__c, Project_Task__r.GMBLASERP__Product__r.Name, Project_Task__r.Product_Code__c FROM Shipment_Line__c];

        displayShipmentLines = new List<Shipment_Line__c>(allShipmentLines);
        inventoryMap = new Map<Id, List<GMBLASERP__Inventory_Movement_Cube__c>>();
        for (Shipment_Line__c sl : allShipmentLines) {
            inventoryMap.put(sl.Product_ID__c, new
        List<AggregateResult> shipInventory = new List<AggregateResult>([SELECT GMBLASERP__Product__r.id ID, GMBLASERP__Product__r.Name ProdName, GMBLASERP__Location__r.Name LocName, SUM(GMBLASERP__Quantity__c) Sum
                FROM GMBLASERP__Inventory_Movement_Cube__c WHERE GMBLASERP__Product__c IN
                        :inventoryMap.keyset() GROUP BY GMBLASERP__Product__r.id, GMBLASERP__Product__r.name, GMBLASERP__Location__r.name]);

        for(AggregateResult shipLine : shipInventory){

AggregateResult is not well-documented but it is an object, so you cannot add it to a list of some other object like GMBLASERP__Inventory_Movement_Cube__c. You must iterate through your aggregate result records, extract and cast the values, and create a GMBLASERP__Inventory_Movement_Cube__c record where you assign the values.

For example:

for(AggregateResult shipLine : shipInventory){
  Id theProdId = (Id) shipline.get('ID');
  String theProdName = (String) shipline.get('prodName');
  Integer theSum = (Integer) shipline.get('Sum');
  GMBLASERP__Inventory_Movement_Cube__c theCube = new GMBLASERP__Inventory_Movement_Cube__c(ProdName__c = theProdName, Sum__c = theSum);

See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm for more information.

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.