function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TC AdminTC Admin 

error on trigger - execution of AfterUpdate caused by: System.ListException:

Hi All,
I have created the below code for a trigger that once an opportuntiylineitem is added, edited, deleted or undelted it SUM() the total of all product ids on opportuntilineitems as a particular status.

However I get the below error when you try to update an opportunitylineitem in Salesforce:
LookupRollup: execution of AfterUpdate caused by: System.ListException: Row with null Id at index: 0 Trigger.LookupRollup: line 11, column 1

This is my code for the trigger:
trigger LookupRollup on OpportunityLineItem (after insert,after update, after delete,after undelete) {

     //Declare Variables
    List<Product2> ProductIDs = new List<Product2>();

	//Get list of all product codes on unprocessed orders
    ProductIDs = [ SELECT id,Other_Details__c
							FROM Product2 ];

	//map productid to totals on orders
	MAP<id,AggregateResult > ProductTotal = new MAP<id,AggregateResult >(
        	[SELECT Product2Id,SUM(NUMBER_OF_UNITS__c)vol
			FROM OpportunityLineItem 
			WHERE (Opportunity.Sage_Status__c like 'Processing%' 
			OR Opportunity.Sage_Status__c like 'Despatched%')
             GROUP BY Product2Id]) ;
    
	//Update to product
	FOR (Product2 pt : ProductIds) {
        STRING OnOrderTotal = string.ValueOf(ProductTotal.get(pt.id));
		pt.Other_details__C = OnOrderTotal;
		UPSERT pt;
	}

	}

Any ideas?
Best Answer chosen by TC Admin
David Zhu 🔥David Zhu 🔥
You may change that line a bit:
List<AggregateResult> AggregateResultList = [SELECT Product2Id,SUM(NUMBER_OF_UNITS__c)vol FROM OpportunityLineItem WHERE (Opportunity.Sage_Status__c like 'Processing%' OR Opportunity.Sage_Status__c like 'Despatched%') GROUP BY Product2Id]) ;

MAP<id,AggregateResult > ProductTotal = new MAP<id,AggregateResult >();

for (AggregateResult ar: AggregateResultList)
{
     ProductTotal.put(Id.valueof(string.valueof(ar.get('product2Id'))),ar);
}


 

All Answers

David Zhu 🔥David Zhu 🔥
When using Map<Id,xxx> with soql, there must be ID field in the Select command.
e.g. map<id,account> accountMap =  new Map<id,account>[Select ID, xxx from Account];
When using aggregate function, ID not present in Select command.

You may need to change the code as below:

List<AggregateResult> AggregateResultList = [SELECT Product2Id,SUM(NUMBER_OF_UNITS__c)vol FROM OpportunityLineItem WHERE (Opportunity.Sage_Status__c like 'Processing%' OR Opportunity.Sage_Status__c like 'Despatched%') GROUP BY Product2Id]) ;

MAP<id,AggregateResult > ProductTotal = new MAP<id,AggregateResult >();

for (AggregateReulst ar: AggregateResultList)
{
ProductTotal .put (ar.product2Id,ar);
}
TC AdminTC Admin
Hi David, OK so I see where you are coming from. Only thing is then is it doesnt recongised ar.producy2id, ar as a variable?
David Zhu 🔥David Zhu 🔥
Change this line ProductTotal .put (ar.product2Id,ar);


for (AggregateReulst ar: AggregateResultList)
{
ProductTotal .put (ID.valueof(ar.get('product2Id'),ar);
}

 
Dhanraj kumar 7Dhanraj kumar 7
https://www.youtube.com/watch?v=DLYMyhHs5zI
https://www.youtube.com/watch?v=noQWH7f8SqU
TC AdminTC Admin
Hi David, I get error message - Method does not exsit or incorrect signature: void ValueOf)Object aggregateresult) from the type ID on this line:
ProductTotal.put (ID.valueof(ar.get('product2Id'),ar);

 
David Zhu 🔥David Zhu 🔥
You may change that line a bit:
List<AggregateResult> AggregateResultList = [SELECT Product2Id,SUM(NUMBER_OF_UNITS__c)vol FROM OpportunityLineItem WHERE (Opportunity.Sage_Status__c like 'Processing%' OR Opportunity.Sage_Status__c like 'Despatched%') GROUP BY Product2Id]) ;

MAP<id,AggregateResult > ProductTotal = new MAP<id,AggregateResult >();

for (AggregateResult ar: AggregateResultList)
{
     ProductTotal.put(Id.valueof(string.valueof(ar.get('product2Id'))),ar);
}


 
This was selected as the best answer
TC AdminTC Admin
David you are a legend - and i've learnt alot thank you.