You need to sign in to do that
Don't have an account?
Matt Tindall
Trigger Error.
Hello,
I am trying to create a trigger that will select the quantity of products that an opportunity has using a specific criteria.
The issue I'm having is:
1) I am un sure on how to make the query string match the opportunity from the list.
2) The string i am using is giving the following error "Illegal assignment from LIST<aggregateResult> to Decimal
Here is the code i am trying to use:
trigger CountWasteBins on Opportunity (after UPDATE) {
// List all customers
list<Opportunity> CustomerList = [SELECT id, General_Waste_Containers__c FROM Opportunity];
// Update all customers from the list
public void UpdateField()
{
for(Opportunity o : CustomerList)
{
o.General_Waste_Containers__c = [SELECT sum(Quantity) FROM OpportunityLineItem WHERE PricebookEntry.Product2.Family = 'General Waste'];
}
}
public void UpdateCustomerList()
{
// Update List
update CustomerList;
}
}
I am trying to create a trigger that will select the quantity of products that an opportunity has using a specific criteria.
The issue I'm having is:
1) I am un sure on how to make the query string match the opportunity from the list.
2) The string i am using is giving the following error "Illegal assignment from LIST<aggregateResult> to Decimal
Here is the code i am trying to use:
trigger CountWasteBins on Opportunity (after UPDATE) {
// List all customers
list<Opportunity> CustomerList = [SELECT id, General_Waste_Containers__c FROM Opportunity];
// Update all customers from the list
public void UpdateField()
{
for(Opportunity o : CustomerList)
{
o.General_Waste_Containers__c = [SELECT sum(Quantity) FROM OpportunityLineItem WHERE PricebookEntry.Product2.Family = 'General Waste'];
}
}
public void UpdateCustomerList()
{
// Update List
update CustomerList;
}
}
set<id> opSet = new set<id>();
for(opportunity o:trigger.new){
opSet.add(o.id);
}
map<id,decimal> aggMap = new map<id,decimal>();
for(aggregateResult r: [SELECT sum(Quantity), OpportunityId FROM OpportunityLineItem WHERE PricebookEntry.Product2.Family = 'General Waste' GROUP BY OpportunityId]){
aggMap.put(string.valueof(r.get('OpportunityId')),decimal.valueof(r.get('expr0')));
}
for(opportunity o:trigger.new){
o.General_Waste_Containers__c = 0;
if(aggMap.get(o.id) != null){
o.General_Waste_Containers__c = aggMap.get(o.id);
}
}
Hi Blake,
Thanks for your reply, but after i have tried this code I'm getting an error on line 10 "Variable does not exist: decimal".
aggMap.put(string.valueof(r.get('OpportunityId')),decimal.valueof(r.get('expr0')));
Any ideas?
aggMap.put(string.valueof(r.get('OpportunityId')),double.valueof(r.get('expr0')));